結果

問題 No.545 ママの大事な二人の子供
ユーザー uenokuuenoku
提出日時 2017-07-21 23:16:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,632 bytes
コンパイル時間 961 ms
コンパイル使用メモリ 100,172 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-04-17 10:06:40
合計ジャッジ時間 2,342 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "math.h"
#include <set>
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long long int lli;
int main()
{
    int n;
    lli a[50], b[50];
    cin >> n;
    rep(i, n) cin >> a[i] >> b[i];
    int l = n / 2;
    int r = n - l;
    set<lli> v1, v2;
    if (n == 1) {
        cout << abs(min(a[0], b[0])) << endl;
        return 0;
    }
    rep(i, 1 << l)
    {
        lli c = 0;
        rep(j, l)
        {
            if ((i >> j) & 1) {
                c += a[j];
            } else {
                c -= b[j];
            }
        }
        v1.insert(c);
    }
    rep(i, 1 << r)
    {
        lli c = 0;
        rep(j, r)
        {
            if ((i >> j) & 1) {
                c += a[j + l];
            } else {
                c -= b[j + l];
            }
        }
        v2.insert(c);
    }
    lli ans = 1e15;
    for (auto v : v1) {
        auto itr = v2.lower_bound(v);
        //    cout << v << " " << *itr << endl;
        if (itr != v2.end()) {
            ans = min(abs(*itr + v), ans);
            itr++;
        }
        if (itr != v2.end()) {
            ans = min(abs(*itr + v), ans);
            itr--;
        }
        if (itr != v1.begin()) {
            itr--;
            ans = min(abs(*itr + v), ans);
        }
    }
    cout << ans << endl;
    // 足したときの絶対値が最小になるようにしたい
    // min min(v1[j] +v2[i])
}
0