結果

問題 No.527 ナップサック容量問題
ユーザー masamasa
提出日時 2017-06-09 23:54:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,256 bytes
コンパイル時間 3,538 ms
コンパイル使用メモリ 75,048 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-24 03:23:32
合計ジャッジ時間 3,614 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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>(2001, -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 = 10;
	int maxi = (1 << n_bit) - 1;
	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 - 1) {
			mini = tmp2;
		}
	}

	mini = max(mini, 1);
	maxi--;

	cout << mini << endl;
	if (maxi == (1 << n_bit) - 2) {
		cout << "inf" << endl;
	} else {
		cout << maxi << endl;
	}
	return 0;
}
0