結果
| 問題 | No.527 ナップサック容量問題 |
| コンテスト | |
| ユーザー |
vjudge1
|
| 提出日時 | 2025-10-19 22:05:14 |
| 言語 | C++17(gcc12) (gcc 12.4.0 + boost 1.90.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 944 bytes |
| 記録 | |
| コンパイル時間 | 1,295 ms |
| コンパイル使用メモリ | 200,376 KB |
| 実行使用メモリ | 5,888 KB |
| 最終ジャッジ日時 | 2026-07-15 19:23:04 |
| 合計ジャッジ時間 | 3,157 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 3 |
| other | WA * 37 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:19:16: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
19 | freopen("pack.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
main.cpp:20:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
20 | freopen("pack.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int maxn=105,maxm=1e5+5;
int n,m,V;
int v[maxn],w[maxn],dp[maxn][maxm];
bool check(int type){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=dp[i-1][j];
if(j>=w[i]){
dp[i][j]=max(dp[i][j],dp[i-1][j-w[i]]+v[i]);
}
}
}
if(type==1) return dp[n][m]>=V;
return dp[n][m]<=V;
}
int main(){
freopen("pack.in","r",stdin);
freopen("pack.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
cin>>V;
int l=1,r=1e5,ans=1;
while(l<=r){
m=(l+r)>>1;
if(check(1)){
ans=m;
r=m-1;
}else l=m+1;
}
cout<<ans<<endl;
l=1,r=1e5;
while(l<=r){
m=(l+r)>>1;
if(check(2)){
ans=m;
l=m+1;
}else r=m-1;
}
if(ans==100000) puts("inf");
else cout<<ans<<endl;
return 0;
}
vjudge1