Problem:


Score : 1200 points

Problem Statement

You are given a permutation of 1,2,...,N: p_1,p_2,...,p_N. Determine if the state where p_i=i for every i can be reached by performing the following operation any number of times:

Choose three elements p_{i-1},p_{i},p_{i+1} (2\leq i\leq N-1) such that p_{i-1}>p_{i}>p_{i+1} and reverse the order of these three.

Constraints

3 \leq N \leq 3 × 10^5

p_1,p_2,...,p_N is a permutation of 1,2,...,N.

Input

Input is given from Standard Input in the following format:

N
p_1
:
p_N

Output

If the state where p_i=i for every i can be reached by performing the operation, print Yes; otherwise, print No.

Sample Input 1

5
5
2
1
4
3

Sample Output 1

Yes

The state where p_i=i for every i can be reached as follows:

Reverse the order of p_1,p_2,p_3. The sequence p becomes 1,2,5,4,3.

Reverse the order of p_3,p_4,p_5. The sequence p becomes 1,2,3,4,5.

Sample Input 2

4
3
2
4
1

Sample Output 2

No

Sample Input 3

7
3
2
1
6
5
4
7

Sample Output 3

Yes

Sample Input 4

6
5
3
4
1
2
6

Sample Output 4

No



Sample input:
5
5
2
1
4
3


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
print("No")
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514
a=114514

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
p = [int(input()) for _ in range(N)]
p_tmp = [i for i in p]
swap_idx = []
i = 0
while i < N:
    if p_tmp[i] == i + 1:
        i += 1
        continue
    found = False
    for j in range(i + 2, N, 2):
        if p_tmp[j] == i + 1:
            found = True
            break
    if not found:
        print('No')
        sys.exit(0)
    # checkいるかも？
    # swap
    tmp = p_tmp[j]
    for k in range(j, i, -2):
        p_tmp[k] = p_tmp[k - 2]
    p_tmp[i] = tmp
    for k in range(i+1, j, 2):
        if p_tmp[k] != k + 1:
            print('No')
            sys.exit(0)
    i += 2
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
p = [int(input()) for _ in range(N)]
p_tmp = [i for i in p]
swap_idx = []
i = 0
while i < N:
    if p_tmp[i] == i + 1:
        i += 1
        continue
    found = False
    for j in range(i + 2, N, 2):
        if p_tmp[j] == i + 1:
            found = True
            break
    if not found:
        print('No')
        sys.exit(0)
    # checkいるかも？
    # swap
    tmp = p_tmp[j]
    for k in range(j, i, -2):
        p_tmp[k] = p_tmp[k - 2]
    p_tmp[i] = tmp
    for k in range(i, j):
        if p_tmp[k] != k + 1:
            print('No')
            sys.exit(0)
    i = j-1
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
N = int(input())
p = [int(input()) for _ in range(N)]
p_tmp = [i for i in p]
swap_idx = []
i = 0
while i < N:
    if p_tmp[i] == i + 1:
        i += 1
        continue
    found = False
    for j in range(i + 2, N, 2):
        if p_tmp[j] == i + 1:
            found = True
            break
    if not found:
        print('No')
        sys.exit(0)
    # checkいるかも？
    # swap
    tmp = p_tmp[j]
    for k in range(j, i+1, -2):
        p_tmp[k] = p_tmp[k - 2]
    p_tmp[i] = tmp
    for k in range(i, j):
        if p_tmp[k] != k + 1:
            print('No')
            sys.exit(0)
    i = j
print('Yes')

Submission status: Wrong Answer

Solution code:
n=int(input())
ans='Yes'
for i in range(n):
  if (i+int(input()))%2==0:
    ans='No'
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans='Yes'
i=0
while ans=='Yes' and i<n:
  if (i+int(input()))%2==0:
    ans='No'
  i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans='Yes'
i=0
while ans=='Yes' and i<n:
  if (i+int(input()))%2==0:
    ans='No'
  i+=1

Submission status: Wrong Answer

Solution code:
n = int(input())
p = []
for i in range(n):
  array = int(input())
  p.append(array)

