結果

問題 No.626 Randomized 01 Knapsack
ユーザー ikdikd
提出日時 2017-12-15 23:22:50
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 830 bytes
コンパイル時間 1,613 ms
コンパイル使用メモリ 95,460 KB
実行使用メモリ 7,812 KB
最終ジャッジ日時 2023-09-03 17:38:37
合計ジャッジ時間 25,996 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 12 ms
5,388 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 48 ms
7,232 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1,043 ms
7,744 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1,314 ms
7,748 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 8 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

  int n; long wmax; rd(n, wmax);
  auto v=new long[](n), w=new long[](n);
  foreach(i; 0..n) rd(v[i], w[i]);

  size_t B=5000;
  alias T=Tuple!(long, "vsum", long, "wsum");
  T[] cur; cur~=T(0, 0);
  foreach(i; 0..n){
    T[] nex;
    foreach(e; cur){
      nex~=e;
      if(e.wsum+w[i]<=wmax) nex~=T(e.vsum+v[i], e.wsum+w[i]);
    }
    sort!((x, y)=>(x.vsum>y.vsum))(nex);
    if(nex.length>B) cur=nex[0..B];
    else cur=nex;
  }

  writeln(cur[0].vsum);
}

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));
  }
}
void wr(T...)(T x){
  import std.stdio;
  foreach(e; x) stderr.write(e, " ");
  stderr.writeln();
}
0