結果

問題 No.527 ナップサック容量問題
ユーザー schwarzahl
提出日時 2017-06-10 04:54:26
言語 Java
(openjdk 23)
結果
AC  
実行時間 743 ms / 2,000 ms
コード長 2,225 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 81,680 KB
実行使用メモリ 61,580 KB
最終ジャッジ日時 2024-09-22 23:03:48
合計ジャッジ時間 14,145 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

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

	private void solveA() {
		Scanner sc = new Scanner(System.in);
		String line = sc.nextLine();
		int h = c2i(line.charAt(0)) * 10 + c2i(line.charAt(1));
		int m = c2i(line.charAt(3)) * 10 + c2i(line.charAt(4));
		m += 5;
		if (m >= 60) {
			m -= 60;
			h += 1;
			h = h % 24;
		}
		System.out.printf("%02d:%02d", h, m);
	}

	private int c2i(char c) {
		return c - '0';
	}

	private void solveB() {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long M = sc.nextLong();

		long pp = 0;
		long p = 1;
		for (int n = 3; n <= N; n++) {
			long now = (pp + p) % M;
			pp = p;
			p = now;
		}
		System.out.println(p);
	}

	private void solveC() {
		Scanner sc = new Scanner(System.in);
		final int MAX_W = 100001;

		int N = sc.nextInt();
		List<Budget> list = new ArrayList<>();
		for (int i = 0; i < N; i++) {
			Budget b = new Budget();
			b.v = sc.nextInt();
			b.w = sc.nextInt();
			list.add(b);
		}
		int V = sc.nextInt();

		int[] values = new int[MAX_W];

		Set<Integer> set = new HashSet<>();
		set.add(0);
		for (Budget b : list) {
			int[] nextValues = Arrays.copyOf(values, MAX_W);
			Set<Integer> next_set = new HashSet<>();
			for (int weight : set) {
				int tmp_v = values[weight];
				int next_v = tmp_v + b.v;
				int next_w = weight + b.w;
				if (next_v > nextValues[next_w]) {
					nextValues[next_w] = next_v;
					if (next_v <= V) {
						next_set.add(next_w);
					}
				}
				next_set.add(weight);
			}
			set = next_set;
			values = nextValues;
		}
		int min = 1;
		Integer max = null;
		for (int wi = 0; wi < 100001; wi++) {
			if (min == 1 && V > 0 && values[wi] == V) {
				min = wi;
			}
			if (max == null && values[wi] > V) {
				max = wi;
			}
		}
		System.out.println(min);
		System.out.println(max == null ? "inf" : max - 1);
	}

	class Budget {
		int v;
		int w;

		public int getValue() {
			return v;
		}

		public int getWeight() {
			return w;
		}
	}
}
0