結果

問題 No.527 ナップサック容量問題
ユーザー tossytossy
提出日時 2017-06-09 23:03:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,339 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 169,644 KB
実行使用メモリ 5,316 KB
最終ジャッジ日時 2023-10-23 23:57:17
合計ジャッジ時間 3,588 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,308 KB
testcase_01 AC 4 ms
5,308 KB
testcase_02 AC 4 ms
5,312 KB
testcase_03 AC 4 ms
5,308 KB
testcase_04 AC 3 ms
5,308 KB
testcase_05 AC 12 ms
5,316 KB
testcase_06 AC 20 ms
5,316 KB
testcase_07 AC 17 ms
5,312 KB
testcase_08 AC 10 ms
5,312 KB
testcase_09 AC 4 ms
5,312 KB
testcase_10 AC 21 ms
5,312 KB
testcase_11 AC 15 ms
5,316 KB
testcase_12 AC 12 ms
5,316 KB
testcase_13 AC 21 ms
5,316 KB
testcase_14 AC 9 ms
5,308 KB
testcase_15 AC 14 ms
5,312 KB
testcase_16 AC 4 ms
5,308 KB
testcase_17 AC 12 ms
5,312 KB
testcase_18 AC 13 ms
5,312 KB
testcase_19 AC 4 ms
5,308 KB
testcase_20 AC 10 ms
5,308 KB
testcase_21 AC 23 ms
5,312 KB
testcase_22 AC 17 ms
5,312 KB
testcase_23 AC 23 ms
5,312 KB
testcase_24 AC 7 ms
5,308 KB
testcase_25 AC 16 ms
5,312 KB
testcase_26 AC 15 ms
5,312 KB
testcase_27 AC 15 ms
5,312 KB
testcase_28 AC 13 ms
5,312 KB
testcase_29 AC 24 ms
5,312 KB
testcase_30 AC 5 ms
5,312 KB
testcase_31 AC 12 ms
5,312 KB
testcase_32 AC 14 ms
5,312 KB
testcase_33 AC 12 ms
5,312 KB
testcase_34 AC 14 ms
5,312 KB
testcase_35 AC 14 ms
5,312 KB
testcase_36 AC 4 ms
5,308 KB
testcase_37 AC 11 ms
5,312 KB
testcase_38 AC 14 ms
5,312 KB
testcase_39 AC 13 ms
5,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600
#define long long long // for codeforces

#define MX 100010
long dp[2][MX+5];

int main(){
  int n;
  cin>>n;
  vector<int> v(n),w(n);
  rep(i,n) cin>>v[i]>>w[i];
  int V;
  cin>>V;

  auto prev = dp[0];
  auto crnt = dp[1];

  fill(prev, prev+MX+5, INF);
  prev[0] = 0;

  rep(i,n){
    fill(crnt, crnt+MX+5, INF);
    rep(j,MX){
      crnt[j] = min(crnt[j], prev[j]);
      if(j+v[i]<MX) crnt[j+v[i]] = min(crnt[j+v[i]], prev[j] + w[i]);
    }
    swap(crnt, prev);
  }

  for(int i = MX-1; i>=0; i--) prev[i] = min(prev[i], prev[i+1]);

  int mn = prev[V];
  if(mn == 0) mn = 1;
  cout << mn << endl;
  int mx = prev[V+1];
  if(mx >= INF) cout << "inf" << endl;
  else cout << mx-1 << endl;


  return 0;
}
0