結果

問題 No.545 ママの大事な二人の子供
ユーザー dsaito11dsaito11
提出日時 2019-08-05 19:57:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,478 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 91,076 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 17:26:16
合計ジャッジ時間 2,957 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//#include <bits/stdc++.h>

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_map>
#include <map>

#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;

int main(){
    long long int n;
    cin >> n;

    vector<long long int> a(n), b(n);
    long long int i, j;
    for(i = 0; i < n; i++){
        cin >> a[i] >> b[i];
    }

    long long int l = n/2;
    long long int r = n-n/2;
    vector<long long int> p, q;

    //前半を全列挙
    long long int sum = 0;
    for(i = 0; i < (1<<l); i++){
        sum = 0;
        for(j = 0; j < l; j++){
            if((i>>j)%2 == 1){
                sum += a[j];
            }else{
                sum -= b[j];
            }
        }
        p.push_back(sum);
    }
    sort(p.begin(), p.end());

    long long int ans = INF;
    long long int k;
    for(i = 0; i < (1<<r); i++){
        sum = 0;
        for(j = 0; j < r; j++){
            if((i>>j)%2 == 1){
                sum += a[j+l];
            }else{
                sum -= b[j+l];
            }
        }
        k = lower_bound(p.begin(), p.end(), -sum) - p.begin();
        if(0 <= k && k < p.size()) ans = min(ans, abs(sum+p[k]));
        if(0 <= k-1 && k-1 < p.size()) ans = min(ans, abs(sum+p[k-1]));
        if(0 <= k+1 && k+1 < p.size()) ans = min(ans, abs(sum+p[k+1]));
    }

    cout << ans << endl;
}

0