p1 = p[0::2]
p2 = p[1::2]

if all([x%2 == 1 for x in p1]) and all([x%2 == 0 for x in p2]):
  print('Yes')
else:
  print('No')

Submission status: Wrong Answer

Solution code:
N = int(input())
p = [ int(input()) for i in range(N) ]
even = [0]*(N//2)
odd = [0]*(N-len(even))
for i in range(len(even)):
    if p[2*i+1]%2 != 0:
        print("No")
        exit()
for i in range(len(odd)):
    if p[2*i]%2 != 1:
        print("No")
        exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
N=int(input())
p=[int(input()) for i in range(N)]
for i in range(N):
    if i%2==p[i]%2:
        print("No")
        exit()
print("Yes")

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    n = input()
    n = int(n)

    f = True
    t = 1
    x = -1
    for i in range(1, n + 1):
        p = input()
        p = int(p)
        if (p + i) % 2 == 1:
            f = False
        elif p == t:
            if x == -1 or x == i:
                t = i + 1
                x = -1
            else:
                t = i
        else:
            if (t + i) % 2 == 0:
                if x == -1:
                    x = p
                else:
                    f = False
            else:
                if p != i:
                    f = False
        if x == i + 2:
            x = -1


    print('Yes' if f else 'No')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    n = input()
    n = int(n)

    f = True
    t = 1
    x = -1
    for i in range(1, n + 1):
        p = input()
        p = int(p)
        if (p + i) % 2 == 1:
            f = False
        elif p == t:
            if x == -1 or x == i:
                t = i + 1
                x = -1
            else:
                t = i
                if x == i + 2:
                    x = -1
        else:
            if (t + i) % 2 == 0:
                if x != -1:
                    f = False
                elif p != i + 2:
                    x = p
            else:
                if p != i:
                    f = False


    print('Yes' if f else 'No')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    n = input()
    n = int(n)

    f = True
    t = 1
    x = -1
    for i in range(1, n + 1):
        p = input()
        p = int(p)
        if (p + i) % 2 == 1:
            f = False
        elif p == t:
            if x == -1 or x == i:
                t = i + 1
                x = -1
            else:
                t = i
        else:
            if (t + i) % 2 == 0:
                if x != -1:
                    f = False
                elif p != i + 2:
                    x = p
            else:
                if p != i:
                    f = False


    print('Yes' if f else 'No')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    n = input()
    n = int(n)

    f = True
    t = 1
    x = -1
    for i in range(1, n + 1):
        p = input()
        p = int(p)
        if (p + i) % 2 == 1:
            f = False
        elif p == t:
            if x == -1 or x == i:
                t = i + 1
                x = -1
            else:
                t = i
        else:
            if (t + i) % 2 == 0:
                if p != i + 2:
                    if x == -1:
                        x = p
                    else:
                        f = False
            else:
                if p != i:
                    f = False


    print('Yes' if f else 'No')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = 'Yes'  

for i in range(N):
    if (int(input()) + i + N) % 2 == 1:
        ans = 'No'

else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

for i in range(N):
    p = int(input())
    if (p-1)%2 != i%2:
        print('No')
        break
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
N = int(input())
A=[]
ret = "Yes"
for i in range(N):
    A.append (int(input()))
    
for i in range(N-2):
    if A[i]-A[i+2] % 2 !=0:
        ret = "No"


print (ret)

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
lis=[]
for i in range(N):
  lis.append(int(input()))
j = 0
while j < N:
  if j%2==lis[j]%2:
    print('No')
    sys.exit()
  j += 1
print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

N=int(input())
p=[0]+[int(input()) for i in range(N)]
#N=10
#p=[3,2,5,4,9,6,1,8,7,10]
#p=[0]+p

now=1
checking=1
while now<=N:

    #print("x",now,checking)

    if p[now]==checking:
        now+=1
        checking=now
        continue
    elif p[now]==now+2:
        checking=now
        while True:
            if now+1<=N:
                if p[now+1]!=now+1:
                    print("No")
                    sys.exit()

            if now+2<=N:
                #print("b",now)
                if p[now+2]==now+4:
                    now+=2
                elif p[now+2]==checking:
                    now+=3
                    checking=now
                    #print("a",now)
                    break
                else:
                    #print("c",now)
                    if now+4>N:
                        print("No")
                        sys.exit()

                    if p[now+4]!=checking:
                        print("No")
                        sys.exit()

                    now+=2
                    if (p[now]-now)%2==1:
                        print("No")
                        sys.exit()
                    else:
                        for i in range(now+1,p[now],2):
                            if p[i]!=i:
                                print("No")
                                sys.exit()

                        for i in range(now+4,p[now],2):
                            if p[i]!=i-2:
                                print("No")
                                sys.exit()

                        now,checking=p[now],p[now]-2
                        break


    else:
        if (p[now]-now)%2==1:
            print("No")
            sys.exit()
        else:
            for i in range(now+1,p[now],2):
                if p[i]!=i:
                    print("No")
                    sys.exit()

            for i in range(now+2,p[now],2):
                if p[i]!=i-2:
                    print("No")
                    sys.exit()

            #if p[p[now]-2]==



            now,checking=p[now],p[now]-2




print("Yes")

Submission status: Wrong Answer

Solution code:
import sys

#N=int(input())
#p=[0]+[int(input()) for i in range(N)]
N=10
p=[3,2,5,4,9,6,1,8,7,10]
p=[0]+p

now=1
checking=1
while now<=N:

    #print("x",now,checking)

    if p[now]==checking:
        now+=1
        checking=now
        continue
    elif p[now]==now+2:
        checking=now
        while True:
            if now+1<=N:
                if p[now+1]!=now+1:
                    print("No")
                    sys.exit()

            if now+2<=N:
                #print("b",now)
                if p[now+2]==now+4:
                    now+=2
                elif p[now+2]==checking:
                    now+=3
                    checking=now
                    #print("a",now)
                    break
                else:
                    #print("c",now)
                    if now+4>N:
                        print("No")
                        sys.exit()

                    if p[now+4]!=checking:
                        print("No")
                        sys.exit()

                    now+=2
                    if (p[now]-now)%2==1:
                        print("No")
                        sys.exit()
                    else:
                        for i in range(now+1,p[now],2):
                            if p[i]!=i:
                                print("No")
                                sys.exit()

                        for i in range(now+4,p[now],2):
                            if p[i]!=i-2:
                                print("No")
                                sys.exit()

                        now,checking=p[now],p[now]-2
                        break


    else:
        if (p[now]-now)%2==1:
            print("No")
            sys.exit()
        else:
            for i in range(now+1,p[now],2):
                if p[i]!=i:
                    print("No")
                    sys.exit()

            for i in range(now+2,p[now],2):
                if p[i]!=i-2:
                    print("No")
                    sys.exit()

            #if p[p[now]-2]==



            now,checking=p[now],p[now]-2




print("Yes")

Submission status: Wrong Answer

Solution code:
import sys

N=int(input())
p=[0]+[int(input()) for i in range(N)]
#N=10
#p=[3, 2, 5, 4, 9, 6, 1, 8, 7, 10]
#p=[0]+p

now=1
checking=1
while now<=N:

    #print("x",now,checking)

    if p[now]==checking:
        now+=1
        checking=now
        continue
    elif p[now]==now+2:
        checking=now
        while True:
            if now+1<N:
                if p[now+1]!=now+1:
                    print("No")
                    sys.exit()

            if now+2<N:
                #print("b",now)
                if p[now+2]==now+4:
                    now+=2
                elif p[now+2]==checking:
                    now+=3
                    checking=now
                    #print("a",now)
                    break
                else:
                    #print("c",now)
                    if now+4>=N:
                        print("No")
                        sys.exit()

                    if p[now+4]!=checking:
                        print("No")
                        sys.exit()

                    now+=2
                    if (p[now]-now)%2==1:
                        print("No")
                        sys.exit()
                    else:
                        for i in range(now+1,p[now],2):
                            if p[i]!=i:
                                print("No")
                                sys.exit()

                        for i in range(now+4,p[now],2):
                            if p[i]!=i-2:
                                print("No")
                                sys.exit()

                        now,checking=p[now],p[now]-2
                        break


    else:
        if (p[now]-now)%2==1:
            print("No")
            sys.exit()
        else:
            for i in range(now+1,p[now],2):
                if p[i]!=i:
                    print("No")
                    sys.exit()

            for i in range(now+2,p[now],2):
                if p[i]!=i-2:
                    print("No")
                    sys.exit()

            #if p[p[now]-2]==



            now,checking=p[now],p[now]-2




print("Yes")

Submission status: Wrong Answer

Solution code:
import sys

N=int(input())
p=[0]+[int(input()) for i in range(N)]

now=1
checking=1
while now<=N:
    if p[now]==checking:
        now+=1
        checking+=1
        continue
    elif p[now]==now+2:
        if now+1<=N:
            if p[now+1]==now+1:
                checking=now
                now+=2
                
                continue
            else:
                print("No")
                sys.exit()
    else:
        if (p[now]-now)%2==1:
            print("No")
            sys.exit()
        else:
            for i in range(now+1,p[now],2):
                if p[i]!=i:
                    print("No")
                    sys.exit()

            for i in range(now+2,p[now],2):
                if p[i]!=i-2:
                    print("No")
                    sys.exit()

            now=p[now]
            checking=p[now]

print("Yes")

Submission status: Wrong Answer

Solution code:
N = int(input())
ret = "Yes"
isOdd = 1
for __ in range(N):
  if int(input())%2 != isOdd:
  	ret = "No"
  isOdd = 1 - isOdd
print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [0]*N
for i in range(N):
    a[i] = int(input())

b = a[0::2]
c = a[1::2]
b.sort()
c.sort()

B = 0
C = 0
for i in range(1,N+1):
    if i % 2 == 1:
        if b[B] != i:
            print("No")
            exit(0)
        else:
            B += 1
    else:
        if c[C] != i:
            print("No")
            exit(0)

        else:
            C += 1

print("Yes")

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [0]*N
for i in range(N):
    a[i] = int(input())

b = a[0::2]
c = a[1::2]
b.sort()
c.sort()

for B,C in zip(b,c):
    if B > C:
        print("No")
        exit(0)

print("Yes")

Submission status: Wrong Answer

Solution code:
def Split(a):
    no = []
    for i, x in a:
        if no:
            is_ok = i == x
            la_ok = no[-1][0] == no[-1][1]
            if is_ok == la_ok:
                yield no
                no = []
        no.append((i, x))
    yield no

n = int(input())
p = list(enumerate((int(input()) for i in range(n)), 1))
for sq in Split(p):
    if len(sq) == 1 and sq[0][0] != sq[0][1]:
        print("No")
        exit(0)
    tl = tr = 0
    for i, x in sq:
        if i > x:
            if x < tr or i > sq[-1][0]:
                print("No")
                exit(0)
            tr = x
        if i < x:
            if x < tl or i < sq[0][0]:
                print("No")
                exit(0)
            tl = x
print("Yes")

Submission status: Wrong Answer

Solution code:
def Split(a):
    no = []
    for i, x in a:
        if no:
            is_ok = i == x
            la_ok = no[-1][0] == no[-1][1]
            if is_ok == la_ok:
                yield no
                no = []
        no.append((i, x))
    yield no

n = int(input())
p = list(enumerate((int(input()) for i in range(n)), 1))
for sq in Split(p):
    if len(sq) == 1 and sq[0][0] != sq[0][1]:
        print("No")
        exit(0)
    tr = sq[0][0]
    tl = sq[-1][0]
    for i, x in sq:
        if i > x:
            if x < tr or i > sq[-1][0]:
                print("No")
                exit(0)
            tr = x
        if i < x:
            if x > tl or i < sq[0][0]:
                print("No")
                exit(0)
            tl = x
print("Yes")

Submission status: Wrong Answer

Solution code:
def Split(a):
    no = []
    for i, x in a:
        if no:
            is_ok = i == x
            la_ok = no[-1][0] == no[-1][1]
            if is_ok == la_ok:
                yield no
                no = []
        no.append((i, x))
    yield no

n = int(input())
p = list(enumerate((int(input()) for i in range(n)), 1))
for sq in Split(p):
    if len(sq) == 1 and sq[0][1] == 0:
        print("No")
        exit(0)
    tr = sq[0][0]
    tl = sq[-1][0]
    for i, x in sq:
        if i > x:
            if x < tr or i > sq[-1][0]:
                print("No")
                exit(0)
            tr = x
        if i < x:
            if x > tl or i < sq[0][0]:
                print("No")
                exit(0)
            tl = x
print("Yes")

Submission status: Wrong Answer

Solution code:
import bisect
def getReverseNum(nums):
    if len(nums) <= 1:
        return nums, 0
    if len(nums) == 2:
        return ([nums[1], nums[0]], 1) if nums[0] > nums[1] else (nums, 0)

    leftNum, left = getReverseNum(nums[:len(nums) // 2])
    rightNum, right = getReverseNum(nums[len(nums) // 2:])

    #print(leftNum, left, "left")
    #print(rightNum, right, "right")
    res = left + right
    resNum = []
    leftP, rightP = 0, 0
    while leftP < len(leftNum) and rightP < len(rightNum):
        if leftNum[leftP] < rightNum[rightP]:
            resNum.append(leftNum[leftP])
            leftP += 1
            res += rightP
        else:
            resNum.append(rightNum[rightP])
            rightP += 1
            #res += (rightP + 1)
        #print(resNum[-1], res)

    resNum += leftNum[leftP:] if leftP < len(leftNum) else []
    res += len(rightNum) * (len(leftNum) - leftP) if leftP < len(leftNum) else 0
    resNum += rightNum[rightP:] if rightP < len(rightNum) else []
    return resNum, res

def bi(nums, res):
    for x in reversed(nums):
        pos = bisect.bisect_left(res, x)
        if pos == len(res):
            res.append(x)
        else:
            res[pos] = x
        if len(res) > 3:
            print("No")
            return
    print("Yes")
if __name__ == "__main__":
    N = int(raw_input())
    nums = []
    for i in range(N):
        nums.append(int(raw_input()))

    _, reverseNum = getReverseNum(nums)
    #print(reverseNum)
    if reverseNum % 3 != 0:
        print("No")
    else:
        bi(nums, [])

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
ans = "Yes"
for i in range(1,N+1):
    n = int(input().strip())
    if(n%2 != i%2):
        ans = "No"
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
def getReverseNum(nums):
    if len(nums) <= 1:
        return nums, 0
    if len(nums) == 2:
        return ([nums[1], nums[0]], 1) if nums[0] > nums[1] else (nums, 0)

    leftNum, left = getReverseNum(nums[:len(nums) // 2])
    rightNum, right = getReverseNum(nums[len(nums) // 2:])

    #print(leftNum, left, "left")
    #print(rightNum, right, "right")
    res = left + right
    resNum = []
    leftP, rightP = 0, 0
    while leftP < len(leftNum) and rightP < len(rightNum):
        if leftNum[leftP] < rightNum[rightP]:
            resNum.append(leftNum[leftP])
            leftP += 1
            res += rightP
        else:
            resNum.append(rightNum[rightP])
            rightP += 1
            #res += (rightP + 1)
        #print(resNum[-1], res)

    resNum += leftNum[leftP:] if leftP < len(leftNum) else []
    res += len(rightNum) * (len(leftNum) - leftP) if leftP < len(leftNum) else 0
    resNum += rightNum[rightP:] if rightP < len(rightNum) else []
    return resNum, res

if __name__ == "__main__":
    N = int(raw_input())
    nums = []
    for i in range(N):
        nums.append(int(raw_input()))

    _, reverseNum = getReverseNum(nums)
    #print(reverseNum)
    if reverseNum % 3 != 0:
        print("No")
    else:
        res = []
        for x in reversed(nums):
            pos = bisect.bisect_left(res, x)
            if pos == len(res):
                res.append(x)
            else:
                res[pos] = x
        if len(res) > 3:
            print("No")
        else:
            print("Yes")

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
ans = "Yes"
for i in range(1,N+1):
    n = int(input().strip())
    if(n%2 != i%2):
        ans = "No"

print(ans)

Submission status: Wrong Answer

Solution code:

def getReverseNum(nums):
    if len(nums) <= 1:
        return nums, 0
    if len(nums) == 2:
        return ([nums[1], nums[0]], 1) if nums[0] > nums[1] else (nums, 0)

    leftNum, left = getReverseNum(nums[:len(nums) // 2])
    rightNum, right = getReverseNum(nums[len(nums) // 2:])

    #print(leftNum, left, "left")
    #print(rightNum, right, "right")
    res = left + right
    resNum = []
    leftP, rightP = 0, 0
    while leftP < len(leftNum) and rightP < len(rightNum):
        if leftNum[leftP] < rightNum[rightP]:
            resNum.append(leftNum[leftP])
            leftP += 1
            res += rightP
        else:
            resNum.append(rightNum[rightP])
            rightP += 1
            #res += (rightP + 1)
        #print(resNum[-1], res)

    resNum += leftNum[leftP:] if leftP < len(leftNum) else []
    res += len(rightNum) * (len(leftNum) - leftP) if leftP < len(leftNum) else 0
    resNum += rightNum[rightP:] if rightP < len(rightNum) else []
    return resNum, res

if __name__ == "__main__":
    N = int(raw_input())
    nums = []
    for i in range(N):
        nums.append(int(raw_input()))

    _, reverseNum = getReverseNum(nums)
    #print(reverseNum)
    if reverseNum % 3 == 0:
        print("Yes")
    else:
        print("No")

Submission status: Wrong Answer

Solution code:
import sys

N=int(input())
p=[int(input()) for i in range(N)]

s=list(range(1,N+1))

i=1
while i<N:
    x=p.index(i)

    #print(x)

    
    if i==x+1:
        i+=1
        continue
    elif x<i-1:
        break

    
    if (x-i+1)%2!=0:
        
        break

    #print(s,i,x)

    for j in range(i-1,x,2):
        s[j],s[j+2]=s[j+2],s[j]
        i=x+2
    #print(s,i)

if s==p:
    print("Yes")
    sys.exit()

#print(s)

s=list(range(1,N+1))
i=N
while i>0:
    x=p.index(i)
    if i==x+1:
        i-=1
        continue
    elif x>i-1:
        break

    
    if (x-i-1)%2!=0:
        
        break

    #print(s,i,x)

    for j in range(i-1,x,-2):
        s[j],s[j-2]=s[j-2],s[j]
        i=x
    #print(s,i)

if s==p:
    print("Yes")
    sys.exit()

else:
    print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
arr=[0]*(n+1)
for i in range(1,n+1):
	arr[i] = int(input())
suma = 0
flag=0
for i in range(1,n+1):
	#if i>arr[i]:
	if i-arr[i] & 1 ==1:
		flag=1
		break
	suma+=(i-arr[i])
#print(flag,suma)
if flag==1:
	print("No")
elif flag==0 and suma==0:
	print("Yes")
else:
	print("No")

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
P = [0] + list(map(int,read().split()))

# permutationになっている極小区間に分ける

intervals = []
left = 0
right = -1
for i,p in enumerate(P):
    if right < p:
        right = p
    if i == right:
        intervals.append((left,right))
        left = i+1
        right = -1

def check(L,R):
    if L == R:
        return True
    to_left = []
    fixed = []
    to_right = []
    for i,p in enumerate(P[L:R+1],L):
        if i > p:
            to_left.append(p)
        elif i == p:
            fixed.append(p)
        else:
            to_right.append(p)
            
    if fixed != list(range(L+1,R+1,2)):
        return False
    if any(x > y for x,y in zip(to_left, to_left[1:])):
        return False
    if any(x > y for x,y in zip(to_right, to_right[1:])):
        return False
    return True

answer = 'Yes' if all(check(L,R) for L,R in intervals) else 'No'
print(answer)

Submission status: Accepted

Solution code:
import sys


def solve(ppp):
    section_start = -1
    moved_left_max = 0
    moved_right_max = 0
    prev = True

    for i, p in enumerate(ppp, start=1):
        if i == p:
            if prev:
                moved_left_max = 0
                moved_right_max = 0
                section_start = -1
            prev = True
        else:
            if not prev:
                if moved_left_max > i - 1:
                    return False

                moved_left_max = 0
                moved_right_max = 0
                section_start = i

            if section_start == -1:
                section_start = i

            if i > p:
                if section_start > p:
                    return False
                if moved_right_max > p:
                    return False
                moved_right_max = p
            else:
                if moved_left_max > p:
                    return False
                moved_left_max = p

            prev = False
    return True


n, *ppp = map(int, sys.stdin)
print('Yes' if solve(ppp) else 'No')

Submission status: Accepted

Solution code:
def Split(a):
    no = []
    for i, x in a:
        if no and (i == x) == (no[-1][0] == no[-1][1]):
            yield no
            no = []
        no.append((i, x))
    yield no

for sq in Split((i + 1, int(input())) for i in range(int(input()))):
    tb = [0, 0]
    for np, goal in sq:
        if goal != np:
            if goal < tb[np < goal] or goal > sq[-1][0] or goal < sq[0][0]:
                print("No")
                exit(0)
            tb[np < goal] = goal
print("Yes")

Submission status: Accepted

Solution code:
def Split(a):
    no = []
    for i, x in a:
        if no:
            is_ok = i == x
            la_ok = no[-1][0] == no[-1][1]
            if is_ok == la_ok:
                yield no
                no = []
        no.append((i, x))
    yield no

n = int(input())
p = list(enumerate((int(input()) for i in range(n)), 1))
for sq in Split(p):
    tl = tr = 0
    for np, goal in sq:
        if goal > np:
            if goal < tr or goal > sq[-1][0]:
                print("No")
                exit(0)
            tr = goal
        elif goal < np:
            if goal < tl or goal < sq[0][0]:
                print("No")
                exit(0)
            tl = goal
print("Yes")

Submission status: Runtime Error

Solution code:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    int n;
	cin>>n;
	int p[300030];
	for(int i=0; i<n; i++){
		cin>>p[i];
		p[i]--;
	}
	for(int i=0; i<n; i++){
		if((p[i]&1)!=(i&1)){
			cout<<"No"<<endl;
			return 0;
		}
	}
	set<int> st[2];
	for(int i=0; i<n; i++) st[i&1].insert(i);
	int x[2]={}, y[2]={};
	for(int i=0; i<n; i++){
		int a=*st[i&1].begin();
		if(p[i]==a){
			st[i&1].erase(st[i&1].begin());
			continue;
		}
		if(p[i]<x[i&1]){
			cout<<"No"<<endl;
			return 0;
		}
		st[i&1].erase(p[i]);
		x[(i&1)^1]=max(x[(i&1)^1], p[i]+3);
		x[i&1]=max(x[i&1], p[i]+2);
		y[(i&1)^1]=max(y[(i&1)^1], p[i]+1);
	}
	cout<<"Yes"<<endl;
	return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
n = int(input())
p = [int(input()) for _ in range(n)]
odd = p[::2]
even = p[1::2]
if all(x % 2 == 1 for x in odd) and all(x % 2 == 0 for x in even):
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
N = int(input())
ret = "Yes"
isOdd = 1
for __ in range(N):
  if int(input())%2 != isOdd
  	ret = "No"
  isOdd = 1 - isOdd
print(ret)

Submission status: Runtime Error

Solution code:
N = int(input())
ret = "Yes"
isOdd = 1
for __ in range(N):
  if int(input)%2 != isOdd
  	ret = "No"
  isOdd = 1 - isOdd
print(ret)