結果

問題 No.2283 Prohibit Three Consecutive
ユーザー cyk203cyk203
提出日時 2023-04-28 21:42:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,975 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 175,024 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 23:21:17
合計ジャッジ時間 2,440 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 14 ms
5,376 KB
testcase_04 AC 13 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 15 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 9 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 13 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* 
 * Author: $%U%$
 * Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
 */
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=200003,M=3003,K=503,MOD=998244353,INF=9000000000000000007ll;
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define per(i,a,b) for(ll i=(a);i>=(b);i--)
#define re(i,a,b) for(ll i=(a);i<(b);i++)
#define pe(i,a,b) for(ll i=(a);i>(b);i--)
ll gcd(ll A,ll B){return B?gcd(B,A%B):A;}
ll pw(ll A,ll B){
	ll res=1;
	while(B){
		if(B&1)res=res*A%MOD;
		A=A*A%MOD;
		B>>=1;
	}
	return res;
}
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
template <typename T>
inline void read(T& r) {
	r=0;bool w=0; char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9') r=r*10+(ch^48), ch=getchar();
	r=w?-r:r;
}
template <typename T>
inline void readupp(T& r){
	r=0;
	char ch=getchar();
	while(ch>'Z'||ch<'A')ch=getchar();
	r=ch;
}
template <typename T>
inline void readlow(T& r){
	r=0;
	char ch=getchar();
	while(ch>'z'||ch<'a')ch=getchar();
	r=ch;
}
template <typename T>
inline void readdig(T& r){
	r=0;
	char ch=getchar();
	while(ch>'9'||ch<'0')ch=getchar();
	r=ch-'0';
}
template <typename T>
inline void readvisi(T& r){
	r=0;
	char ch=getchar();
	while(ch>126||ch<33)ch=getchar();
	r=ch;
}
template <typename T>
inline ll readlowstr(T& r){
	ll n=0;
	char ch=getchar();
	while(ch>'z'||ch<'a')ch=getchar();
	while(ch<='z'&&ch>='a')r[++n]=ch,ch=getchar();
	return n;
}
template <typename T>
inline ll readuppstr(T& r){
	ll n=0;
	char ch=getchar();
	while(ch>'Z'||ch<'A')ch=getchar();
	while(ch<='Z'&&ch>='A')r[++n]=ch,ch=getchar();
	return n;
}
template <typename T>
inline ll readdigstr(T& r){
	ll n=0;
	char ch=getchar();
	while(ch>'9'||ch<'0')ch=getchar();
	while(ch<='9'&&ch>='0')r[++n]=ch,ch=getchar();
	return n;
}
template <typename T>
inline ll readvisistr(T& r){
	ll n=0;
	char ch=getchar();
	while(ch>126||ch<33)ch=getchar();
	while(ch<=126&&ch>=33)r[++n]=ch,ch=getchar();
	return n;
}
void _FILE(string s){
	freopen((s+".in").c_str(),"r",stdin);
	freopen((s+".out").c_str(),"w",stdout);
}
ll T,n;
char a[N];
set<pair<ll,ll> > s;
bool flag;
bool vst[N];
void upd(pair<ll,ll> t){
	ll o1=t.first,o2=t.second;
	vst[o1]=1;
	ll o3=(o1+n-1)%n,o4=(o2+1)%n;
	if(a[o3]==a[o1]||a[o4]==a[o2]){
		flag=0;
		return;
	}
	a[o3]=a[o1]^1,a[o4]=a[o2]^1;
	ll o5=(o3+n-1)%n,o6=(o4+1)%n;
	if(a[o3]==a[o5]&&!vst[o5])s.insert(make_pair(o5,o3));
	if(a[o4]==a[o6]&&!vst[o4])s.insert(make_pair(o4,o6));
}
int main(){
	read(T);
	while(T--){
		read(n);
		re(i,0,n)readvisi(a[i]);
		fill(vst,vst+n,0);
		s.clear();
		flag=1;
		re(i,0,n){
			if(a[i]==a[(i+1)%n]&&a[i]!='?')s.insert(make_pair(i,(i+1)%n));
		}
		while(!s.empty()){
			pair<ll,ll> tmp=*s.begin();
			s.erase(s.begin());
			upd(tmp);
			if(!flag)break;
		}
		re(i,0,n){
			if(a[i]==a[(i+1)%n]&&a[(i+1)%n]==a[(i+2)%n]&&a[i]!='?')flag=0;
		}
		if(flag)puts("Yes");
		else puts("No");
	}
	return 0;
}
0