結果

問題 No.545 ママの大事な二人の子供
ユーザー MisterMister
提出日時 2020-08-27 19:38:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,172 bytes
コンパイル時間 814 ms
コンパイル使用メモリ 85,584 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-04-25 14:39:10
合計ジャッジ時間 2,431 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 7 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 25 ms
6,940 KB
testcase_23 AC 54 ms
9,728 KB
testcase_24 AC 23 ms
6,940 KB
testcase_25 AC 52 ms
9,472 KB
testcase_26 AC 51 ms
9,600 KB
testcase_27 AC 51 ms
9,728 KB
testcase_28 AC 62 ms
9,600 KB
testcase_29 AC 52 ms
9,600 KB
testcase_30 AC 50 ms
9,728 KB
testcase_31 AC 54 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>

using lint = long long;
constexpr lint INF = 1LL << 50;

lint asum = 0;

std::set<lint> calc(int n) {
    std::vector<lint> ds(n);
    for (auto& d : ds) {
        lint a, b;
        std::cin >> a >> b;
        asum += a;
        d = b + a;
    }

    std::set<lint> ret{-INF, INF};
    for (int b = 0; b < (1 << n); ++b) {
        lint sum = 0;
        for (int i = 0; i < n; ++i) {
            if ((b >> i) & 1) sum += ds[i];
        }
        ret.insert(sum);
    }

    return ret;
}

void solve() {
    int n;
    std::cin >> n;

    auto fs = calc(n / 2),
         bs = calc(n - n / 2);

    lint ans = INF;
    for (auto f : fs) {
        if (std::abs(f) == INF) continue;

        // f + b >= asum, as small as possible
        auto b = *bs.lower_bound(asum - f);
        ans = std::min(ans, std::abs(f + b - asum));

        // f + b <= asum, as large as possible
        b = *(--bs.upper_bound(asum - f));
        ans = std::min(ans, std::abs(f + b - asum));
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0