結果

問題 No.545 ママの大事な二人の子供
ユーザー pekempeypekempey
提出日時 2017-07-16 18:05:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,079 bytes
コンパイル時間 705 ms
コンパイル使用メモリ 78,012 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 22:35:46
合計ジャッジ時間 1,767 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 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 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 12 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 21 ms
5,376 KB
testcase_26 AC 22 ms
5,376 KB
testcase_27 AC 21 ms
5,376 KB
testcase_28 AC 23 ms
5,376 KB
testcase_29 AC 23 ms
5,376 KB
testcase_30 AC 23 ms
5,376 KB
testcase_31 AC 23 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

int main() {
	int n;
	std::cin >> n;

	const int n1 = n / 2;
	const int n2 = (n + 1) / 2;

	std::vector<long long> a(n), b(n);
	for (int i = 0; i < n; i++) {
		std::cin >> a[i] >> b[i];
	}

	std::vector<long long> d1, d2;
	for (int i = 0; i < 1 << n1; i++) {
		long long diff = 0;
		for (int j = 0; j < n1; j++) {
			if (i >> j & 1) {
				diff += a[j];
			} else {
				diff -= b[j];
			}
		}
		d1.push_back(diff);
	}
	for (int i = 0; i < 1 << n2; i++) {
		long long diff = 0;
		for (int j = 0; j < n2; j++) {
			if (i >> j & 1) {
				diff += a[n1 + j];
			} else {
				diff -= b[n1 + j];
			}
		}
		d2.push_back(diff);
	}

	std::sort(d1.begin(), d1.end());
	std::sort(d2.begin(), d2.end());

	long long ans = 1e18;
	for (int i = 0; i < d1.size(); i++) {
		int j = std::lower_bound(d2.begin(), d2.end(), -d1[i]) - d2.begin();
		for (int d = -1; d <= 1; d++) {
			if (0 <= j + d && j + d < d2.size()) {
				ans = std::min(ans, std::abs(d1[i] + d2[j + d]));
			}
		}
	}
	std::cout << ans << std::endl;
}
0