結果

問題 No.1369 交換門松列・竹
ユーザー ks2mks2m
提出日時 2021-01-29 22:44:33
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 2,228 bytes
コンパイル時間 3,091 ms
コンパイル使用メモリ 81,660 KB
実行使用メモリ 61,116 KB
最終ジャッジ日時 2023-09-09 16:21:23
合計ジャッジ時間 7,790 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 WA -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 WA -
testcase_07 AC 43 ms
49,528 KB
testcase_08 RE -
testcase_09 AC 43 ms
49,640 KB
testcase_10 AC 43 ms
49,652 KB
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 150 ms
56,392 KB
testcase_21 RE -
testcase_22 AC 152 ms
57,124 KB
testcase_23 RE -
testcase_24 AC 152 ms
56,928 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 123 ms
57,704 KB
testcase_29 AC 120 ms
57,700 KB
testcase_30 AC 214 ms
59,092 KB
testcase_31 AC 207 ms
61,116 KB
testcase_32 RE -
testcase_33 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.TreeSet;

public class Main {
	static BufferedReader br;
	static PrintWriter pw;

	public static void main(String[] args) throws Exception {
		br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		pw = new PrintWriter(System.out);
		for (int i = 0; i < t; i++) {
			solve();
		}
		br.close();
		pw.flush();
	}

	static void solve() throws Exception {
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}

		TreeSet<Integer> set = new TreeSet<>();
		for (int i = 0; i < n - 2; i++) {
			int[] b = new int[3];
			for (int j = 0; j < 3; j++) {
				b[j] = a[i + j];
			}
			if (!check(b)) {
				set.add(i);
			}
		}
		int l = set.pollFirst();
		int r = l;
		if (!set.isEmpty()) {
			r = set.pollLast();
		}
		if (!set.isEmpty()) {
			for (int i : set) {
				if (l + 2 < i && i < r - 2) {
					pw.println("No");
					return;
				}
			}
		}
		int el = Math.min(l + 3, n);
		for (int i = Math.max(l - 2, 0); i < el; i++) {
			int er = Math.min(r + 3, n);
			for (int j = Math.max(r - 2, l + 1); j < er; j++) {
				int t = a[j];
				a[j] = a[i];
				a[i] = t;
				boolean flg = true;
				int e = Math.min(i + 3, n - 2);
				for (int k = Math.max(l - 2, 0); k < e; k++) {
					int[] b = new int[3];
					for (int k2 = 0; k2 < 3; k2++) {
						b[k2] = a[k + k2];
					}
					if (!check(b)) {
						flg = false;
						break;
					}
				}
				if (flg) {
					e = Math.min(r + 3, n - 2);
					for (int k = Math.max(j - 2, 0); k < e; k++) {
						int[] b = new int[3];
						for (int k2 = 0; k2 < 3; k2++) {
							b[k2] = a[k + k2];
						}
						if (!check(b)) {
							flg = false;
							break;
						}
					}
					if (flg) {
						throw new Exception();
//						pw.println("Yes");
//						return;
					}
				}
				a[i] = a[j];
				a[j] = t;
			}
		}
		pw.println("No");
	}

	static boolean check(int[] a) {
		if (a[0] != a[2]) {
			if (a[0] < a[1] && a[1] > a[2]) return true;
			if (a[0] > a[1] && a[1] < a[2]) return true;
		}
		return false;
	}
}
0