結果
問題 |
No.527 ナップサック容量問題
|
ユーザー |
|
提出日時 | 2020-05-16 04:39:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 11 ms / 2,000 ms |
コード長 | 573 bytes |
コンパイル時間 | 2,785 ms |
コンパイル使用メモリ | 191,816 KB |
最終ジャッジ日時 | 2025-01-10 12:19:42 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:11:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 11 | scanf("%d",&n); | ~~~~~^~~~~~~~~ main.cpp:12:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 12 | rep(i,n) scanf("%d%d",&v[i],&w[i]); | ~~~~~^~~~~~~~~~~~~~~~~~~~ main.cpp:13:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 13 | scanf("%d",&V); | ~~~~~^~~~~~~~~
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int INF=1<<29; int main(){ int n,v[1000],w[1000],V; scanf("%d",&n); rep(i,n) scanf("%d%d",&v[i],&w[i]); scanf("%d",&V); int dp[100001]; rep(a,1e5+1) dp[a]=-INF; dp[0]=0; rep(i,n){ for(int a=1e5;a>=w[i];a--) dp[a]=max(dp[a],dp[a-w[i]]+v[i]); } rep(l,1e5+1) if(dp[l]==V) { printf("%d\n",l>0?l:1); if(accumulate(v,v+n,0)==V){ puts("inf"); } else{ int r; for(r=l+1;r<=1e5;r++) if(dp[r]>V) break; printf("%d\n",r-1); } break; } return 0; }