結果

問題 No.281 門松と魔法(1)
ユーザー ぴろずぴろず
提出日時 2015-09-19 00:27:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 129 ms / 1,000 ms
コード長 1,528 bytes
コンパイル時間 1,930 ms
コンパイル使用メモリ 77,732 KB
実行使用メモリ 41,356 KB
最終ジャッジ日時 2024-04-24 12:35:33
合計ジャッジ時間 9,525 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
41,248 KB
testcase_01 AC 118 ms
41,140 KB
testcase_02 AC 102 ms
40,024 KB
testcase_03 AC 100 ms
40,212 KB
testcase_04 AC 110 ms
41,100 KB
testcase_05 AC 101 ms
40,176 KB
testcase_06 AC 109 ms
40,048 KB
testcase_07 AC 110 ms
41,136 KB
testcase_08 AC 112 ms
41,092 KB
testcase_09 AC 116 ms
40,780 KB
testcase_10 AC 109 ms
40,928 KB
testcase_11 AC 112 ms
40,996 KB
testcase_12 AC 106 ms
39,900 KB
testcase_13 AC 100 ms
39,980 KB
testcase_14 AC 111 ms
41,076 KB
testcase_15 AC 94 ms
39,968 KB
testcase_16 AC 111 ms
40,968 KB
testcase_17 AC 116 ms
41,232 KB
testcase_18 AC 112 ms
41,292 KB
testcase_19 AC 117 ms
40,204 KB
testcase_20 AC 115 ms
41,060 KB
testcase_21 AC 119 ms
40,016 KB
testcase_22 AC 115 ms
40,772 KB
testcase_23 AC 111 ms
39,944 KB
testcase_24 AC 111 ms
40,060 KB
testcase_25 AC 129 ms
40,976 KB
testcase_26 AC 121 ms
41,128 KB
testcase_27 AC 110 ms
41,048 KB
testcase_28 AC 99 ms
39,944 KB
testcase_29 AC 111 ms
41,168 KB
testcase_30 AC 111 ms
41,216 KB
testcase_31 AC 102 ms
40,048 KB
testcase_32 AC 107 ms
40,740 KB
testcase_33 AC 116 ms
40,972 KB
testcase_34 AC 113 ms
41,184 KB
testcase_35 AC 111 ms
40,972 KB
testcase_36 AC 100 ms
39,724 KB
testcase_37 AC 102 ms
40,224 KB
testcase_38 AC 110 ms
41,280 KB
testcase_39 AC 99 ms
39,728 KB
testcase_40 AC 103 ms
39,832 KB
testcase_41 AC 118 ms
41,084 KB
testcase_42 AC 108 ms
40,480 KB
testcase_43 AC 113 ms
40,868 KB
testcase_44 AC 109 ms
41,132 KB
testcase_45 AC 100 ms
39,972 KB
testcase_46 AC 109 ms
40,988 KB
testcase_47 AC 99 ms
39,916 KB
testcase_48 AC 106 ms
41,008 KB
testcase_49 AC 120 ms
41,356 KB
testcase_50 AC 115 ms
41,080 KB
testcase_51 AC 103 ms
39,832 KB
testcase_52 AC 109 ms
40,888 KB
testcase_53 AC 115 ms
41,124 KB
testcase_54 AC 102 ms
39,940 KB
testcase_55 AC 111 ms
40,732 KB
testcase_56 AC 109 ms
40,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no280;

import java.util.Scanner;

public class Main {

	public static long INF = 1L << 60L;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int d = sc.nextInt();
		int[] h = new int[3];
		for(int i=0;i<3;i++) {
			h[i] = sc.nextInt();
		}

		if (d == 0) {
			if (h[0] != h[1] && h[1] != h[2] && h[2] != h[0] && (h[1] > h[0] && h[1] > h[2]) || (h[1] < h[0] && h[1] < h[2])) {
				System.out.println(0);
			}else{
				System.out.println(-1);
			}
			return;
		}


		int[] p = new int[3];
		for(int i=0;i<3;i++) {
			p[i] = i;
		}

		long ans = INF;
		do {
			if (p[0] != 1 && p[2] != 1) {
				continue;
			}
			long count = 0;
			long x = h[p[0]];
			for(int i=1;i<3;i++) {
				if (x == 0) {
					count = INF;
					break;
				}
				if (h[p[i]] >= x) {
					long magic = (h[p[i]] - x) / d + 1;
					count += magic;
					x = Math.max(0, h[p[i]] - magic * d);
				}else{
					x = h[p[i]];
				}
			}
//			System.out.println(Arrays.toString(p) + ":" + count);
			ans = Math.min(ans,count);
		} while(nextPermutation(p));

		if (ans >= INF) {
			System.out.println(-1);
		}else{
			System.out.println(ans);
		}
	}

	static boolean nextPermutation(int[] p) {
		for(int a=p.length-2;a>=0;--a) {
			if(p[a]<p[a+1]) {
				for(int b=p.length-1;;--b) {
					if(p[b]>p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for(++a, b=p.length-1;a<b;++a,--b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
				}
			}
		}
		return false;
	}

}
0