結果

問題 No.545 ママの大事な二人の子供
ユーザー masamasa
提出日時 2017-07-15 00:01:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,505 bytes
コンパイル時間 816 ms
コンパイル使用メモリ 83,588 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-16 20:49:23
合計ジャッジ時間 1,890 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 7 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 30 ms
7,040 KB
testcase_23 WA -
testcase_24 AC 8 ms
6,944 KB
testcase_25 AC 61 ms
10,880 KB
testcase_26 AC 57 ms
11,008 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>

using namespace std;
using LL = long long;

template <class T>
void show(T t) {
	for (auto e : t) {
		printf("%3d ", e);
	}
	printf("\n");
}

set<LL> get_diffs(vector<LL> &va, vector<LL> &vb) {
	set<LL> diffs;
	int m = va.size();

// cout << "get_diffs" << endl;
// cout << m << endl;
// show(va);
// show(vb);


	for (int i = 0; i < (1LL << m); i++) {
		LL sum_a = 0;
		LL sum_b = 0;
		for (int j = 0; j < m; j++) {
			if ((i >> j) & 1) {
				sum_b += vb[j];
			} else {
				sum_a += va[j];
			}
		}
// printf("i %d, diff %lld\n", i, sum_a - sum_b);
		diffs.insert(sum_a - sum_b);
	}
// show(diffs);
	return diffs;
}

int main() {
	int n, n_half, x, y;
	long long ans = 0;
	cin >> n;
	n_half = n / 2;

	vector<LL> a1, b1, a2, b2;
	for (int i = 0; i < n; i++) {
		cin >> x >> y;
		if (i < n_half) {
			a1.emplace_back(x);
			b1.emplace_back(y);
		} else {
			a2.emplace_back(x);
			b2.emplace_back(y);
		}
		ans += x;
	}

	auto diffset1 = get_diffs(a1, b1);
	auto diffset2 = get_diffs(a2, b2);

	auto diffs1 = vector<LL>(diffset1.begin(), diffset1.end());
	auto diffs2 = vector<LL>(diffset2.begin(), diffset2.end());
	// show(diffs1);
	// show(diffs2);
	diffs2.emplace_back(diffs2.back());

	for (auto d: diffs1) {
		auto minus_d = -d;
		auto it = lower_bound(diffs2.begin(), diffs2.end(), minus_d);
		ans = min({ans, abs(d + *it), abs(d + *prev(it))});
	}

	cout << ans << endl;
	return 0;
}
0