結果

問題 No.527 ナップサック容量問題
ユーザー schwarzahl
提出日時 2017-06-10 05:22:52
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,490 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 79,700 KB
実行使用メモリ 75,680 KB
最終ジャッジ日時 2024-09-23 02:36:19
合計ジャッジ時間 23,195 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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