結果
問題 | No.545 ママの大事な二人の子供 |
ユーザー |
![]() |
提出日時 | 2017-07-14 23:29:04 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 36 ms / 2,000 ms |
コード長 | 1,380 bytes |
コンパイル時間 | 1,360 ms |
コンパイル使用メモリ | 164,080 KB |
実行使用メモリ | 6,528 KB |
最終ジャッジ日時 | 2024-10-07 23:58:37 |
合計ジャッジ時間 | 2,555 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 32 |
ソースコード
#include <bits/stdc++.h>#define FOR(i,bg,ed) for(ll i=(bg);i<(ed);i++)#define REP(i,n) FOR(i,0,n)#define MOD 1000000007#define int long longusing namespace std;typedef long long ll;const int INF = 1e9;int n;int A[40], B[40];set<int> foo;int ans = 7e9;void dfs(int i, int t, int a, int b){if (n == 1) {foo.insert(0);return;}if (i == t) {foo.insert(b - a);return;}dfs(i + 1, t, a + A[i], b);dfs(i + 1, t, a, b + B[i]);}void dfs2(int i, int t, int a, int b){//cout << "i = " << i << endl;if (i == t) {int b_a = b - a;//auto it = lower_bound(foo.begin(), foo.end(), -b_a);auto it = foo.lower_bound(-b_a);if (it == foo.end()) {it--;}//cout << "AAA = " << *it << endl;//cout << "b_a = " << b_a << endl;ans = min(ans, abs(-b_a - *it));if (it != foo.begin()) {it--;ans = min(ans, abs(-b_a - *it));}return;}dfs2(i + 1, t, a + A[i], b);dfs2(i + 1, t, a, b + B[i]);}signed main(){cin >> n;REP(i,n) cin >> A[i] >> B[i];dfs(0, n / 2, 0, 0);//cout << "BBB" << endl;//for (auto it=foo.begin(); it!=foo.end(); it++) {// cout << *it << endl;//}dfs2(n / 2, n, 0, 0);cout << ans << endl;}