結果
| 問題 |
No.545 ママの大事な二人の子供
|
| コンテスト | |
| ユーザー |
nenuon
|
| 提出日時 | 2017-07-15 02:06:34 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,287 bytes |
| コンパイル時間 | 930 ms |
| コンパイル使用メモリ | 96,852 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-08 00:28:58 |
| 合計ジャッジ時間 | 1,801 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 WA * 31 |
ソースコード
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;
int main(){
int n; cin >> n;
ll a[n], b[n];
FOR(i,0,n) {
scanf("%d%d", a+i,b+i);
}
if(n==1) {
cout << min(a[0], b[0]) << endl;
return 0;
}
// 半分で作れる差
int n2 = n / 2;
vector<ll> d;
FOR(i,0,n2) {
ll sa = 0;
FOR(j,0,n2) {
if((i>>j)&1) sa += a[j];
else sa -= b[j];
}
d.push_back(sa);
}
sort(d.begin(),d.end());
ll ans = 1e15;
// もう半分で作れるやつ
FOR(i,0,(n-n2)) {
ll sa = 0;
FOR(j,0,(n-n2)) {
if((i>>j)&1) sa += a[j+n2];
else sa -= b[j+n2];
}
// 先の半分の差と合わせて差が小さいものを探す->二分探索
auto itr = lower_bound(d.begin(),d.end(),-sa); // 足して0にしたいので-t
FOR(j,0,2) {
if(itr != d.begin()) itr--;
}
FOR(j,0,4) {
if(itr != d.end()) {
ans = min(ans, abs(sa + *itr));
itr++;
}
}
}
cout << ans << endl;
return 0;
}
nenuon