Official

A - ARC Arc Editorial by evima


Hints: https://atcoder.jp/contests/arc192/editorial/12197


We consider cases according to \(N\) modulo \(4\).

When \(N\) is a multiple of \(4\)

The string ARCRARCR...ARCR is always a good string.

When \(N\) is odd

If all elements of \(A\) are \(0\), then no good string exists.

Proof When it is possible to choose an index \(i\) to perform an operation, it is impossible to choose \(i-1\) or \(i+1\) for an operation.
Therefore, if we avoid operating on the same position more than once, the positions on which operations are performed do not overlap. Hence, the number of \(1\)’s contained in \(A\) is always even.
Since \(N\) is odd, it is impossible to make all elements of \(A\) (an odd number of elements) equal to \(1\).

Now consider the case where \(A\) contains a \(1\). Choose an arbitrary index \(x\) such that \(A_x=1\). If a construction is possible with \(x=N\), then by an appropriate cyclic shift a construction for the general case can be obtained. In the case \(x=N\), either ARCRARCR...ARC or ARCRARCR...CRA satisfies the conditions.

When \(N\) modulo \(4\) is \(2\)

If all elements of \(A\) are \(0\), then no good string exists.

Proof Since an operation must be possible, we may assume without loss of generality that the first three characters of \(S\) are ARC.
From here, in order to be able to operate on \(A_3\) and \(A_4\), the string must continue as ARCRA.
In this way the string is uniquely determined, and eventually one obtains the string ARCRARCR...CRAR.
However, with this string it is impossible to operate on the last two terms of \(A\), so it is impossible to turn all elements of \(A\) into \(1\).

Now consider the case where \(A\) contains a \(1\). Let \(T\) be the set of indices \(i\) for which \(A_i=1\). If \(T\) does not contain an odd index or \(T\) does not contain an even index, then no good string exists.

Proof Consider the case in which every element of \(A\) is \(0\).
Define \(X\) to be the number of odd indices \(i\) with \(A_i=0\), and \(Y\) to be the number of even indices \(i\) with \(A_i=0\). Initially, \(X=Y\).
If we never operate on the same position more than once, then because the operated positions do not overlap, in each operation both \(X\) and \(Y\) decrease by exactly \(1\).
Since we have already shown that it is impossible to reduce both \(X\) and \(Y\) to \(0\), it follows that if \(T\) is missing either an odd or an even index, then no good string exists.

If \(T\) contains both odd and even indices, then a good string exists. Choose an arbitrary odd index \(x\) in \(T\) and an arbitrary even index \(y\) in \(T\).

If a construction is possible with \(y=N\), then by an appropriate cyclic shift a construction for the general case can be obtained. In the case \(y=N\), the following constructions work:

  • If \(x\) leaves a remainder of \(1\) when divided by \(4\), then concatenating a string of length \(x\) ARCRARCR...CRA with a string of length \(N-x\) ARCRARCR...CRA satisfies the conditions.
  • If \(x\) leaves a remainder of \(3\) when divided by \(4\), then concatenating a string of length \(x\) ARCRARCR...ARC with a string of length \(N-x\) ARCRARCR...ARC satisfies the conditions.

Thus, all cases have been covered.

Sample Implementation (PyPy3)

N = int(input())
A = list(map(int, input().split()))

if N % 4 == 0:
    print("Yes")
elif N % 2 == 1:
    if 1 in A:
        print("Yes")
    else:
        print("No")
else:
    odd = False
    even = False
    for i in range(N):
        if A[i] == 1:
            if i % 2 == 1:
                odd = True
            else:
                even = True

    if odd and even:
        print("Yes")
    else:
        print("No")

posted:
last update: