結果

問題 No.545 ママの大事な二人の子供
ユーザー ikdikd
提出日時 2017-11-24 20:46:17
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,030 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 96,368 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 16:59:56
合計ジャッジ時間 2,626 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 19 ms
4,376 KB
testcase_23 AC 39 ms
4,380 KB
testcase_24 AC 13 ms
4,376 KB
testcase_25 AC 33 ms
4,380 KB
testcase_26 AC 37 ms
4,376 KB
testcase_27 AC 27 ms
4,376 KB
testcase_28 AC 38 ms
4,376 KB
testcase_29 AC 30 ms
4,380 KB
testcase_30 AC 35 ms
4,380 KB
testcase_31 AC 33 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.math, std.range;

  int n; rd(n);
  auto a=new long[](n), b=new long[](n);
  foreach(i; 0..n) rd(a[i], b[i]);

  auto data=new long[](0);
  foreach(bit; 0..(1<<(n/2))){
    long sa=0, sb=0;
    foreach(i; 0..(n/2)){
      if(bit&(1<<i)) sa+=a[i];
      else sb+=b[i];
    }
    data~=(sa-sb);
  }
  sort(data);
  auto p=assumeSorted(data);
  auto m=n-n/2;
  long mn=1_000_000_000_000_000_000;
  foreach(bit; 0..(1<<m)){
    long sa=0, sb=0;
    foreach(i; 0..m){
      if(bit&(1<<i)) sa+=a[i+n/2];
      else sb+=b[i+n/2];
    }
    auto d=sa-sb;
    auto lb=p.lowerBound(-d),
         ub=p.upperBound(-d);
    if(lb.length+ub.length<p.length) mn=0;
    if(lb.length>0) mn=min(mn, (d+lb.back).abs);
    if(ub.length>0) mn=min(mn, (d+ub.front).abs);
  }

  writeln(mn);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
0