結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 7 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 28 ms
6,528 KB
testcase_23 AC 60 ms
9,728 KB
testcase_24 AC 24 ms
6,656 KB
testcase_25 AC 58 ms
9,728 KB
testcase_26 AC 59 ms
9,856 KB
testcase_27 AC 55 ms
9,728 KB
testcase_28 AC 63 ms
9,728 KB
testcase_29 AC 56 ms
9,728 KB
testcase_30 AC 57 ms
9,728 KB
testcase_31 AC 57 ms
9,600 KB
権限があれば一括ダウンロードができます

ソースコード

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 != v2.begin()) {
            itr--;
            ans = min(abs(*itr + v), ans);
        }
    }
    cout << ans << endl;
    // 足したときの絶対値が最小になるようにしたい
    // min min(v1[j] +v2[i])
}
0