結果

問題 No.545 ママの大事な二人の子供
ユーザー 2nanoda series2nanoda series
提出日時 2023-04-22 11:43:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,099 bytes
コンパイル時間 763 ms
コンパイル使用メモリ 84,128 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 16:27:37
合計ジャッジ時間 1,775 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,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
5,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 #

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
void halr_sum(vector<long long int> &a,vector<long long int> &b,int temp_sum,int i,int end, vector<long long int> &sum){
    if(i==end){
        sum.push_back(temp_sum);
        return;
    }
    halr_sum(a,b,temp_sum+a[i],i+1,end,sum);
    halr_sum(a,b,temp_sum-b[i],i+1,end,sum);
}
int main(){
    int n;
    cin >> n;
    vector<long long int> a(n),b(n);
    for(int i=0; i<n; i++){
        cin >> a[i]>>b[i];
    }
    // Sum_i in Select (a[i]) とSum_i in Not Select (b[i])の
    // 差の絶対値の最小値を求める
    // 1<=n<=32
    // 0<=a[i],b[i]<=5*10^9

    // 単純に計算すると2^32=2^10*3 *4 =4* 10^9 なので間に合わない.
    // なお,2^16 = 64*1000=6*10^4である.
    // 従って,グループを2グループに分け,それぞれで全通りで差を計算する.(半分全列挙)
    // この時,差について各 -\inf ~ 0 | \inf がある.
    // 従って,各上,下から確かめれば良い.

    if (n==1){
        cout << min(a[0],b[0]) << endl;
        return 0;
    }
    
    int lc=n/2,rc=n-lc;
    vector<long long int> l(lc),r(rc);

    vector<long long int> lsum,rsum;// それぞれありうる合計値の差異のリスト.

    halr_sum(a,b,0,0,lc,lsum);
    halr_sum(a,b,0,lc,n,rsum);

    sort(lsum.begin(),lsum.end());
    sort(rsum.begin(),rsum.end());

    // cout << lsum.size() <<endl;
    // for(int i=0;i<lsum.size();i++){
    //     cout << lsum[i] << " ";
    // }
    // cout<<endl;
    // for(int i=0;i<rsum.size();i++){
    //     cout << rsum[i] << " ";
    // }
    // cout<<endl;

    long long int ans=5*(long long int)1000000000*32;// lsum,rsumの合計の絶対値最小になる時の値を探す.
    int j=rsum.size()-1;
    long long int temp=ans;
    for(int i=0;i<lsum.size();i++){
        while(temp>0 && j>0){
            temp=lsum[i]+rsum[j];// jを下げていくとtempは減少する.
            ans=min(ans,abs(temp));
            j--;
        }
    }

    cout << ans << endl;

    return 0;
}

0