結果

問題 No.602 隠されていたゲーム2
ユーザー 37zigen37zigen
提出日時 2017-12-03 01:17:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 851 ms / 2,000 ms
コード長 2,288 bytes
コンパイル時間 3,013 ms
コンパイル使用メモリ 76,704 KB
実行使用メモリ 72,772 KB
最終ジャッジ日時 2023-08-22 05:38:51
合計ジャッジ時間 11,882 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,540 KB
testcase_01 AC 126 ms
56,088 KB
testcase_02 AC 127 ms
55,660 KB
testcase_03 AC 130 ms
55,660 KB
testcase_04 AC 127 ms
55,744 KB
testcase_05 AC 127 ms
55,996 KB
testcase_06 AC 127 ms
55,940 KB
testcase_07 AC 125 ms
55,416 KB
testcase_08 AC 126 ms
55,664 KB
testcase_09 AC 126 ms
55,816 KB
testcase_10 AC 127 ms
55,364 KB
testcase_11 AC 127 ms
55,956 KB
testcase_12 AC 128 ms
57,460 KB
testcase_13 AC 130 ms
55,596 KB
testcase_14 AC 130 ms
55,652 KB
testcase_15 AC 128 ms
55,792 KB
testcase_16 AC 352 ms
60,720 KB
testcase_17 AC 440 ms
60,668 KB
testcase_18 AC 667 ms
65,204 KB
testcase_19 AC 750 ms
70,468 KB
testcase_20 AC 715 ms
72,772 KB
testcase_21 AC 663 ms
71,396 KB
testcase_22 AC 851 ms
71,636 KB
testcase_23 AC 665 ms
71,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n;
		long[] d;
		long x, y;
		n = sc.nextInt();
		d = new long[n];
		for (int i = 0; i < n; ++i)
			d[i] = sc.nextLong();
		Arrays.sort(d);
		x = sc.nextLong();
		y = sc.nextLong();
		x = Math.abs(x);
		y = Math.abs(y);
		System.out.println(solve(n, d, x, y));
	}

	static int solve(int n, long[] d, long x, long y) {
		if (x < y) {
			x ^= y;
			y ^= x;
			x ^= y;
		}
		if (x == 0 && y == 0) {
			return 0;
		}
		for (int i = 0; i < n; ++i) {
			if (d[i] == x + y) {
				return 1;
			}
		}
		HashSet<Long> set = new HashSet();
		for (int i = 0; i < n; ++i) {
			set.add(d[i]);
		}

		for (int i = 0; i < n; ++i) {
			if (!set.contains(x + y - d[i]) && !set.contains(d[i] - x - y) && (!set.contains(d[i] - x + y) || d[i] < x)
					&& (!set.contains(d[i] + x - y) || d[i] < y))
				continue;
			return 2;
		}

		long[] evenD = new long[n];
		long[] oddD = new long[n];
		int oddp = 0, evenp = 0;
		for (int i = 0; i < n; ++i) {
			if (d[i] % 2 == 0)
				evenD[evenp++] = d[i];
			else
				oddD[oddp++] = d[i];
		}
		evenD = Arrays.copyOf(evenD, evenp);
		oddD = Arrays.copyOf(oddD, oddp);
		Arrays.sort(evenD);
		Arrays.sort(oddD);
		if ((x - y) % 2 == 0) {
			if (2 * Math.max(evenD.length > 0 ? evenD[evenD.length - 1] : 0,
					oddD.length > 0 ? oddD[oddD.length - 1] : 0) >= x + y) {
				return 2;
			}
		} else if (evenD.length > 0 && oddD.length > 0) {
			for (int t = 0; t < 2; ++t) {
				int p1 = 0;
				for (p1 = 0; p1 < evenD.length; ++p1) {
					int q1 = Arrays.binarySearch(oddD, x + y - evenD[p1]);
					if (q1 < 0)
						q1 = -q1 - 1;
					int q2 = Arrays.binarySearch(oddD, evenD[p1] - x - y);
					if (q2 < 0)
						q2 = -q2 - 1;
					int p2 = Math.max(q1, q2);
					if (p2 >= oddD.length)
						continue;
					if (evenD[p1] + oddD[p2] >= x + y && x + y >= evenD[p1] - oddD[p2]
							&& evenD[p1] - oddD[p2] >= Math.min(x, y) - Math.max(x, y)) {
						return 2;
					}
				}
				long[] tmp = Arrays.copyOf(evenD, evenD.length);
				evenD = oddD;
				oddD = tmp;
			}

		}
		return -1;

	}


	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0