A - ARC Arc Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

正整数 NN と、長さ NN00 または 11 からなる数列 A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N) が与えられます。

長さ NN の英大文字のみからなる文字列 SS であって、以下の操作を 00 回以上好きな回数行うことで AA00 が含まれないようにできるものを 良い文字列 と呼びます。ここで、SiS_i (1iN)(1\leq i\leq N)SSii 文字目を表し、SN+1=S1,SN+2=S2,AN+1=A1S_{N+1}=S_1,S_{N+2}=S_2,A_{N+1}=A_1 とします。

  • 次のうちどちらかを選び、実行する。
    • 1iN1\leq i\leq N なる整数 ii であって Si=S_i= ASi+1=S_{i+1}= RSi+2=S_{i+2}= C となるものを選び、Ai,Ai+1A_i,A_{i+1}11 に置き換える。
    • 1iN1\leq i\leq N なる整数 ii であって Si+2=S_{i+2}= ASi+1=S_{i+1}= RSi=S_i= C となるものを選び、Ai,Ai+1A_i,A_{i+1}11 に置き換える。

良い文字列が存在するか判定してください。

制約

  • 3N2000003\leq N\leq 200000
  • Ai{0,1}A_i\in \lbrace 0,1 \rbrace (1iN)(1\leq i\leq N)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 \dots ANA_N

出力

良い文字列が存在するならば Yes と、存在しないならば No と出力せよ。

正誤判定器は大文字と小文字を区別せず、どちらも受理する。例えば、答えが Yes となるときに yesYESyEs などと出力しても正解と判定される。


入力例 1Copy

Copy
12
0 1 0 1 1 1 1 0 1 1 1 0

出力例 1Copy

Copy
Yes

例えば RARCARCCRAGC は良い文字列です。以下のように操作することで、AA の全ての要素を 11 にすることが可能であるからです。

  • はじめ、A=(0,1,0,1,1,1,1,0,1,1,1,0)A=(0,1,0,1,1,1,1,0,1,1,1,0) である。
  • i=2i=2 として前者の操作をする。A=(0,1,1,1,1,1,1,0,1,1,1,0)A=(0,1,1,1,1,1,1,0,1,1,1,0) となる。
  • i=5i=5 として前者の操作をする。A=(0,1,1,1,1,1,1,0,1,1,1,0)A=(0,1,1,1,1,1,1,0,1,1,1,0) となる。
  • i=8i=8 として後者の操作をする。A=(0,1,1,1,1,1,1,1,1,1,1,0)A=(0,1,1,1,1,1,1,1,1,1,1,0) となる。
  • i=12i=12 として後者の操作をする。A=(1,1,1,1,1,1,1,1,1,1,1,1)A=(1,1,1,1,1,1,1,1,1,1,1,1) となる。

良い文字列が存在するので、Yes と出力してください。


入力例 2Copy

Copy
3
0 0 0

出力例 2Copy

Copy
No

良い文字列は存在しません。


入力例 3Copy

Copy
29
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

出力例 3Copy

Copy
Yes

操作するまでもなく AA00 は含まれていないので、英大文字からなる長さ 2929 の文字列はすべて良い文字列です。

Score : 400400 points

Problem Statement

You are given a positive integer NN and a sequence A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N) of length NN, consisting of 00 and 11.

We call a string SS of length NN, consisting only of uppercase English letters, a good string if it is possible to perform the following operation any number of times (possibly zero) so that the sequence AA contains no 00. Here, SiS_i (1iN)(1\leq i\leq N) denotes the ii-th character of SS, and we define SN+1=S1S_{N+1}=S_1, SN+2=S2S_{N+2}=S_2, and AN+1=A1A_{N+1}=A_1.

  • Perform one of the following operations:
    • Choose an integer ii with 1iN1\leq i\leq N such that Si=S_i= A, Si+1=S_{i+1}= R, and Si+2=S_{i+2}= C, and replace each of AiA_i and Ai+1A_{i+1} with 11.
    • Choose an integer ii with 1iN1\leq i\leq N such that Si+2=S_{i+2}= A, Si+1=S_{i+1}= R, and Si=S_i= C, and replace each of AiA_i and Ai+1A_{i+1} with 11.

Determine whether there exists a good string.

Constraints

  • 3N2000003\leq N\leq 200000
  • Ai{0,1}A_i\in \lbrace 0,1 \rbrace (1iN)(1\leq i\leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 \dots ANA_N

Output

If there exists a good string, print Yes; otherwise, print No.

The judge is case-insensitive; for example, if the correct answer is Yes, outputs such as yes, YES, or yEs will also be accepted.


Sample Input 1Copy

Copy
12
0 1 0 1 1 1 1 0 1 1 1 0

Sample Output 1Copy

Copy
Yes

For example, RARCARCCRAGC is a good string. This is because it is possible to change all elements of AA to 11 by performing the following operations:

  • Initially, A=(0,1,0,1,1,1,1,0,1,1,1,0)A=(0,1,0,1,1,1,1,0,1,1,1,0).
  • Perform the first operation with i=2i=2. Then, A=(0,1,1,1,1,1,1,0,1,1,1,0)A=(0,1,1,1,1,1,1,0,1,1,1,0).
  • Perform the first operation with i=5i=5. Then, A=(0,1,1,1,1,1,1,0,1,1,1,0)A=(0,1,1,1,1,1,1,0,1,1,1,0).
  • Perform the second operation with i=8i=8. Then, A=(0,1,1,1,1,1,1,1,1,1,1,0)A=(0,1,1,1,1,1,1,1,1,1,1,0).
  • Perform the second operation with i=12i=12. Then, A=(1,1,1,1,1,1,1,1,1,1,1,1)A=(1,1,1,1,1,1,1,1,1,1,1,1).

Since there exists a good string, output Yes.


Sample Input 2Copy

Copy
3
0 0 0

Sample Output 2Copy

Copy
No

Good strings do not exist.


Sample Input 3Copy

Copy
29
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 3Copy

Copy
Yes

Since AA already contains no 00, every string of length 2929 consisting of uppercase English letters is a good string.



2025-04-25 (Fri)
13:51:51 +00:00