結果

問題 No.546 オンリー・ワン
ユーザー koba-e964koba-e964
提出日時 2017-07-14 23:04:52
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,783 bytes
コンパイル時間 939 ms
コンパイル使用メモリ 98,148 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 20:30:03
合計ジャッジ時間 1,373 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
権限があれば一括ダウンロードができます

ソースコード

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.
    int lo = upper_bound(lv.begin(), lv.end(), atot - t)
      - lv.begin();
    int hi = lower_bound(lv.begin(), lv.end(), atot - 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