結果

問題 No.527 ナップサック容量問題
ユーザー masamasa
提出日時 2017-06-10 00:05:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,093 ms / 2,000 ms
コード長 1,227 bytes
コンパイル時間 1,310 ms
コンパイル使用メモリ 75,668 KB
実行使用メモリ 49,460 KB
最終ジャッジ日時 2023-10-24 03:32:06
合計ジャッジ時間 8,383 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,388 KB
testcase_01 AC 5 ms
4,652 KB
testcase_02 AC 19 ms
4,912 KB
testcase_03 AC 6 ms
4,652 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 405 ms
24,716 KB
testcase_06 AC 1,008 ms
48,132 KB
testcase_07 AC 143 ms
21,284 KB
testcase_08 AC 336 ms
21,548 KB
testcase_09 AC 13 ms
4,520 KB
testcase_10 AC 385 ms
26,036 KB
testcase_11 AC 636 ms
35,116 KB
testcase_12 AC 442 ms
26,300 KB
testcase_13 AC 1,093 ms
49,460 KB
testcase_14 AC 67 ms
11,780 KB
testcase_15 AC 89 ms
16,668 KB
testcase_16 AC 5 ms
4,652 KB
testcase_17 AC 114 ms
14,292 KB
testcase_18 AC 152 ms
15,876 KB
testcase_19 AC 7 ms
4,916 KB
testcase_20 AC 39 ms
12,308 KB
testcase_21 AC 128 ms
29,604 KB
testcase_22 AC 80 ms
20,756 KB
testcase_23 AC 120 ms
28,940 KB
testcase_24 AC 22 ms
8,612 KB
testcase_25 AC 76 ms
20,364 KB
testcase_26 AC 67 ms
18,516 KB
testcase_27 AC 69 ms
18,780 KB
testcase_28 AC 61 ms
16,668 KB
testcase_29 AC 125 ms
29,468 KB
testcase_30 AC 75 ms
8,632 KB
testcase_31 AC 45 ms
14,556 KB
testcase_32 AC 56 ms
17,196 KB
testcase_33 AC 46 ms
15,084 KB
testcase_34 AC 58 ms
17,724 KB
testcase_35 AC 173 ms
17,724 KB
testcase_36 AC 5 ms
4,388 KB
testcase_37 AC 58 ms
14,028 KB
testcase_38 AC 94 ms
16,932 KB
testcase_39 AC 143 ms
15,876 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

int n, v;
vector<int> value, weight;

int calc(int w_max) {
	vector<vector<int>> dp(n, vector<int>(w_max + 1001, -1));

	dp[0][0] = 0;
	dp[0][weight[0]] = value[0];
	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j <= w_max; j++) {
			if (dp[i][j] < 0) {
				continue;
			}
			dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			int nj = j + weight[i+1];
			dp[i+1][nj] = max(dp[i+1][nj], dp[i][j] + value[i+1]);
		}
	}

	int ret = 0;
	for (int i = 0; i <= w_max; i++) {
		ret = max(ret, dp[n - 1][i]);
	}

	return ret;
}

int main() {
	cin >> n;

	value.assign(n, 0);
	weight.assign(n, 0);
	for (int i = 0; i < n; i++) {
		cin >> value[i] >> weight[i];
	}
	cin >> v;

	int n_bit = 17;
	int maxi = 0;
	int mini = (1 << n_bit) - 1;
	for (int i = n_bit - 1; i >= 0; i--) {
		int tmp1 = maxi + (1 << i);
		int tmp2 = mini - (1 << i);
		int x = calc(tmp1);
		int y = calc(tmp2);
		if (x <= v) {
			maxi = tmp1;
		}
		if (y >= v) {
			mini = tmp2;
		}
	}

	mini = max(mini, 1);

	cout << mini << endl;
	if (maxi > 100000) {
		cout << "inf" << endl;
	} else {
		cout << maxi << endl;
	}
	return 0;
}
0