結果

問題 No.1369 交換門松列・竹
ユーザー ks2mks2m
提出日時 2021-01-29 23:25:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 226 ms / 2,000 ms
コード長 2,473 bytes
コンパイル時間 3,150 ms
コンパイル使用メモリ 75,388 KB
実行使用メモリ 59,752 KB
最終ジャッジ日時 2023-09-09 17:10:21
合計ジャッジ時間 8,562 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,564 KB
testcase_01 AC 44 ms
49,656 KB
testcase_02 AC 44 ms
49,568 KB
testcase_03 AC 45 ms
49,668 KB
testcase_04 AC 44 ms
49,372 KB
testcase_05 AC 44 ms
49,656 KB
testcase_06 AC 44 ms
50,060 KB
testcase_07 AC 43 ms
49,480 KB
testcase_08 AC 45 ms
49,528 KB
testcase_09 AC 45 ms
49,424 KB
testcase_10 AC 44 ms
49,560 KB
testcase_11 AC 43 ms
49,988 KB
testcase_12 AC 44 ms
49,504 KB
testcase_13 AC 170 ms
59,548 KB
testcase_14 AC 191 ms
59,752 KB
testcase_15 AC 173 ms
59,156 KB
testcase_16 AC 155 ms
56,452 KB
testcase_17 AC 148 ms
56,544 KB
testcase_18 AC 146 ms
56,540 KB
testcase_19 AC 175 ms
59,012 KB
testcase_20 AC 152 ms
56,176 KB
testcase_21 AC 226 ms
58,720 KB
testcase_22 AC 146 ms
55,488 KB
testcase_23 AC 153 ms
55,452 KB
testcase_24 AC 157 ms
56,744 KB
testcase_25 AC 137 ms
56,140 KB
testcase_26 AC 141 ms
55,556 KB
testcase_27 AC 140 ms
56,116 KB
testcase_28 AC 112 ms
57,736 KB
testcase_29 AC 121 ms
56,992 KB
testcase_30 AC 223 ms
58,540 KB
testcase_31 AC 207 ms
59,300 KB
testcase_32 AC 149 ms
56,372 KB
testcase_33 AC 123 ms
57,184 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;
				}
			}
		}
		boolean near = false;
		if (r - l < 5) {
			near = true;
		}
		int el = Math.min(l + 3, n);
		for (int i = Math.max(l - 2, 0); i < el; i++) {
			int sr = 0;
			int er = n;
			if (!near) {
				sr = Math.max(r - 2, i + 1);
				er = Math.min(r + 3, n);
			}
			for (int j = sr; j < er; j++) {
				if (i == j) {
					continue;
				}
				int t = a[j];
				a[j] = a[i];
				a[i] = t;
				boolean flg = true;
				int e = Math.min(l + 7, n - 2);
				for (int k = Math.max(l - 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) {
					int sk = Math.max(j - 2, 0);
					int ek = Math.min(j + 1, n - 2);
					if (!near) {
						sk = Math.max(r - 6, 0);
						ek = Math.min(r + 5, n - 2);
					}
					for (int k = sk; k < ek; 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