結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
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 2 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 < pow(2,n/2); i++) {
        ll rem=i;
        ll A=0;
        ll B=0;
        for (int j = 0; j < n/2; j++) {
            if(rem/pow(2,j)>0){
                rem-=pow(2,j);
                A+=a[j]; 
            }else{
                B+=b[j];
            }
        }
        A_B[i]=A-B;
    }
    sort(A_B,A_B+n/2+1);
    
    
    ll A_B_2[65536];
    for (int i = 0; i < pow(2,n-n/2); i++) {
        ll rem=i;
        ll A=0;
        ll B=0;
        for (int j = 0; j < n-n/2; j++) {
            if(rem/pow(2,j)>0){
                rem-=pow(2,j);
                A+=a[j+n/2]; 
            }else{
                B+=b[j+n/2];
            }
        }
        A_B_2[i]=A-B;
    }
    sort(A_B_2,A_B_2+n-n/2+1);
    
    ll res=5000000000;
    for (int i = 0; i < 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,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