結果
問題 |
No.527 ナップサック容量問題
|
ユーザー |
![]() |
提出日時 | 2017-06-13 23:59:55 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,341 bytes |
コンパイル時間 | 695 ms |
コンパイル使用メモリ | 74,572 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-24 17:04:27 |
合計ジャッジ時間 | 1,872 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 11 WA * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:16:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 16 | scanf("%d %d", &v[i], &w[i]); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <iomanip> #include <stack> #include <queue> #include <vector> #include <algorithm> #include <map> #include <set> using namespace std; int main(){ int n; cin >> n; vector<int> v(n), w(n); for(int i = 0; i < n; i++){ scanf("%d %d", &v[i], &w[i]); } int mx_v; cin >> mx_v; vector<int> dp(100010, -1); bool over = false; bool ok = false; dp[0] = 0; for(int i = 0; i < n; i++) { for (int j = 100000; j >= 0; j--) { if (dp[j] >= 0) { if (j + v[i] > mx_v){ dp[j + v[i]] = dp[j] + w[i]; over = true; }else if(j + v[i] == mx_v){ dp[j + v[i]] = dp[j] + w[i]; ok = true; } else dp[j + v[i]] = dp[j] + w[i]; } } } int mx = 0; int tmp = -1; int tmp1 = -1; for(int i = 0; i <= 100000; i++){ if(i <= mx_v)mx = max(mx, dp[i]); if(i == mx_v)tmp = dp[i]; else if(tmp != -1 && tmp < dp[i]){ tmp1 = dp[i]; break; } } if(!ok && over)cout << 1 << endl << *min_element(v.begin(), v.end()) << endl; else if(ok && !over)cout << mx << endl << "inf" << endl; else cout << tmp << endl << tmp1 - 1 << endl; }