結果

問題 No.545 ママの大事な二人の子供
ユーザー PachicobuePachicobue
提出日時 2017-07-14 22:45:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,356 bytes
コンパイル時間 1,860 ms
コンパイル使用メモリ 170,876 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 20:36:40
合計ジャッジ時間 2,959 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 2 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 15 ms
5,376 KB
testcase_23 AC 28 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 24 ms
5,376 KB
testcase_26 AC 27 ms
5,376 KB
testcase_27 AC 20 ms
5,376 KB
testcase_28 AC 28 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 <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

int main()
{
    int n;
    cin >> n;
    vector<ll> A(n);
    vector<ll> B(n);
    for (ll i = 0; i < n; i++) {
        cin >> A[i] >> B[i];
    }
    const int former_half = n / 2;
    const int latter_half = n - former_half;
    vector<ll> former;
    vector<ll> latter;

    const ll former_maximum = 1 << former_half;
    const ll latter_maximum = 1 << latter_half;

    for (ll i = 0; i < former_maximum; i++) {
        ll num = i;
        ll a = 0;
        ll b = 0;
        for (int j = 0; j < former_half; j++) {
            if (num % 2 == 1) {
                a += A[j];
            } else {
                b += B[j];
            }
            num /= 2;
        }
        former.push_back(a - b);
    }

    for (ll i = 0; i < latter_maximum; i++) {
        ll num = i;
        ll a = 0;
        ll b = 0;
        for (int j = 0; j < latter_half; j++) {
            if (num % 2 == 1) {
                a += A[j + former_half];
            } else {
                b += B[j + former_half];
            }
            num /= 2;
        }
        latter.push_back(a - b);
    }
    sort(latter.begin(), latter.end());
    ll mini = INF<ll>;
    for (ll i = 0; i < former_maximum; i++) {
        const ll need = -former[i];
        auto ite = lower_bound(latter.begin(), latter.end(), need);
        if (ite == latter.end()) {
            if (ite != latter.begin()) {
                mini = min(mini, abs(former[i] + (*(ite - 1))));
            }
        } else {
            mini = min(mini, abs(former[i] + (*ite)));
            if (ite != latter.begin()) {
                mini = min(mini, abs(former[i] + (*(ite - 1))));
            }
        }
    }
    cout << mini << endl;

    return 0;
}
0