結果

問題 No.158 奇妙なお使い
ユーザー htensai
提出日時 2020-05-14 15:56:18
言語 Java
(openjdk 23)
結果
AC  
実行時間 390 ms / 5,000 ms
コード長 1,474 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 79,296 KB
実行使用メモリ 57,880 KB
最終ジャッジ日時 2024-09-15 08:20:15
合計ジャッジ時間 7,748 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int[] aArr = new int[4];
		int[] bArr = new int[4];
		for (int i = 0; i < 4; i++) {
		    aArr[i] = sc.nextInt();
		}
		for (int i = 0; i < 4; i++) {
		    bArr[i] = sc.nextInt();
		}
		HashMap<Integer, Integer> dp = new HashMap<>();
		dp.put(a * 1000 + b, c);
		int count = -1;
		while (dp.size() > 0) {
		    HashMap<Integer, Integer> next = new HashMap<>();
		    for (Map.Entry<Integer, Integer> entry : dp.entrySet()) {
		        int x = entry.getKey() / 1000;
		        int y = entry.getKey() % 1000;
		        int z = entry.getValue();
		        setDP(x, y, z, aArr, next);
		        setDP(x, y, z, bArr, next);
		    }
		    dp = next;
		    count++;
		}
		System.out.println(count);
	}
	
	static boolean setDP(int a, int b, int c, int[] arr, HashMap<Integer, Integer> dp) {
	    if (a * 1000 + b * 100 + c < arr[0]) {
	        return false;
	    }
	    int x = arr[0];
	    while (x >= 1000 && a > 0) {
	        x -= 1000;
	        a--;
	    }
	    while (x >= 100 && b > 0) {
	        x -= 100;
	        b--;
	    }
	    c -= x;
	    if (c < 0) {
	        return false;
	    }
	    a += arr[1];
	    b += arr[2];
	    c += arr[3];
	    int key = a * 1000 + b;
	    if (!dp.containsKey(key) || dp.get(key) < c) {
	        dp.put(key, c);
	    }
	    return true;
	}
}
0