結果

問題 No.545 ママの大事な二人の子供
ユーザー assy1028assy1028
提出日時 2017-11-21 12:59:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,148 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 115,644 KB
実行使用メモリ 4,560 KB
最終ジャッジ日時 2023-08-17 11:44:48
合計ジャッジ時間 2,737 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,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 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
4,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 10 ms
4,400 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <tuple>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

int n;
ll a[32], b[32];

ll solve() {
    vector<ll> dif0, dif1;
    int m = n / 2, l = n - m;
    for (int i = 0; i < (1<<m); i++) {
        ll d = 0;
        for (int j = 0; j < m; j++) {
            if ((i>>j)&1) d += a[j];
            else d -= b[j];
        }
        dif0.push_back(d);
    }
    for (int i = 0; i < (1<<l); i++) {
        ll d = 0;
        for (int j = m; j < n; j++) {
            if ((i>>j)&1) d += a[j];
            else d -= b[j];
        }
        dif1.push_back(d);
    }
    sort(ALL(dif1));

    ll res = 1e18;
    for (int i = 0; i < dif0.size(); i++) {
        int k = (int)(lower_bound(ALL(dif1), -dif0[i]) - dif1.begin());
        res = min(res, abs(dif0[i] + dif1[k]));
        if (k > 0) res = min(res, abs(dif0[i] + dif1[k-1]));
        if (k < dif1.size()-1) res = min(res, abs(dif0[i] + dif1[k+1]));
    }
    return res;
}

void input() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}
0