結果

問題 No.360 増加門松列
ユーザー mitsuomitsuo
提出日時 2016-05-04 17:58:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 1,836 bytes
コンパイル時間 1,909 ms
コンパイル使用メモリ 84,384 KB
実行使用メモリ 59,160 KB
最終ジャッジ日時 2023-08-25 18:38:44
合計ジャッジ時間 6,277 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 159 ms
58,520 KB
testcase_01 AC 145 ms
58,392 KB
testcase_02 AC 140 ms
58,080 KB
testcase_03 AC 142 ms
58,100 KB
testcase_04 AC 155 ms
59,160 KB
testcase_05 AC 158 ms
58,744 KB
testcase_06 AC 145 ms
58,608 KB
testcase_07 AC 143 ms
58,456 KB
testcase_08 AC 159 ms
58,348 KB
testcase_09 AC 157 ms
58,460 KB
testcase_10 AC 157 ms
58,016 KB
testcase_11 AC 146 ms
58,040 KB
testcase_12 AC 162 ms
58,820 KB
testcase_13 AC 153 ms
58,892 KB
testcase_14 AC 150 ms
58,056 KB
testcase_15 AC 158 ms
58,464 KB
testcase_16 AC 158 ms
55,980 KB
testcase_17 AC 143 ms
58,556 KB
testcase_18 AC 145 ms
58,516 KB
testcase_19 AC 146 ms
58,176 KB
testcase_20 AC 145 ms
58,496 KB
testcase_21 AC 143 ms
58,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] input = new String[1];
		int i = 0;
		while (sc.hasNext()) {
			input[i] = sc.nextLine();
			i++;
		}
		String[] d = input[0].split(" ");
		int[] dl = new int[d.length];
		for (i = 0; i < d.length; i++) {
			dl[i] = Integer.valueOf(d[i]);
		}

		System.out.println(solve(dl));

		sc.close();
	}

	public static String solve(int[] d) {
		List<Integer> t = new ArrayList<>();
		for (int dd : d) {
			t.add(dd);
		}
		List<List<Integer>> candidate = getPermulation(t);
		// System.out.println(candidate.size());

		for (List<Integer> c : candidate) {
			if (isMatch(toArr(c))) {
				return "YES";
			}
		}

		return "NO";
	}

	private static int[] toArr(List<Integer> c) {
		int[] r = new int[c.size()];
		for (int i = 0; i < c.size(); i++) {
			r[i] = c.get(i);
		}
		return r;
	}

	public static List<List<Integer>> getPermulation(List<Integer> d) {
		int LL = d.size();
		List<List<Integer>> ret = new ArrayList<List<Integer>>();
		if (LL == 1) {
			ret.add(d);
		} else {
			for (int i = 0; i < LL; i++) {
				List<Integer> r = new ArrayList<>(d);
				int keep = r.remove(i);
				List<List<Integer>> rr = getPermulation(r);

				for (int k = 0; k < rr.size(); k++) {
					rr.get(k).add(keep);
				}
				ret.addAll(rr);
			}

		}
		return ret;
	}

	private static boolean isMatch(int[] c) {
		for (int i = 0; i < c.length - 2; i++) {
			int a0 = i;
			int a1 = i + 1;
			int a2 = i + 2;
			if (c[a0] == c[a1] || c[a1] == c[a2] || c[a2] == c[a0]) {
				return false;
			}
			if (c[a0] > c[a2]) {
				return false;
			}
			if ((c[a0] < c[a1] && c[a1] < c[a2]) || (c[a2] < c[a1] && c[a1] < c[a0])) {
				return false;
			}

		}
		return true;
	}
}
0