結果

問題 No.527 ナップサック容量問題
ユーザー schwarzahlschwarzahl
提出日時 2017-06-10 05:22:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,514 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 4,974 ms
コンパイル使用メモリ 79,316 KB
実行使用メモリ 76,956 KB
最終ジャッジ日時 2023-10-24 10:07:21
合計ジャッジ時間 24,834 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
40,892 KB
testcase_01 AC 110 ms
39,904 KB
testcase_02 AC 114 ms
40,368 KB
testcase_03 AC 123 ms
40,816 KB
testcase_04 AC 124 ms
40,916 KB
testcase_05 AC 419 ms
58,160 KB
testcase_06 AC 1,185 ms
60,728 KB
testcase_07 AC 794 ms
59,232 KB
testcase_08 AC 374 ms
54,540 KB
testcase_09 AC 109 ms
39,888 KB
testcase_10 AC 1,118 ms
60,524 KB
testcase_11 AC 579 ms
58,524 KB
testcase_12 AC 447 ms
58,636 KB
testcase_13 AC 1,256 ms
75,780 KB
testcase_14 AC 370 ms
56,096 KB
testcase_15 AC 549 ms
57,212 KB
testcase_16 AC 126 ms
48,708 KB
testcase_17 AC 429 ms
69,920 KB
testcase_18 AC 487 ms
58,440 KB
testcase_19 AC 127 ms
57,028 KB
testcase_20 AC 348 ms
53,428 KB
testcase_21 AC 1,330 ms
75,464 KB
testcase_22 AC 801 ms
73,060 KB
testcase_23 AC 1,117 ms
73,868 KB
testcase_24 AC 223 ms
61,044 KB
testcase_25 AC 684 ms
72,928 KB
testcase_26 AC 599 ms
73,044 KB
testcase_27 AC 652 ms
72,548 KB
testcase_28 AC 521 ms
73,632 KB
testcase_29 AC 1,514 ms
76,956 KB
testcase_30 AC 128 ms
57,468 KB
testcase_31 AC 145 ms
57,720 KB
testcase_32 AC 153 ms
57,876 KB
testcase_33 AC 150 ms
57,892 KB
testcase_34 AC 152 ms
57,440 KB
testcase_35 AC 528 ms
72,324 KB
testcase_36 AC 126 ms
57,136 KB
testcase_37 AC 410 ms
71,168 KB
testcase_38 AC 469 ms
71,904 KB
testcase_39 AC 438 ms
72,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args){
		Main main = new Main();
		main.solveC();
	}

	private void solveC() {
		Scanner sc = new Scanner(System.in);

		final int N = sc.nextInt();
		TreeMap<Integer, Integer> map = new TreeMap<>();
		map.put(0, 0);
		for (int i = 0; i < N; i++) {
			final int value = sc.nextInt();
			final int weight = sc.nextInt();
			TreeMap<Integer, Integer> nextMap = new TreeMap<>(map);
			for (Map.Entry<Integer, Integer> e : map.entrySet()) {
				final int prevWeight = e.getKey();
				final int prevValue = e.getValue();
				int nextWeight = prevWeight + weight;
				int nextValue = prevValue + value;
				if (!map.containsKey(nextWeight) || nextValue > map.get(nextWeight)) {
					nextMap.put(nextWeight, nextValue);
				}
			}
			map = nextMap;
		}
		final int V = sc.nextInt();

		Integer min = null;
		Integer max = null;
		for (Map.Entry<Integer, Integer> e : map.entrySet()) {
			final int weight = e.getKey();
			final int value = e.getValue();
			if (V > 0 && min == null && value == V) {
				min = weight;
			}
			if (value > V) {
				max = weight;
				break;
			}
		}

		System.out.println(min == null ? 1 : min);
		System.out.println(max == null ? "inf" : max - 1);
	}
}
0