結果

問題 No.545 ママの大事な二人の子供
ユーザー hiyokko2hiyokko2
提出日時 2017-07-14 23:29:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,380 bytes
コンパイル時間 1,439 ms
コンパイル使用メモリ 163,844 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-04-16 20:44:04
合計ジャッジ時間 2,676 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
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 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 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 16 ms
5,376 KB
testcase_23 AC 40 ms
6,400 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 33 ms
6,400 KB
testcase_26 AC 38 ms
6,400 KB
testcase_27 AC 27 ms
6,528 KB
testcase_28 AC 42 ms
6,400 KB
testcase_29 AC 31 ms
6,528 KB
testcase_30 AC 34 ms
6,528 KB
testcase_31 AC 32 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,bg,ed) for(ll i=(bg);i<(ed);i++)
#define REP(i,n) FOR(i,0,n)
#define MOD 1000000007
#define int long long
using namespace std;
typedef long long ll;
const int INF = 1e9;

int n;
int A[40], B[40];

set<int> foo;
int ans = 7e9;

void dfs(int i, int t, int a, int b)
{
    if (n == 1) {
        foo.insert(0);
        return;
    }

    if (i == t) {
        foo.insert(b - a);
        return;
    }

    dfs(i + 1, t, a + A[i], b);
    dfs(i + 1, t, a, b + B[i]);
}

void dfs2(int i, int t, int a, int b)
{
    //cout << "i = " << i << endl;

    if (i == t) {
        int b_a = b - a;
        //auto it = lower_bound(foo.begin(), foo.end(), -b_a);
        auto it = foo.lower_bound(-b_a);
        if (it == foo.end()) {
            it--;
        }
        //cout << "AAA = " << *it << endl;
        //cout << "b_a = " << b_a << endl;
        ans = min(ans, abs(-b_a - *it));
        if (it != foo.begin()) {
            it--;
            ans = min(ans, abs(-b_a - *it));
        }
        return;
    }

    dfs2(i + 1, t, a + A[i], b);
    dfs2(i + 1, t, a, b + B[i]);
}

signed main()
{
    cin >> n;
    REP(i,n) cin >> A[i] >> B[i];

    dfs(0, n / 2, 0, 0);

    //cout << "BBB" << endl;
    //for (auto it=foo.begin(); it!=foo.end(); it++) {
    //    cout << *it << endl;
    //}

    dfs2(n / 2, n, 0, 0);

    cout << ans << endl;
}
0