結果
| 問題 |
No.527 ナップサック容量問題
|
| コンテスト | |
| ユーザー |
schwarzahl
|
| 提出日時 | 2017-06-10 05:21:12 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,296 bytes |
| コンパイル時間 | 2,257 ms |
| コンパイル使用メモリ | 79,548 KB |
| 実行使用メモリ | 73,652 KB |
| 最終ジャッジ日時 | 2024-09-23 02:34:11 |
| 合計ジャッジ時間 | 22,979 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 33 WA * 4 |
ソースコード
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 (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);
}
}
schwarzahl