結果

問題 No.281 門松と魔法(1)
ユーザー uafr_csuafr_cs
提出日時 2015-12-04 02:51:35
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,601 bytes
コンパイル時間 2,410 ms
コンパイル使用メモリ 79,092 KB
実行使用メモリ 41,768 KB
最終ジャッジ日時 2024-04-24 11:58:59
合計ジャッジ時間 11,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
41,320 KB
testcase_01 AC 131 ms
41,544 KB
testcase_02 AC 134 ms
41,136 KB
testcase_03 AC 131 ms
41,764 KB
testcase_04 AC 131 ms
41,188 KB
testcase_05 AC 133 ms
41,200 KB
testcase_06 AC 132 ms
41,768 KB
testcase_07 AC 117 ms
39,988 KB
testcase_08 AC 132 ms
41,292 KB
testcase_09 AC 130 ms
41,188 KB
testcase_10 AC 132 ms
41,048 KB
testcase_11 AC 130 ms
41,368 KB
testcase_12 AC 132 ms
41,324 KB
testcase_13 AC 129 ms
41,452 KB
testcase_14 AC 131 ms
41,528 KB
testcase_15 AC 117 ms
39,988 KB
testcase_16 AC 130 ms
41,288 KB
testcase_17 AC 132 ms
41,248 KB
testcase_18 AC 130 ms
41,188 KB
testcase_19 AC 132 ms
41,464 KB
testcase_20 AC 130 ms
41,124 KB
testcase_21 AC 131 ms
41,676 KB
testcase_22 AC 120 ms
39,992 KB
testcase_23 AC 129 ms
41,332 KB
testcase_24 AC 130 ms
41,192 KB
testcase_25 AC 132 ms
41,160 KB
testcase_26 AC 132 ms
41,556 KB
testcase_27 AC 132 ms
41,532 KB
testcase_28 AC 129 ms
41,428 KB
testcase_29 AC 129 ms
41,340 KB
testcase_30 AC 131 ms
41,176 KB
testcase_31 AC 129 ms
41,288 KB
testcase_32 AC 131 ms
41,372 KB
testcase_33 AC 130 ms
41,244 KB
testcase_34 AC 130 ms
41,464 KB
testcase_35 AC 117 ms
39,988 KB
testcase_36 AC 128 ms
41,432 KB
testcase_37 AC 116 ms
40,048 KB
testcase_38 WA -
testcase_39 AC 133 ms
41,640 KB
testcase_40 AC 119 ms
41,024 KB
testcase_41 AC 129 ms
41,456 KB
testcase_42 AC 130 ms
41,520 KB
testcase_43 AC 130 ms
41,704 KB
testcase_44 AC 130 ms
41,580 KB
testcase_45 AC 129 ms
41,168 KB
testcase_46 AC 118 ms
40,060 KB
testcase_47 AC 130 ms
41,156 KB
testcase_48 AC 133 ms
41,136 KB
testcase_49 AC 133 ms
41,336 KB
testcase_50 AC 130 ms
41,280 KB
testcase_51 AC 132 ms
41,160 KB
testcase_52 AC 130 ms
41,408 KB
testcase_53 AC 118 ms
39,876 KB
testcase_54 AC 131 ms
41,560 KB
testcase_55 AC 130 ms
41,124 KB
testcase_56 AC 129 ms
41,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static boolean is_valid(final long h1, final long h2, final long h3){
		return ((h1 < h2 && h2 > h3) || (h1 > h2 && h2 < h3)) && (h1 != h3);
	}
	
	public static long d(final long h, final long d){
		return Math.max(0, h - d);
	}
	
	public static long d(final long h_max, final long h_min, final long d){
		return h_max < h_min ? 0 : (h_max - h_min) / d + 1;
	}
	
	public static long all_same(final long h, final long d){
		return h > d ? 3 : -1;
	}
	
	public static long two_same(final long h1, final long h2, final long h3, final long d){
		if(h1 == h2 && h2  < h3){ return is_valid(h1, d(h2, d), h3) ? 1 : -1; }
		if(h1  > h2 && h2 == h3){ return is_valid(h1, d(h2, d), h3) ? 1 : -1; }
		
		if(h1 == h2 && h2  > h3){
			if(is_valid(d(h1, d), h2, h3)){ // d(h1) < h2 && h2 > h3
				return 1;
			}
			
			final long h2_count = d(h2, h3, d);
			final long d_h2 = h2 - d * h2_count;
			if(is_valid(h1, d_h2, h3)){ return h2_count; } // h1 > d(h2) && d(h2) < h3
			else{ return -1; }
		}
		
		if(h1 < h2 && h2 == h3){
			if(is_valid(h1, h2, d(h3, d))){
				return 1;
			}
			
			final long h2_count = d(h2, h1, d);
			final long d_h2 = Math.max(0, h2 - d * h2_count);
			if(is_valid(h1, d_h2, h3)){ return h2_count; }
			else { return -1; }
		}
		
		if(h1 == h3 && h1 < h2){ return is_valid(d(h1, d), h2, h3) ? 1 : -1; }
		if(h1 == h3 && h1 > h2){ 
			final long h1_count = d(h1, h2, d);
			final long d_h1 = Math.max(0, h1 - d * h1_count);
			
			if(d_h1 == 0){ return -1; }
			return h1_count * 2 + 1;
		}
		
		return -1;
	}
	
	public static long all_diff(final long h1, final long h2, final long h3, final long d){
		if(h1 > h2 && h2 > h3){
			final long h1_count = d(h1, h2, d);
			final long d_h1 = Math.max(0, h1 - d * h1_count);
			
			final long h2_count = d(h2, h3, d);
			final long d_h2 = Math.max(0, h2 - d * h2_count);
			
			if(is_valid(d_h1, h2, h3) && is_valid(h1, d_h2, h3)){
				return Math.min(h1_count, h2_count);
			}else if(is_valid(d_h1, h2, h3)){
				return h1_count;
			}else if(is_valid(h1, d_h2, h3)){
				return h2_count;
			}else{
				return Math.min(two_same(d_h1, h2, h3, d), two_same(h1, d_h2, h3, d));
			}
		}
		
		if(h1 < h2 && h2 < h3){
			final long h3_count = d(h3, h2, d);
			final long d_h3 = Math.max(0, h3 - d * h3_count);
			
			final long h2_count = d(h2, h1, d);
			final long d_h2 = Math.max(0, h2 - d * h2_count);
			
			if(is_valid(h1, h2, d_h3) && is_valid(h1, d_h2, h3)){
				return Math.min(h3_count, h2_count);
			}else if(is_valid(h1, h2, d_h3)){
				return h3_count;
			}else if(is_valid(h1, d_h2, h3)){
				return h2_count;
			}else{
				return Math.min(two_same(h1, h2, d_h3, d), two_same(h1, d_h2, h3, d));
			}
		}
		
		return -1;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final long d = sc.nextLong();
		long h1 = sc.nextLong();
		long h2 = sc.nextLong();
		long h3 = sc.nextLong();
		
		if(is_valid(h1, h2, h3)){
			System.out.println(0);
			return;
		}else if(d == 0){
			System.out.println(-1);
			return;
		}
		
		final long max = Math.max(h1, Math.max(h2, h3));
		final long min = Math.min(h1, Math.min(h2, h3));
		final long med = (h1 + h2 + h3) - (max + min);
		//System.out.println(max + " " + min + " " + med);
		
		if(max == min){
			System.out.println(all_same(max, d));
		}else if(max == med || min == med){
			System.out.println(two_same(h1, h2, h3, d));
		}else{
			System.out.println(all_diff(h1, h2, h3, d));
		}
		
	}

}
0