結果

問題 No.545 ママの大事な二人の子供
ユーザー erbowlerbowl
提出日時 2018-06-14 22:34:28
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,778 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 148,176 KB
実行使用メモリ 5,076 KB
最終ジャッジ日時 2023-09-13 04:19:28
合計ジャッジ時間 4,494 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
int main() {
    ll n,a[32],b[32];
    cin >>n;
    for (int i = 0; i < n; i++) {
        cin >>a[i]>>b[i];
    }
    ll A_B[65536];
    for (int i = 0; i < (ll)pow(2,n/2); i++) {
        // std::cout << i << std::endl;
        ll rem=i;
        ll A=0;
        ll B=0;
        ll j=n/2-1;
        while(j>=0){
            if(rem/(ll)pow(2,j)>0){
                rem-=(ll)pow(2,j);
                A+=a[j];
            }else{
                B+=b[j];
            }
            j--;
        }
        A_B[i]=A-B;
        // std::cout << A-B<<" ";
    }
    // std::cout << std::endl;
    sort(A_B,A_B+(ll)pow(2,n/2));
    
    
    ll A_B_2[65536];
    for (int i = 0; i < (ll)pow(2,n-n/2); i++) {
        // std::cout << i << std::endl;
        ll rem=i;
        ll A=0;
        ll B=0;
        ll j=n-n/2-1;
        while(j>=0){
            if(rem/(ll)pow(2,j)>0){
                rem-=(ll)pow(2,j);
                A+=a[j+n/2];
            }else{
                B+=b[j+n/2];
            }
            j--;
        }
        A_B_2[i]=A-B;
        // std::cout << A-B << std::endl;
    }
    sort(A_B_2,A_B_2+(ll)pow(2,n-n/2));
    
    // for (int i = 0; i < (ll)pow(2,n/2); i++) {
    //     std::cout <<  A_B[i]<<" ";
    // }
    // std::cout << std::endl;
    // for (int i = 0; i < (ll)pow(2,n-n/2); i++) {
    //     std::cout <<  A_B_2[i]<<" ";
    // }
    // std::cout << std::endl;
    
    ll res=5000000000;
    for (int i = 0; i < (ll)pow(2,n/2); i++) {
        ll aa=distance(A_B_2,lower_bound(A_B_2, A_B_2+n-n/2+1 , -A_B[i]));
        res=min(res,abs(A_B[i]-A_B_2[aa]));
        if(aa>0){
            res=min(res,abs(A_B[i]-A_B_2[aa-1]));
        }
    }
    std::cout << res << std::endl;
}

0