結果

問題 No.2518 Adjacent Larger
ユーザー cho435cho435
提出日時 2023-10-28 13:02:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,318 bytes
コンパイル時間 4,974 ms
コンパイル使用メモリ 266,656 KB
実行使用メモリ 5,116 KB
最終ジャッジ日時 2023-10-28 13:02:14
合計ジャッジ時間 7,272 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 12 ms
4,348 KB
testcase_07 AC 12 ms
4,348 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 WA -
testcase_14 RE -
testcase_15 WA -
testcase_16 AC 37 ms
5,116 KB
testcase_17 AC 22 ms
4,432 KB
testcase_18 AC 23 ms
4,432 KB
testcase_19 AC 19 ms
4,348 KB
testcase_20 AC 27 ms
4,432 KB
testcase_21 AC 35 ms
4,348 KB
testcase_22 AC 35 ms
4,348 KB
testcase_23 AC 38 ms
4,960 KB
testcase_24 AC 36 ms
4,960 KB
testcase_25 AC 37 ms
4,960 KB
testcase_26 AC 37 ms
4,960 KB
testcase_27 AC 35 ms
4,348 KB
testcase_28 AC 53 ms
4,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)

void solve(){
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n) cin>>a.at(i);
	int flg=0;
	rep(i,n-1){
		if(a.at(i)==a.at(i+1)){
			if(a.at(i)!=1) flg=1;
		}
	}
	if(a.at(0)==a.at(n-1)) if(a.at(0)!=1) flg=1;
	int c0=0,c2=0;
	rep(i,n){
		if(a.at(i)==0) c0++;
		if(a.at(i)==2) c2++;
	}
	if(c0*c2==0) flg=1;
	int sm=0;
	rep(i,n) sm+=a.at(i);
	if(sm!=n) flg=1;
	if(flg){
		cout<<"No\n";
		return;
	}
	vector<int> ls(n,-1);
	rep(i,n){
		if(a.at(i)==2) ls.at(i)=1;
		if(a.at(i)==0) ls.at(i)=0;
		if(i<n-1){
			if(a.at(i+1)==0) ls.at(i)=1;
			if(a.at(i+1)==2) ls.at(i)=0;
		}
	}
	if(a.at(0)==0) ls.at(n-1)=1;
	if(a.at(0)==2) ls.at(n-1)=0;
	queue<int> q;
	rep(i,n-1){
		if(ls.at(i)!=-1&&ls.at(i+1)==-1) q.push(i+1);
	}
	if(ls.at(n-1)!=-1&&ls.at(0)==-1) q.push(0);
	while(!q.empty()){
		int p=q.front();
		q.pop();
		int bf=(p+n-1)%n;
		if(ls.at(p)!=-1){
			int sm=0;
			sm+=ls.at(p);
			sm+=ls.at(bf)^1;
			if(sm!=a.at(p)){
				flg=1;
				break;
			}
			continue;
		}
		ls.at(p)=a.at(p)-ls.at(bf)^1;
		if(ls.at(p)==2){
			flg=1;
			break;
		}
		q.push((p+1)%n);
	}
	rep(i,n) assert(ls.at(i)!=-1);
	if(flg) cout<<"No\n";
	else cout<<"Yes\n";
}

int main(){
	int t;
	cin>>t;
	rep(i,t) solve();
}
0