結果

問題 No.545 ママの大事な二人の子供
ユーザー hiyokko2hiyokko2
提出日時 2017-07-14 23:23:37
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,337 bytes
コンパイル時間 1,412 ms
コンパイル使用メモリ 164,196 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-04-16 20:42:19
合計ジャッジ時間 7,192 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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);
        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