結果

問題 No.545 ママの大事な二人の子供
ユーザー kappybarkappybar
提出日時 2020-08-12 20:22:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,390 bytes
コンパイル時間 1,804 ms
コンパイル使用メモリ 171,264 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 00:49:54
合計ジャッジ時間 3,064 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 13 ms
5,376 KB
testcase_23 AC 25 ms
5,376 KB
testcase_24 AC 9 ms
5,376 KB
testcase_25 AC 21 ms
5,376 KB
testcase_26 AC 25 ms
5,376 KB
testcase_27 AC 17 ms
5,376 KB
testcase_28 AC 26 ms
5,376 KB
testcase_29 AC 20 ms
5,376 KB
testcase_30 AC 19 ms
5,376 KB
testcase_31 AC 20 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main(){
    ll n;
    cin >> n;
    vector<ll> a(n),b(n);
    rep(i,n) cin >> a[i] >> b[i];
    vector<ll> one;
    rep(bit,1<<(n/2)){
        ll aa = 0,bb = 0;
        rep(i,n/2){
            if(bit>>i&1){
                aa += a[i];
            }else{
                bb += b[i];
            }
        }
        one.emplace_back(aa-bb);
    }
    vector<ll> two;
    rep(bit,1<<((n+1)/2)){
        ll aa = 0,bb = 0;
        rep(i,(n+1)/2){
            if(bit>>i&1){
                aa += a[n/2+i];
            }else{
                bb += b[n/2+i];
            }
        }
        two.emplace_back(aa-bb);
    }
    sort(two.begin(),two.end());
    ll ans = LINF;
    rep(i,one.size()){
        ll idx = lower_bound(two.begin(),two.end(),-one[i]) - two.begin();
        if(idx==0) ans = min(ans, abs(one[i]+two[idx]) );
        else if(idx==two.size()) ans = min(ans, abs(one[i]+two[idx-1]) );
        else{
            ans = min(ans, abs(one[i]+two[idx]) );
            ans = min(ans, abs(one[i]+two[idx-1]) );
        }
    }
    cout << ans << endl;
    return 0;
}
0