結果
| 問題 |
No.545 ママの大事な二人の子供
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-07-15 00:17:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,489 bytes |
| コンパイル時間 | 993 ms |
| コンパイル使用メモリ | 84,156 KB |
| 実行使用メモリ | 10,880 KB |
| 最終ジャッジ日時 | 2024-10-08 00:20:24 |
| 合計ジャッジ時間 | 2,400 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 WA * 6 |
ソースコード
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
using namespace std;
using LL = long long;
const LL inf = 1LL << 60;
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();
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];
}
}
diffs.insert(sum_a - sum_b);
}
return diffs;
}
int main() {
int n, n_half, x, y;
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);
}
}
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.insert(diffs2.begin(), -inf);
diffs2.emplace_back(inf);
diffs2.emplace_back(inf);
long long ans = inf;
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)),
abs(d + *next(it))
});
}
cout << ans << endl;
return 0;
}