結果

問題 No.527 ナップサック容量問題
コンテスト
ユーザー ikd
提出日時 2017-11-26 23:10:29
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 991 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 534 ms
コンパイル使用メモリ 84,952 KB
実行使用メモリ 43,596 KB
最終ジャッジ日時 2026-03-05 17:41:41
合計ジャッジ時間 3,357 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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