結果

問題 No.527 ナップサック容量問題
ユーザー pekempeypekempey
提出日時 2017-06-09 22:39:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 891 bytes
コンパイル時間 698 ms
コンパイル使用メモリ 72,920 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 22:11:14
合計ジャッジ時間 3,497 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 9 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 96 ms
4,348 KB
testcase_06 AC 226 ms
4,348 KB
testcase_07 AC 36 ms
4,348 KB
testcase_08 AC 80 ms
4,348 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 103 ms
4,348 KB
testcase_11 AC 149 ms
4,348 KB
testcase_12 AC 104 ms
4,348 KB
testcase_13 AC 236 ms
4,348 KB
testcase_14 AC 18 ms
4,348 KB
testcase_15 AC 21 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 31 ms
4,348 KB
testcase_18 AC 39 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 9 ms
4,348 KB
testcase_21 AC 25 ms
4,348 KB
testcase_22 AC 17 ms
4,348 KB
testcase_23 AC 24 ms
4,348 KB
testcase_24 AC 6 ms
4,348 KB
testcase_25 AC 17 ms
4,348 KB
testcase_26 AC 15 ms
4,348 KB
testcase_27 AC 15 ms
4,348 KB
testcase_28 AC 13 ms
4,348 KB
testcase_29 AC 24 ms
4,348 KB
testcase_30 AC 23 ms
4,348 KB
testcase_31 AC 12 ms
4,348 KB
testcase_32 AC 14 ms
4,348 KB
testcase_33 AC 12 ms
4,348 KB
testcase_34 AC 14 ms
4,348 KB
testcase_35 AC 45 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 14 ms
4,348 KB
testcase_38 AC 25 ms
4,348 KB
testcase_39 AC 38 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
	int n;
	std::cin >> n;

	std::vector<int> v(n), w(n);
	for (int i = 0; i < n; i++) {
		std::cin >> v[i] >> w[i];
	}

	auto f = [&](int x) {
		std::vector<int> dp(x + 1, -1.01e9);
		dp[0] = 0;
		for (int i = 0; i < n; i++) {
			for (int j = x - w[i]; j >= 0; j--) {
				dp[j + w[i]] = std::max(dp[j + w[i]], dp[j] + v[i]);
			}
		}
		return *std::max_element(dp.begin(), dp.end());
	};
	int V;
	std::cin >> V;

	int L = 0;
	int R = 100001;
	while (R - L > 1) {
		int M = (L + R) / 2;
		if (f(M) >= V) {
			R = M;
		} else {
			L = M;
		}
	}
	std::cout << R << std::endl;

	L = R;
	R = 100002;
	while (R - L > 1) {
		int M = (L + R) / 2;
		if (f(M) > V) {
			R = M;
		} else {
			L = M;
		}
	}
	if (R - 1 > 100000) {
		std::cout << "inf" << std::endl;
	} else {
		std::cout << R - 1 << std::endl;
	}
}
0