結果

問題 No.2518 Adjacent Larger
ユーザー ks2m
提出日時 2023-10-27 21:44:08
言語 Java
(openjdk 23)
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 1,384 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 78,288 KB
実行使用メモリ 64,404 KB
最終ジャッジ日時 2024-09-25 13:49:55
合計ジャッジ時間 8,552 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int t = Integer.parseInt(br.readLine());
		PrintWriter pw = new PrintWriter(System.out);
		label:
		for (int z = 0; z < t; z++) {
			int n = Integer.parseInt(br.readLine());
			String[] sa = br.readLine().split(" ");
			int[] a = new int[n];
			int[] c = new int[3];
			for (int i = 0; i < n; i++) {
				a[i] = Integer.parseInt(sa[i]);
				c[a[i]]++;
			}

			int sum = c[1] + c[2] * 2;
			if (sum != n || c[0] == 0 || c[2] == 0) {
				pw.println("No");
				continue;
			}

			int start = -1;
			for (int i = 0; i < n; i++) {
				if (a[i] == 2) {
					start = i;
					break;
				}
			}

			boolean up = true;
			for (int i = start + 1; i < start + n; i++) {
				int x = i % n;
				if (up) {
					if (a[x] == 0) {
						up = false;
					} else if (a[x] == 1) {
						up = true;
					} else {
						pw.println("No");
						continue label;
					}
				} else {
					if (a[x] == 0) {
						pw.println("No");
						continue label;
					} else if (a[x] == 1) {
						up = false;
					} else {
						up = true;
					}
				}
			}
			if (up) {
				pw.println("No");
			} else {
				pw.println("Yes");
			}
		}
		pw.flush();
		br.close();
	}
}
0