結果

問題 No.527 ナップサック容量問題
ユーザー hotpepsihotpepsi
提出日時 2017-06-13 00:18:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 1,169 bytes
コンパイル時間 470 ms
コンパイル使用メモリ 60,084 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 21:40:07
合計ジャッジ時間 2,169 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 17 ms
4,348 KB
testcase_06 AC 71 ms
4,348 KB
testcase_07 AC 25 ms
4,348 KB
testcase_08 AC 12 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 84 ms
4,348 KB
testcase_11 AC 27 ms
4,348 KB
testcase_12 AC 19 ms
4,348 KB
testcase_13 AC 75 ms
4,348 KB
testcase_14 AC 14 ms
4,348 KB
testcase_15 AC 16 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 24 ms
4,348 KB
testcase_18 AC 30 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 14 ms
4,348 KB
testcase_22 AC 9 ms
4,348 KB
testcase_23 AC 10 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 7 ms
4,348 KB
testcase_26 AC 7 ms
4,348 KB
testcase_27 AC 7 ms
4,348 KB
testcase_28 AC 6 ms
4,348 KB
testcase_29 AC 13 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 37 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 9 ms
4,348 KB
testcase_38 AC 16 ms
4,348 KB
testcase_39 AC 29 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int n, v[100], w[100];

int get_max_value(int max_weight) {
	int dp[100001];
	memset(dp, -1, sizeof(dp));
	dp[0] = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = max_weight; j >= w[i]; --j) {
			int k = dp[j - w[i]];
			if (k >= 0) {
				dp[j] = max(dp[j], k + v[i]);
			}
		}
	}
	return *max_element(dp, dp + max_weight + 1);
}

int main(int argc, char *argv[]) {
	int total_weight = 0, total_value = 0, max_value;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> v[i] >> w[i];
		total_value += v[i];
		total_weight += w[i];
	}
	cin >> max_value;
	int low = 0, high = total_weight;
	while (high - low > 1) {
		int med = (high + low) / 2;
		if (get_max_value(med) >= max_value) {
			high = med;
		} else {
			low = med;
		}
	}
	cout << high << endl;
	if (max_value == total_value) {
		cout << "inf" << endl;
	} else {
		low = high;
		high = total_weight;
		while (high - low > 1) {
			int med = (high + low) / 2;
			if (get_max_value(med) > max_value) {
				high = med;
			} else {
				low = med;
			}
		}
		cout << low << endl;
	}
	return 0;
}
0