結果

問題 No.527 ナップサック容量問題
ユーザー どららどらら
提出日時 2017-06-10 06:31:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,472 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 168,972 KB
実行使用メモリ 75,500 KB
最終ジャッジ日時 2023-10-24 20:21:40
合計ジャッジ時間 6,475 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
75,500 KB
testcase_01 AC 95 ms
23,280 KB
testcase_02 AC 875 ms
52,176 KB
testcase_03 AC 95 ms
23,252 KB
testcase_04 AC 47 ms
23,252 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

#define INF 10000000

int N;
int v[105];
int w[105];
int V;

bool C(int W){
  vector<int> dp(W+1, -1);
  dp[0] = 0;
  rep(i,N){
    for(int j=W; j>=0; j--){
      if(dp[j]>=0 && j+w[i]<=W){
        dp[j+w[i]] = max(dp[j+w[i]], dp[j] + v[i]);
      }
    }
  }
  int ret = 0;
  rep(i,dp.size()){
    ret = max(ret, dp[i]);
  }
  return ret < V;
}
bool C2(int W){
  vector<int> dp(W+1, -1);
  dp[0] = 0;
  rep(i,N){
    for(int j=W; j>=0; j--){
      if(dp[j]>=0 && j+w[i]<=W){
        dp[j+w[i]] = max(dp[j+w[i]], dp[j] + v[i]);
      }
    }
  }
  int ret = 0;
  rep(i,dp.size()){
    ret = max(ret, dp[i]);
  }
  return ret == V;
}

int main(){
  cin >> N;
  rep(i,N){
    cin >> v[i] >> w[i];
  }
  cin >> V;

  int ret_min, ret_max;
  {
    int lb = 0, ub = INF;
    while(ub-lb>1){
      int m = (lb+ub)/2;
      if(C(m)) lb = m;
      else ub = m;
    }
    ret_min = ub;
  }
  {
    int lb = ret_min, ub = INF;
    while(ub-lb>1){
      int m = (lb+ub)/2;
      if(C2(m)) lb = m;
      else ub = m;
    }
    ret_max = lb;
  }

  cout << ret_min << endl;
  if(ret_max > 100000){
    cout << "inf" << endl;
  }else{
    cout << ret_max << endl;
  }
  
  return 0;
}
0