結果

問題 No.527 ナップサック容量問題
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2017-06-10 00:01:30
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,730 bytes
コンパイル時間 1,760 ms
コンパイル使用メモリ 159,772 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-24 03:29:39
合計ジャッジ時間 3,570 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 6 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 13 ms
4,348 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 11 ms
4,348 KB
testcase_29 AC 21 ms
4,348 KB
testcase_30 AC 15 ms
4,348 KB
testcase_31 AC 11 ms
4,348 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 13 ms
4,348 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
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) l = m;
		else r = m;
	}
	// printf("lr %d %d\n", l, r);
	reps(i, max(1, l - 100), min(r + 1000, 100) + 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