結果

問題 No.545 ママの大事な二人の子供
ユーザー nenuonnenuon
提出日時 2017-07-15 02:05:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,223 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 96,732 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-16 20:56:18
合計ジャッジ時間 1,693 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
  int a[n], b[n];
  FOR(i,0,n) {
    scanf("%d%d", a+i,b+i);
  }
  // 半分で作れる差
  int n2 = n / 2;
  vector<int> d;
  FOR(i,0,n2) {
    int 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());
  int ans = 1e9;
  // もう半分で作れるやつ
  FOR(i,0,(n-n2)) {
    int 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,2) {
      if(itr != d.begin()) itr--;
    }
    FOR(j,0,4) {
      if(itr != d.end()) {
        ans = min(ans, abs(sa + *itr));
        itr++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
0