結果

問題 No.545 ママの大事な二人の子供
ユーザー tnakao0123tnakao0123
提出日時 2017-07-22 00:17:11
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,886 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 88,148 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-17 10:47:07
合計ジャッジ時間 1,790 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 545.cc: No.545 ママの大事な二人の子供 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 32;
const int MAX_H = MAX_N / 2;
const int HBITS = 1 << MAX_H;

typedef long long ll;
const ll LINF = 1LL << 60;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;

/* global variables */

ll as[MAX_N], bs[MAX_N];
ll vp[HBITS], vq[HBITS];

/* subroutines */

void setv(int i0, int i1, ll v[]) {
  int k = i1 - i0;
  int kbits = 1 << k;

  for (int bits = 0; bits < kbits; bits++) {
    ll sum = 0;
    for (int i = i0, bi = 1; i < i1; i++, bi <<= 1) {
      if (bits & bi) sum += bs[i];
      else sum -= as[i];
    }
    v[bits] = sum;
  }
}

void printv(int n, ll v[]) {
  for (int i = 0; i < n; i++) printf("%lld ", v[i]); putchar('\n');
}

/* main */

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> as[i] >> bs[i];

  if (n == 1) {
    printf("%lld\n", min(as[0], bs[0]));
    return 0;
  }

  int pn = n / 2, qn = n - pn;
  int pbits = 1 << pn, qbits = 1 << qn;
  setv(0, pn, vp);
  setv(pn, n, vq);
  sort(vp, vp + pbits);
  sort(vq, vq + qbits);
  //printv(pbits, vp), printv(qbits, vq);
  
  ll mind = LINF;

  for (int bits = 0; bits < pbits; bits++) {
    ll &p = vp[bits];
    int qi = lower_bound(vq, vq + qbits, -p) - vq;

    if (qi > 0) {
      ll d0 = abs(p + vq[qi - 1]);
      if (mind > d0) mind = d0;
    }
    if (qi < qn) {
      ll d1 = abs(p + vq[qi]);
      if (mind > d1) mind = d1;
    }
  }

  printf("%lld\n", mind);
  return 0;
}
0