結果

問題 No.545 ママの大事な二人の子供
ユーザー koba-e964koba-e964
提出日時 2017-07-14 23:01:25
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,835 bytes
コンパイル時間 996 ms
コンパイル使用メモリ 98,244 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-16 20:38:49
合計ジャッジ時間 2,538 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 WA -
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 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 32 ms
6,656 KB
testcase_23 AC 74 ms
10,240 KB
testcase_24 WA -
testcase_25 AC 70 ms
10,240 KB
testcase_26 AC 70 ms
10,368 KB
testcase_27 AC 69 ms
10,368 KB
testcase_28 AC 74 ms
10,368 KB
testcase_29 AC 69 ms
10,240 KB
testcase_30 AC 68 ms
10,240 KB
testcase_31 AC 69 ms
10,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;



int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VL d(n);
  ll atot = 0;
  REP(i, 0, n) {
    ll a, b;
    cin >> a >> b;
    atot += a;
    d[i] = b + a;
  }
  sort(d.begin(), d.end());
  set<ll> former;
  set<ll> latter;
  REP(bits, 0, 1 << (n / 2)) {
    ll sum = 0;
    REP(i, 0, n / 2) {
      if (bits & 1 << i) {
	sum += d[i];
      }
    }
    former.insert(sum);
  }
  REP(bits, 0, 1 << (n - (n / 2))) {
    ll sum = 0;
    REP(i, 0, n - n / 2) {
      if (bits & 1 << i) {
	sum += d[i + n / 2];
      }
    }
    latter.insert(sum);
  }
  VL fv(former.begin(), former.end());
  VL lv(latter.begin(), latter.end());
  sort(fv.begin(), fv.end());
  sort(lv.begin(), lv.end());
  ll mi = 1e15;
  REP(i, 0, fv.size()) {
    ll t = fv[i];
    // We are to find position p s.t. abs(atot - t - lv[p]) is smallest.
    ll rem = atot % 2 == 0 ? 0 : 1;
    int lo = upper_bound(lv.begin(), lv.end(), (atot - rem) - t)
      - lv.begin();
    int hi = lower_bound(lv.begin(), lv.end(), (atot + rem) - t)
      - lv.begin();
    if (lo >= 1) {
      mi = min(mi, -(lv[lo - 1] + t) + atot);
    }
    if (hi < (int) lv.size()) {
      mi = min(mi, -atot + (lv[hi] + t));
    }
  }
  cout << mi << "\n";
}
0