結果

問題 No.3024 等式
ユーザー 37zigen37zigen
提出日時 2017-04-08 03:32:13
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,131 bytes
コンパイル時間 3,281 ms
コンパイル使用メモリ 77,500 KB
実行使用メモリ 84,832 KB
最終ジャッジ日時 2023-09-23 04:30:08
合計ジャッジ時間 14,655 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,760 KB
testcase_01 AC 127 ms
55,656 KB
testcase_02 AC 133 ms
55,748 KB
testcase_03 AC 134 ms
55,692 KB
testcase_04 AC 128 ms
55,408 KB
testcase_05 AC 128 ms
55,668 KB
testcase_06 AC 174 ms
61,376 KB
testcase_07 AC 172 ms
61,152 KB
testcase_08 AC 174 ms
61,328 KB
testcase_09 AC 129 ms
55,448 KB
testcase_10 AC 130 ms
55,324 KB
testcase_11 AC 127 ms
55,440 KB
testcase_12 AC 488 ms
70,520 KB
testcase_13 AC 487 ms
70,824 KB
testcase_14 AC 498 ms
70,328 KB
testcase_15 AC 140 ms
57,272 KB
testcase_16 AC 130 ms
55,592 KB
testcase_17 AC 175 ms
60,848 KB
testcase_18 AC 140 ms
55,436 KB
testcase_19 AC 166 ms
59,652 KB
testcase_20 AC 141 ms
57,476 KB
testcase_21 AC 134 ms
55,776 KB
testcase_22 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	ArrayList<Double>[][] dp;

	// [left,right]
	ArrayList<Double> rec(int left, int right, int[] A, int[] ord) {
		if (dp[left][right].size() != 0) {
			return dp[left][right];
		}
		if (right - left == 0) {
			dp[left][right].add((double) A[ord[left]]);
			return dp[left][right];
		}
		ArrayList<Double> ret = new ArrayList<>();
		for (int i = left; i < right; ++i) {
			ArrayList<Double> lv = rec(left, i, A, ord);
			ArrayList<Double> rv = rec(i + 1, right, A, ord);
			for (double v1 : lv) {
				for (double v2 : rv) {
					if (v1 >= v2) {
						ret.add(v1 + v2);
						ret.add(v1 - v2);
						ret.add(v1 * v2);
						if (v1 + v2 < 1e-6 || v1 - v2 < 1e-6) {
							System.out.println("YES");
							System.exit(0);
						}
					}
					if (v2 != 0) {
						ret.add(v1 / v2);
					}
				}
			}
		}
		dp[left][right] = ret;
		return ret;
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		for (int i = 0; i < N; ++i) {
			A[i] = sc.nextInt();
		}
		int[] ord = new int[N];
		for (int i = 0; i < ord.length; ++i) {
			ord[i] = i;
		}
		// permutationの状態数は5040通り。
		do {
			dp = new ArrayList[N][N];
			for (int i = 0; i < N; ++i) {
				for (int j = 0; j < N; ++j) {
					dp[i][j] = new ArrayList<>();
				}
			}
			rec(0, N - 1, A, ord);
		} while (nextPermutation(ord));
		System.out.println("NO");
	}

	boolean nextPermutation(int[] ord) {
		int n = ord.length;
		int i = n - 1;
		while (i - 1 >= 0 && ord[i - 1] > ord[i]) {
			--i;
		}

		if (i == 0)
			return false;
		int j = i;

		while (j + 1 < n && ord[i - 1] < ord[j + 1]) {
			++j;
		}
		int tmp = ord[i - 1];
		ord[i - 1] = ord[j];
		ord[j] = tmp;
		int s = i;
		int t = n - 1;
		while (t - s > 0) {
			tmp = ord[t];
			ord[t] = ord[s];
			ord[s] = tmp;
			++s;
			--t;
		}

		return true;

	}

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