結果
問題 | No.527 ナップサック容量問題 |
ユーザー |
![]() |
提出日時 | 2018-03-02 18:47:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 765 bytes |
コンパイル時間 | 805 ms |
コンパイル使用メモリ | 69,040 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-11 19:18:04 |
合計ジャッジ時間 | 1,832 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 15 WA * 22 |
ソースコード
#include<iostream> #include<algorithm> #include<utility> using namespace std; int v[110], w[110]; int dp[110][1010]; pair<int, int> solve(int n, int V, pair<int, int>P) { P = make_pair(1000000, 0); for (int i = 1; i <= n; i++) { for (int j = 1; j <= 1001; j++) { if (j < w[i]) dp[i][j] = dp[i - 1][j]; else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); if (dp[i][j] == V) { P.first = min(P.first, j); P.second = max(P.second, j); } } } return P; } int main() { int n, V; pair<int, int> P; cin >> n; for (int i = 1; i <= n; i++) cin >> v[i] >> w[i]; cin >> V; cout << solve(n, V, P).first << endl; if(solve(n, V, P).second != 1001) cout << solve(n, V, P).second << endl; else cout << "inf" << endl; }