結果

問題 No.545 ママの大事な二人の子供
ユーザー ldsybldsyb
提出日時 2017-07-15 16:01:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 905 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 77,520 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 21:48:22
合計ジャッジ時間 1,977 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 12 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 8 ms
5,376 KB
testcase_25 AC 18 ms
5,376 KB
testcase_26 AC 23 ms
5,376 KB
testcase_27 AC 17 ms
5,376 KB
testcase_28 AC 25 ms
5,376 KB
testcase_29 AC 20 ms
5,376 KB
testcase_30 AC 19 ms
5,376 KB
testcase_31 AC 19 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
	int n;
	cin >> n;
	int64_t a[n / 2], b[n / 2], c[n - n / 2], d[n - n / 2];
	for(int i = 0; i < n / 2; i++) cin >> a[i] >> b[i];
	for(int i = 0; i < n - n / 2; i++) cin >> c[i] >> d[i];
	vector<int64_t> v, w;
	for(int i = 0; i < 1 << (n / 2); i++){
		int64_t puni = 0;
		for(int j = 0; j < n / 2; j++){
			if(i >> j & 1) puni += a[j];
			else puni -= b[j];
		}
		v.emplace_back(puni);
	}
	for(int i = 0; i < 1 << (n - n / 2); i++){
		int64_t puni = 0;
		for(int j = 0; j < n - n / 2; j++){
			if(i >> j & 1) puni += c[j];
			else puni -= d[j];
		}
		w.emplace_back(puni);
	}
	sort(w.begin(), w.end());
	int64_t ans = 1ll << 60;
	for(int64_t i:v){
		auto itr = lower_bound(w.begin(), w.end(), -i);
		ans = min(ans, abs(i + *itr));
		if(itr != w.begin()) ans = min(ans, abs(i + *--itr));
	}
	cout << ans << endl;
}
0