結果
問題 | No.545 ママの大事な二人の子供 |
ユーザー |
|
提出日時 | 2017-07-15 00:01:17 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,505 bytes |
コンパイル時間 | 824 ms |
コンパイル使用メモリ | 84,028 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-10-08 00:12:30 |
合計ジャッジ時間 | 2,001 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 WA * 6 |
ソースコード
#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; }