結果

問題 No.545 ママの大事な二人の子供
ユーザー erbowlerbowl
提出日時 2018-06-14 22:40:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 1,851 bytes
コンパイル時間 1,989 ms
コンパイル使用メモリ 148,472 KB
実行使用メモリ 5,052 KB
最終ジャッジ日時 2023-09-13 04:20:35
合計ジャッジ時間 4,873 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 7 ms
4,376 KB
testcase_19 AC 13 ms
4,376 KB
testcase_20 AC 26 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 116 ms
4,376 KB
testcase_23 AC 248 ms
4,776 KB
testcase_24 AC 116 ms
4,380 KB
testcase_25 AC 246 ms
4,888 KB
testcase_26 AC 248 ms
4,812 KB
testcase_27 AC 247 ms
4,776 KB
testcase_28 AC 249 ms
4,832 KB
testcase_29 AC 247 ms
4,792 KB
testcase_30 AC 247 ms
5,052 KB
testcase_31 AC 247 ms
4,716 KB
権限があれば一括ダウンロードができます

ソースコード

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::cout << 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+(ll)pow(2,n-n/2) , -A_B[i]));
        // std::cout << aa << std::endl;
        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