結果
問題 | No.545 ママの大事な二人の子供 |
ユーザー |
![]() |
提出日時 | 2020-11-27 08:29:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 24 ms / 2,000 ms |
コード長 | 976 bytes |
コンパイル時間 | 1,002 ms |
コンパイル使用メモリ | 76,928 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-23 21:23:00 |
合計ジャッジ時間 | 1,975 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; const long long INF = 1000000000000; int main(){ int n; cin >> n; vector<long long> A(n), B(n); for (int i = 0; i < n; i++){ cin >> A[i] >> B[i]; } int x = n / 2; int y = n - x; vector<long long> p(1 << x, 0); for (int i = 0; i < (1 << x); i++){ for (int j = 0; j < x; j++){ if (i >> j & 1){ p[i] += A[j]; } else { p[i] -= B[j]; } } } vector<long long> q(1 << y, 0); for (int i = 0; i < (1 << y); i++){ for (int j = 0; j < y; j++){ if (i >> j & 1){ q[i] += A[x + j]; } else { q[i] -= B[x + j]; } } } sort(p.begin(), p.end()); sort(q.begin(), q.end()); long long ans = INF; for (int i = 0; i < (1 << x); i++){ auto itr = lower_bound(q.begin(), q.end(), -p[i]); if (itr != q.end()){ ans = min(ans, abs(p[i] + *itr)); } if (itr != q.begin()){ ans = min(ans, abs(p[i] + *prev(itr))); } } cout << ans << endl; return 0; }