結果

問題 No.545 ママの大事な二人の子供
ユーザー nenuonnenuon
提出日時 2017-07-15 02:10:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,297 bytes
コンパイル時間 1,145 ms
コンパイル使用メモリ 96,556 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 20:56:33
合計ジャッジ時間 2,184 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 13 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 10 ms
5,376 KB
testcase_25 AC 21 ms
5,376 KB
testcase_26 AC 23 ms
5,376 KB
testcase_27 AC 18 ms
5,376 KB
testcase_28 AC 23 ms
5,376 KB
testcase_29 AC 20 ms
5,376 KB
testcase_30 AC 22 ms
5,376 KB
testcase_31 AC 21 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

int main(){
  int n; cin >> n;
  ll a[n], b[n];
  FOR(i,0,n) {
    scanf("%lld%lld", a+i,b+i);
  }
  if(n==1) {
    cout << min(a[0], b[0]) << endl;
    return 0;
  }
  // 半分で作れる差
  int n2 = n / 2;
  vector<ll> d;
  FOR(i,0,1<<n2) {
    ll sa = 0;
    FOR(j,0,n2) {
      if((i>>j)&1) sa += a[j];
      else sa -= b[j];
    }
    d.push_back(sa);
  }
  sort(d.begin(),d.end());
  ll ans = 1e15;
  // もう半分で作れるやつ
  FOR(i,0,1<<(n-n2)) {
    ll sa = 0;
    FOR(j,0,(n-n2)) {
      if((i>>j)&1) sa += a[j+n2];
      else sa -= b[j+n2];
    }
    // 先の半分の差と合わせて差が小さいものを探す->二分探索
    auto itr = lower_bound(d.begin(),d.end(),-sa); // 足して0にしたいので-t
    FOR(j,0,3) {
      if(itr != d.begin()) itr--;
    }
    FOR(j,0,6) {
      if(itr != d.end()) {
        ans = min(ans, abs(sa + *itr));
        itr++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
0