結果

問題 No.527 ナップサック容量問題
ユーザー vjudge1
提出日時 2025-10-19 22:05:14
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 944 bytes
コンパイル時間 8,078 ms
コンパイル使用メモリ 208,984 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-10-19 22:05:24
合計ジャッジ時間 6,430 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 3
other WA * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0