結果

問題 No.1369 交換門松列・竹
ユーザー ks2mks2m
提出日時 2021-01-29 22:52:51
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,229 bytes
コンパイル時間 2,233 ms
コンパイル使用メモリ 75,368 KB
実行使用メモリ 59,416 KB
最終ジャッジ日時 2023-09-09 16:30:46
合計ジャッジ時間 7,552 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,696 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 44 ms
49,700 KB
testcase_04 AC 44 ms
49,468 KB
testcase_05 AC 45 ms
49,700 KB
testcase_06 WA -
testcase_07 AC 46 ms
49,704 KB
testcase_08 AC 46 ms
49,620 KB
testcase_09 AC 46 ms
49,432 KB
testcase_10 AC 45 ms
49,980 KB
testcase_11 AC 46 ms
49,604 KB
testcase_12 AC 46 ms
49,492 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 151 ms
56,564 KB
testcase_16 AC 158 ms
56,824 KB
testcase_17 AC 154 ms
56,820 KB
testcase_18 AC 154 ms
56,836 KB
testcase_19 AC 150 ms
56,568 KB
testcase_20 AC 151 ms
57,076 KB
testcase_21 AC 155 ms
56,552 KB
testcase_22 AC 151 ms
56,916 KB
testcase_23 AC 154 ms
56,076 KB
testcase_24 AC 152 ms
56,080 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 126 ms
57,360 KB
testcase_29 AC 111 ms
57,440 KB
testcase_30 AC 205 ms
59,416 KB
testcase_31 AC 202 ms
58,968 KB
testcase_32 WA -
testcase_33 AC 124 ms
57,500 KB
権限があれば一括ダウンロードができます

ソースコード

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 + 4 < i && i < r - 4) {
					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(Math.max(i + 3, l + 5), 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(Math.min(j - 2, r - 4), 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) {
						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