結果

問題 No.527 ナップサック容量問題
ユーザー uzumakiyorumunnuzumakiyorumunn
提出日時 2017-06-09 23:42:49
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,251 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 58,128 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-10-24 02:28:44
合計ジャッジ時間 4,069 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,696 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>

using namespace std;

#define NMAX 100

int N;
int v[NMAX],w[NMAX];
int V;
int minw,maxw;
int minw2,maxw2;
int minv,maxv;
void nap1(int i,int vsum,int wsum){
	if(i==N){
		if(V==vsum){
			minw=min(minw,wsum);
			maxw=max(maxw,wsum);
		}
	}
	for(int j=i;j<N;j++){
		nap1(j+1,vsum+v[j],wsum+w[i]);
		nap1(j+1,vsum,wsum);
	}
}

void nap2(int i,int vsum,int wsum){
	if(i==N){
		if(V!=vsum){
			if(maxw<=vsum){
				minw2=min(minw2,wsum);
				maxv=max(maxv,vsum);
			}
			if(minw>=vsum){
				maxw2=max(maxw2,wsum);
				minv=max(minv,vsum);
			}
		}
	}
	for(int j=i;j<N;j++){
		nap2(j+1,vsum+v[j],wsum+w[i]);
		nap2(j+1,vsum,wsum);
	}
}


int main(){

	cin>>N;
	int ansi,ansx;
	int minn=1000*100000,sumn=0;
	int minm=1000*100000,summ=0;
	for(int i=0;i<N;i++){
		cin>>v[i];
		cin>>w[i];
		if(minn>v[i]) minn=v[i];
		sumn+=v[i];
		if(minm>w[i]) minm=w[i];
		summ+=w[i];
	}
	cin>>V;
	minw=1000*100000,maxw=-1;
	nap1(0,0,0);
	minw2=1000*100000,maxw2=-1;
	minv=-1,maxv=-1;
	nap2(0,0,0);

	
	if(V>minv) ansi=minw2;
	else ansi=minw2+1;
	if(minn>V) ansi=1;
	if(sumn==V) ansi=summ;
	if(V>maxv) ansx=maxw2;
	else ansx=maxw2-1;
	if(minn>V) ansx=minm-1;

	cout<<ansi<<endl;
	if(sumn!=V) cout<<ansx<<endl;
	else cout<<"inf"<<endl;
}
0