結果

問題 No.281 門松と魔法(1)
ユーザー ぴろず
提出日時 2015-09-19 00:27:54
言語 Java
(openjdk 23)
結果
AC  
実行時間 141 ms / 1,000 ms
コード長 1,528 bytes
コンパイル時間 2,728 ms
コンパイル使用メモリ 77,400 KB
実行使用メモリ 41,876 KB
最終ジャッジ日時 2024-11-06 20:29:02
合計ジャッジ時間 11,483 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

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