結果

問題 No.527 ナップサック容量問題
ユーザー ikdikd
提出日時 2017-11-26 23:10:29
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 991 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 86,680 KB
実行使用メモリ 42,896 KB
最終ジャッジ日時 2023-09-03 17:05:53
合計ジャッジ時間 3,390 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,072 KB
testcase_01 AC 4 ms
5,120 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 4 ms
5,904 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 20 ms
19,632 KB
testcase_06 AC 40 ms
36,276 KB
testcase_07 AC 33 ms
30,176 KB
testcase_08 AC 17 ms
17,756 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 40 ms
36,804 KB
testcase_11 AC 28 ms
27,216 KB
testcase_12 AC 23 ms
20,900 KB
testcase_13 AC 43 ms
37,532 KB
testcase_14 AC 16 ms
15,272 KB
testcase_15 AC 24 ms
23,172 KB
testcase_16 AC 4 ms
4,868 KB
testcase_17 AC 20 ms
20,300 KB
testcase_18 AC 22 ms
21,800 KB
testcase_19 AC 4 ms
5,648 KB
testcase_20 AC 16 ms
17,064 KB
testcase_21 AC 48 ms
42,440 KB
testcase_22 AC 33 ms
30,372 KB
testcase_23 AC 46 ms
42,896 KB
testcase_24 AC 10 ms
11,040 KB
testcase_25 AC 31 ms
28,340 KB
testcase_26 AC 28 ms
26,032 KB
testcase_27 AC 30 ms
26,528 KB
testcase_28 AC 24 ms
22,560 KB
testcase_29 AC 47 ms
42,124 KB
testcase_30 AC 6 ms
6,384 KB
testcase_31 AC 19 ms
20,032 KB
testcase_32 AC 24 ms
23,484 KB
testcase_33 AC 22 ms
21,420 KB
testcase_34 AC 24 ms
24,180 KB
testcase_35 AC 26 ms
24,116 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 20 ms
19,112 KB
testcase_38 AC 25 ms
22,876 KB
testcase_39 AC 22 ms
22,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

  const wmax=100000;
  auto rec=new int[][](n+1, wmax+5);
  foreach(i; 0..n){
    if(w[i]<=wmax) chmax(rec[i+1][w[i]], v[i]);
    for(int j=1; j<=wmax; j++){
      chmax(rec[i+1][j], rec[i][j]);
      if(j+w[i]<=wmax && rec[i][j]>0) chmax(rec[i+1][j+w[i]], rec[i][j]+v[i]);
    }
  }
  auto arr=rec[n][0..(wmax+1)]; // [0, wmax]
  for(int i=1; i<=wmax; i++) chmax(arr[i], arr[i-1]);
  auto p=assumeSorted(arr);
  writeln(max(1, p.lowerBound(vmax).length));
  auto ub=p.upperBound(vmax);
  if(ub.length>0) writeln(p.length-ub.length-1);
  else writeln("inf");
  
}

void chmax(T)(ref T x, T y){
  if(x<y) x=y;
}

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