結果

問題 No.2370 He ate many cakes
ユーザー startcppstartcpp
提出日時 2023-07-03 21:45:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,176 bytes
コンパイル時間 1,237 ms
コンパイル使用メモリ 84,164 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-24 18:53:00
合計ジャッジ時間 2,324 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 9 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 9 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//二分探索 + 半分全列挙 O(2^(N/2) * log(MAX)) + QCFirm法
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

int n, K, a[30];

void dfs(int l, int r, int sumA, vector<int> &vec) {
	if (l == r) {
		vec.push_back(sumA);
		return;
	}
	dfs(l + 1, r, sumA, vec);
	dfs(l + 1, r, sumA + a[l], vec);
}

//総和がX以上になる選び方がK通り以上存在するか
bool check(int X, vector<int> &vec1, vector<int> &vec2) {
	int i, j = (int)vec2.size() - 1;
	int ret = 0;
	rep(i, vec1.size()) {
		for (; j >= 0 && vec1[i] + vec2[j] >= X; j--);
		ret += (int)vec2.size() - 1 - j;
	}
	return ret >= K;
}

signed main() {
	int i;
	cin >> n >> K;
	rep(i, n) cin >> a[i];

	vector<int> vec1, vec2;
	dfs(0, n / 2, 0, vec1);
	dfs(n / 2, n, 0, vec2);
	sort(vec1.begin(), vec1.end());
	sort(vec2.begin(), vec2.end());

	int ok = -1e+11, ng = 1e+11, mid;
	while (ng - ok >= 2) {
		mid = (ok + ng) / 2;
		if (check(mid, vec1, vec2)) ok = mid;
		else ng = mid;
	}

	cout << ok << endl;
	return 0;
}
0