結果
| 問題 |
No.545 ママの大事な二人の子供
|
| コンテスト | |
| ユーザー |
TangentDay
|
| 提出日時 | 2017-07-14 22:41:35 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 21 ms / 2,000 ms |
| コード長 | 1,723 bytes |
| コンパイル時間 | 713 ms |
| コンパイル使用メモリ | 87,492 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-07 23:41:14 |
| 合計ジャッジ時間 | 1,733 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
ソースコード
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
VL rekkyo(VL a, VL b){
int n = a.size();
VL ret;
REP(i,1<<n){
ll x1 = 0, x2 = 0;
REP(j,n){
if ((i >> j) & 1){
x1 += a[j];
}else{
x2 += b[j];
}
}
ret.push_back(x1-x2);
}
sort(ALL(ret));
return ret;
}
int main() {
int n;
cin >> n;
VL a1, b1, a2, b2;
REP(i,n){
ll a, b;
cin >> a >> b;
if (i % 2){
a2.push_back(a);
b2.push_back(b);
}else{
a1.push_back(a);
b1.push_back(b);
}
}
if (n == 1){
cout << min(a1[0], b1[0]) << endl;
return 0;
}
VL d1 = rekkyo(a1, b1), d2 = rekkyo(a2, b2);
// REP(i,d1.size()) cout << d1[i] << " ";
// cout << endl;
// REP(i,d2.size()) cout << d2[i] << " ";
// cout << endl;
ll ans = 1e18;
int j = d2.size() - 1;
REP(i,d1.size()){
while (j > 0 && d1[i] + d2[j] > 0) j--;
ans = min(ans, abs(d1[i] + d2[j]));
if (j + 1 < d2.size()) ans = min(ans, abs(d1[i] + d2[j+1]));
}
cout << ans << endl;
return 0;
}
TangentDay