結果

問題 No.545 ママの大事な二人の子供
ユーザー Konton7Konton7
提出日時 2020-03-03 13:46:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 2,554 ms
コンパイル使用メモリ 204,112 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-04-21 23:55:50
合計ジャッジ時間 4,500 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 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,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 5 ms
6,940 KB
testcase_20 AC 9 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 34 ms
6,940 KB
testcase_23 AC 80 ms
9,728 KB
testcase_24 AC 33 ms
7,040 KB
testcase_25 AC 74 ms
9,984 KB
testcase_26 AC 76 ms
9,984 KB
testcase_27 AC 71 ms
9,984 KB
testcase_28 AC 80 ms
9,856 KB
testcase_29 AC 76 ms
9,856 KB
testcase_30 AC 86 ms
9,984 KB
testcase_31 AC 77 ms
9,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define allpt(v) (v).begin(), (v).end()

const int mod = 1e9 + 7;
const string wsp = " ";
const string tb = "\t";
const string rt = "\n";

template <typename T>
void show1dvec(vector<T> v)
{
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << wsp;
    cout << v[n] << rt;
    return;
}

void makeset(int ps, set<ll> &p_set, vector<PLL> p)
{
    ll a, b, r;
    rep(i, pow(2, ps))
    {
        a = b = 0;
        r = i;
        rep(j, ps)
        {
            if (r % 2)
                a += p[j].first;
            else
                b += p[j].second;
            r /= 2;
        }
        p_set.insert(a - b);
    }
}

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, ps, qs, r;
    cin >> n;
    ll a, b, ab, ans;
    vector<PLL> p, q;
    set<ll> p_set, q_set;

    rep(i, n)
    {
        cin >> a >> b;
        if (i % 2)
            p.push_back(make_pair(a, b));
        else
            q.push_back(make_pair(a, b));
    }
    ps = p.size(), qs = q.size();

    makeset(ps, p_set, p);
    makeset(qs, q_set, q);


    ans = 1e16;
    for (auto z : p_set)
    {
        auto itrr = q_set.lower_bound(-z);
        auto itrl = itrr;
        if (itrr != q_set.begin())
            itrl--;
        if (itrr == q_set.end())
            itrr--;

        ab = min(abs(*itrl + z), abs(*itrr + z));
        ans = min(ans , ab);
    }
    cout << ans << rt;
    return 0;
}
0