結果

問題 No.527 ナップサック容量問題
ユーザー ikd
提出日時 2017-11-26 23:10:29
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 991 bytes
コンパイル時間 746 ms
コンパイル使用メモリ 101,132 KB
実行使用メモリ 42,708 KB
最終ジャッジ日時 2024-06-12 22:42:29
合計ジャッジ時間 2,748 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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