結果

問題 No.545 ママの大事な二人の子供
ユーザー TangentDayTangentDay
提出日時 2017-07-14 22:41:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 891 ms
コンパイル使用メモリ 87,452 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 20:36:16
合計ジャッジ時間 2,066 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

VL rekkyo(VL a, VL b){
    int n = a.size();
    VL ret;
    REP(i,1<<n){
        ll x1 = 0, x2 = 0;
        REP(j,n){
            if ((i >> j) & 1){
                x1 += a[j];
            }else{
                x2 += b[j];
            }
        }
        ret.push_back(x1-x2);
    }
    sort(ALL(ret));
    return ret;
}

int main() {
    int n;
    cin >> n;
    VL a1, b1, a2, b2;
    REP(i,n){
        ll a, b;
        cin >> a >> b;
        if (i % 2){
            a2.push_back(a);
            b2.push_back(b);
        }else{
            a1.push_back(a);
            b1.push_back(b);
        }
    }

    if (n == 1){
        cout << min(a1[0], b1[0]) << endl;
        return 0;
    }

    VL d1 = rekkyo(a1, b1), d2 = rekkyo(a2, b2);

    // REP(i,d1.size()) cout << d1[i] << " ";
    // cout << endl;
    // REP(i,d2.size()) cout << d2[i] << " ";
    // cout << endl;

    ll ans = 1e18;
    int j = d2.size() - 1;
    REP(i,d1.size()){
        while (j > 0 && d1[i] + d2[j] > 0) j--;
        ans = min(ans, abs(d1[i] + d2[j]));
        if (j + 1 < d2.size()) ans = min(ans, abs(d1[i] + d2[j+1]));
    }
    cout << ans << endl;

    return 0;
}
0