結果

問題 No.527 ナップサック容量問題
ユーザー yukudo
提出日時 2017-06-09 23:03:07
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,151 bytes
コンパイル時間 1,251 ms
コンパイル使用メモリ 159,532 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-22 16:56:58
合計ジャッジ時間 2,736 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int nextInt()’:
main.cpp:10:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |   int x; scanf("%d", &x); return x;
      |          ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
template<class T>bool chmin(T&a,const T&b){return a>b?(a=b,true):false;}
template<class T>bool chmax(T&a,const T&b){return a<b?(a=b,true):false;}

int nextInt() {
  int x; scanf("%d", &x); return x;
}

int v[128], w[128];

const int INF = 1001001001;
const int MAX_N = 100010;
int dp[MAX_N];
int rev[MAX_N];

int main2() {
  int N = nextInt();
  REP(i, N) {
    v[i] = nextInt();
    w[i] = nextInt();
  }
  int V = nextInt();
  REP(i, MAX_N) dp[i] = rev[i] = INF;
  dp[0] = 0;
  REP(i, N) {
    for (int j = MAX_N - 1; j - v[i] >= 0; j--) {
      chmin(dp[j], dp[j - v[i]] + w[i]);
    }
  }

  REP(i, MAX_N) {
    if (dp[i] < INF) rev[ dp[i] ] = i;
  }
  REP(i, MAX_N) {
    if (rev[i] == INF && i > 0) 
      rev[i] = rev[i-1];
  }
  int mi = INF, ma = -1;
  REP(i, MAX_N) {
    if (rev[i] == V) {
      chmin(mi, i);
      chmax(ma, i);
    }
  }
  cout << max(1, mi) << endl;
  if (ma >= MAX_N - 1) cout << "inf" << endl; else cout << ma << endl;
  return 0;
}

int main() {
  for (;!cin.eof();cin>>ws) main2();
  return 0;
}
0