結果

問題 No.527 ナップサック容量問題
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2017-06-09 23:49:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,769 bytes
コンパイル時間 1,456 ms
コンパイル使用メモリ 159,664 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-24 03:13:11
合計ジャッジ時間 4,423 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,372 KB
testcase_01 AC 3 ms
4,372 KB
testcase_02 AC 9 ms
4,372 KB
testcase_03 AC 6 ms
4,372 KB
testcase_04 AC 3 ms
4,372 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 199 ms
4,372 KB
testcase_08 WA -
testcase_09 AC 6 ms
4,372 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 105 ms
4,372 KB
testcase_15 AC 101 ms
4,372 KB
testcase_16 AC 8 ms
4,372 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 9 ms
4,372 KB
testcase_20 AC 14 ms
4,372 KB
testcase_21 AC 29 ms
4,372 KB
testcase_22 AC 19 ms
4,372 KB
testcase_23 AC 22 ms
4,372 KB
testcase_24 AC 13 ms
4,372 KB
testcase_25 AC 13 ms
4,372 KB
testcase_26 AC 15 ms
4,372 KB
testcase_27 AC 16 ms
4,372 KB
testcase_28 AC 11 ms
4,372 KB
testcase_29 AC 20 ms
4,372 KB
testcase_30 AC 16 ms
4,372 KB
testcase_31 AC 11 ms
4,372 KB
testcase_32 AC 17 ms
4,372 KB
testcase_33 AC 15 ms
4,372 KB
testcase_34 AC 13 ms
4,372 KB
testcase_35 WA -
testcase_36 AC 5 ms
4,372 KB
testcase_37 AC 46 ms
4,372 KB
testcase_38 AC 135 ms
4,372 KB
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=n-1;i>=(0);i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int N;
int mi, ma;
int v[110], w[110];
int V;
int dp[100010];

int solve(int W){
	rep(i, 100010) dp[i] = 0;
	for (int i = 0; i < N; ++i){
		for (int j = W; j >= w[i]; --j){
			chmax(dp[j], dp[j - w[i]] + v[i]);
		}
	}
	int ret = 0;
	rep(i, W + 1) chmax(ret, dp[i]);
	return ret;
}

int binary(int target){
	// dp[100001] = INF;
	int l = 1, r = 100001;
	while(r - l > 2	){
		int m = (l + r) / 2;
		// printf("%d %d %d\n", l, r, m);
		auto ret = solve(m);
		// printf("ret %d\n", ret);
		if(ret == target) r = m + 1;
		else if(ret < target) l = m;
		else r = m;
	}
	// printf("lr %d %d\n", l, r);
	reps(i, max(1, l - 100), min(r + 100, 10000) + 1){
		// printf("i %d %d\n", i, solve(i));
		if(solve(i) == target) return i;
		else if(solve(i) > target) return i;
	}
	return 100001;
}

int main(void){
	cin >> N;
	rep(i, N) cin >> v[i] >> w[i];

	cin >> V;
	
	// printf("\n");
	// reps(i, 1, 11) printf("%d\n", solve(i));
	// printf("\n");
	int ansl = binary(V);
	int ansr = binary(V + 1)	;
	printf("%d\n", ansl);	
	if(ansr == 100001) printf("inf\n");
	else printf("%d\n", ansr - 1);
	return 0;
}
0