結果
| 問題 | No.527 ナップサック容量問題 |
| コンテスト | |
| ユーザー |
peroon
|
| 提出日時 | 2019-04-25 16:43:46 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 161 ms / 2,000 ms |
| コード長 | 1,430 bytes |
| 記録 | |
| コンパイル時間 | 1,960 ms |
| コンパイル使用メモリ | 173,648 KB |
| 実行使用メモリ | 82,432 KB |
| 最終ジャッジ日時 | 2024-11-20 15:39:22 |
| 合計ジャッジ時間 | 5,649 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll N;
vector<ll> V;
vector<ll> W;
const int W_MAX = 100010;
ll memo[110][W_MAX];
// i : i番目を見ている
// j : 残り容量
ll rec(ll i, ll j){
if(memo[i][j]!=0){
return memo[i][j];
}
ll res;
if(i==N){
res = 0;
}
// 入れられない
else if(W[i]>j){
res = rec(i+1, j);
}
else{
ll ma0 = rec(i+1, j-W[i]) + V[i]; // いれる
ll ma1 = rec(i+1, j); // いれない
res = max(ma0, ma1);
}
return memo[i][j] = res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
// input
cin >> N;
V.resize(N);
W.resize(N);
FOR(i, 0, N){
cin >> V[i] >> W[i];
}
ll V;
cin >> V;
vector<ll> A;
FOR(w, 0, W_MAX){
if(rec(0, w)==V){
A.push_back(w);
}
}
sort(ALL(A));
ll mi = A[0];
ll ma = A.back();
if(mi==0){
mi = 1;
}
p(mi);
if(ma>100000){
p("inf");
}else{
p(ma);
}
return 0;
}
peroon