Problem:


Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.



Sample input:
5
5 2 3 4 1


Sample output:
YES

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
answer = []
diff = 0
diff_1 = -1
diff_2 = -1

index = 0

for i in p:
    if index == 0:
        index += 1
        continue
    
    if p[index] < p[index-1]:
        if diff >= 2:
            print ("NO")
            exit()
        diff += 1

        if diff == 1:
            diff_1 = index-1
        else:
            diff_2 = index

    index += 1

if diff_1 == -1:
    print("YES")
    exit()

if diff_1 != -1 and diff_2 == -1:
    print("NO")
    exit()

diff_1_1 = p[diff_1]
p[diff_1] = p[diff_2]
p[diff_2] = diff_1_1

index = 0

for i in p:
    if index == 0:
        index += 1
        continue
    
    if p[index] < p[index-1]:
        print ("NO")    
        exit()

    index += 1

print("YES")

Submission status: Accepted

Solution code:
N=int(input())
n=list(map(int,input().split()))
cnt=0
ans="YES"
c=""
for i in range(N):
    if n[i] != i+1:
        if cnt == 0:
            c=n[i]
        elif cnt >= 2:
            ans="NO"
            break
        else:
            if c != i+1:
                ans="NO"
                break
        cnt+=1

print(ans)

Submission status: Accepted

Solution code:
import copy
N = int(input())
p = list(map(int, input().split()))

is_asc = True

for k in range(N-1):
  if p[k] > p[k+1]: is_asc = False

if is_asc == True:
    print('YES')
else:
  for i in range(N):
      for j in range(N):
          if i == j: continue
          is_asc = True
          tmp_p = copy.deepcopy(p)
          tmp = p[i]
          tmp_p[i] = p[j]
          tmp_p[j] = tmp
          for k in range(N-1):
              if tmp_p[k] > tmp_p[k+1]: is_asc = False
          if is_asc == True:
              break
      if is_asc == True:
          break

  if is_asc == True:
      print('YES')
  else:
      print('NO')

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
A = []
for i in range(N-1):
    if P[i]>P[i+1]:
        A.append([(P[i],i),(P[i+1],i+1)])
l = len(A)
if l > 2 or l == 1:
    print("NO")
elif l == 0:
    print("YES")
elif l == 2:
    a = max(A[0])
    b = min(A[1])
    P[a[1]],P[b[1]] = b[0],a[0]
    for j in range(N-1):
        if P[j] > P[j+1]:
            print("NO")
            j = "j"
            break
    if j != "j":
        print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)

cnt = 0
for i in range(n):
    if p[i] != q[i]:
        cnt += 1
    if cnt > 2:
        print('NO')
        exit()

print('YES')

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))

flag = []
for i in range(N-1):
  if P[i] > P[i+1]:
    flag.append(i)

if len(flag) > 2:
  print('NO')
if len(flag) == 2:
  A = P[flag[0]]
  B = P[flag[1]+1]
  P[flag[0]] = B
  P[flag[1]+1] = A
  for i in range(N-1):
    if P[i] > P[i+1]:
      print('NO')
      exit()
  print('YES')
  exit()
if len(flag) == 1:
  A = P[flag[0]]
  B = P[flag[0]+1]
  P[flag[0]] = B
  P[flag[0]+1] = A
  for i in range(N-1):
    if P[i] > P[i+1]:
      print('NO')
      exit()
  print('YES')
  exit()
if len(flag) == 0:
  print('YES')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

N = int(input())
P = list(map(int, input().split()))
P_ = sorted(P)

cnt = 0
for i in range(N):
    if P[i] != P_[i]: cnt+=1

if cnt == 2 or cnt == 0: print("YES")
else: print("NO")

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
p = [int(i) for i in input().split()]
q = [i for i in range(1, N+1)]
p = np.array(p)
q = np.array(q)
r = np.array(p-q)
num =  len(np.where(r==0)[0])
print("YES" if N - num == 2 or N == num else "NO")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
p = list(map(int, input().split()))
ans = 0
for i in range(n):
    if i + 1 == p[i]:
        continue
    else:
        ans += 1
        if ans > 2:
            break
if ans <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(input())
list1 = list(map(int, input().split()))
count = 0
 
for i in range(N-1):
  if list1[i] > list1[i+1]:
    count += 1

if count == 0:
  print("YES")
elif count == 1:
  count = 0
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      x = i
  a = list1[x] 
  b = list1[x + 1]
  list1[x] = b
  list1[x + 1] = a
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      count += 1
  if count == 0:
    print("YES")
  else:
    print("NO")
elif count >= 3:
  print("NO")
else:
  count = 0
  list2 = []
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      list2.append(i)
  a = list1[list2[0]] 
  b = list1[list2[1] + 1]
  list1[list2[0]] =b
  list1[list2[1] + 1] =a
  for i in range(N-1):
    if list1[i] > list1[i+1]:
      count += 1
  if count == 0:
    print("YES")
  else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)

cnt = 0

for i in range(N):
    if p[i] != sorted_p[i]:
        cnt += 1


if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = [int(x) for x in input().split()]
B = list(range(1,N+1))

def test(i,j):
  AA = A.copy()
  AA[i], AA[j] = AA[j], AA[i]
  return AA == B

bl = (A == B)
for i,j in itertools.combinations(range(N),2):
  if test(i,j):
    bl = True
answer = 'YES' if bl else 'NO'
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))

index_list = []
count = 0
for i in range(n-1):
    if p[i] >= p[i+1]:
        if count == 0:
            index_list.append(i)
            count += 1
        elif count == 1:
            index_list.append(i+1)
            count += 1
        else:
            break

if count == 0:
    print("YES")
elif count == 2:
    tmp = p[index_list[0]]
    p[index_list[0]] = p[index_list[1]]
    p[index_list[1]] = tmp

    count = 0
    for i in range(n-1):
        if p[i] >= p[i+1]:
            print("NO")
            count += 1
            break
        else:
            pp = p[i]
    if count == 0:
        print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
ans = "YES"
for i in range(N-1):
    if p[i] > p[i+1]:
        ans ="NO"
        break

if ans != "YES":
    for i in range(N-1):
        for j in range(i+1,N):
            pcopy = p.copy()
            pcopy[i],pcopy[j] = pcopy[j],pcopy[i]
            flg = True
            for k in range(N-1):
                if pcopy[k] > pcopy[k+1]:
                    flg = False
            if flg:
                ans ="YES"
                break        

print(ans)

Submission status: Accepted

Solution code:
import copy
def ls_to_int (ls):
	ls_int = []
	for i in ls:
		ls_int.append(int(i))
	return ls_int

def judge(ls):
	is_sorted = True
	for i in range(len(ls)):
		for j in range(i, len(ls)):
			if ls[i] > ls[j] :
				is_sorted = False
				break
	return is_sorted

N = int(input())
p = input().split()
p = ls_to_int(p)
memory = copy.deepcopy(p)
is_able = False
for i in range(len(p)):
	for j in range(len(p)):
		temp = p[i]
		p[i] = p[j]
		p[j] = temp
		if judge(p):
			is_able = True
			break
		p = copy.deepcopy(memory)
	if is_able:
		break
if is_able:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(n):
  if p[i] != i+1:
    count += 1
if count == 0 or count == 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
ps=np.array(list(int(i) for i in input().split())  )

arr=np.arange(1,N+1,1)

flag=np.sum(ps==arr)

if flag>=N-2:
 print("YES")
else:
 print("NO")

Submission status: Accepted

Solution code:
N = int(input())
P = (int,input().split())

sortList = []
for i in range(1,N+1):
        sortList.append(i)

count = 0
for i in range(N):
        # print(P[1][i], sortList[i])
        if (int(P[1][i]) != int(sortList[i])):
                count += 1

if(count == 2 or count == 0):
        print('YES')
else:
        print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))

k = []
for i in range(len(p)):
    if p[i] != i+1:
        k.append(i)
if len(k) == 2 or len(k) == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
a = int(input())
ay = list(map(int, input().split()))
count = 0 

for i in range(0,a):
  if ay[i]  != i + 1:
    count += 1

if count == 0 or count == 2 :
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n=int(input())  
c=list(map(int, input().split()))  
a = 0
b=sorted(c)

for i in range(len(c)):
    if c[i]!=b[i]:
        a+=1
    
if a==0 or a==2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
P = sorted(p)
count = 0
for i in range(n):
    if p[i] != P[i]:
        count += 1
print("YES") if count <= 2 else print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
pp = sorted(p)
c = 0
for i in range(N):
    if p[i] != pp[i]:
        c += 1
print("YES" if c <= 2 else "NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))

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

diff = 0

for i in range(N):
    if p[i] != P[i]:
        diff += 1

if diff <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))

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

d = [0 if p == e else 1 for p, e in zip(P, E)]

if sum(d) == 0 or sum(d) == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(input())
p = [int(_) for _ in input().split()]

P = sorted(p)
cnt = 0
for a, b in zip(p, P):
    if a != b:
        cnt += 1

    if cnt > 2:
        print("NO")
        exit()

print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))

count = 0

for i in range(n):
    if p[i] != i+1:
        count += 1

if count == 0 or count == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
# coding=utf-8

if __name__ == "__main__":
    N = int(input())
    p_num = list(map(int, input().split()))

    is_change = [el1 for (el1, el2) in zip(p_num, range(1, len(p_num)+1)) if el1 != el2]

    if len(is_change) <= 2:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
N = int(input())
l = [int(i) for i in input().split()] 
ll = sorted(l)

c = 0
for i in range(len(l)):
    if (l[i] != ll[i]):
        c += 1

if c > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
import sys
N = int(input())
l = list(map(int, input().split()))

flg = 0
for i in range(N):
    if i+1 != l[i]:
        flg += 1
        l[l[i]-1] = l[i]
    if flg >= 2:
        print('NO')
        sys.exit()
print('YES')

Submission status: Accepted

Solution code:
n = input()
puts = input()
Data = puts.split()
offset = 0

for i in range(int(n)):
	if (i+1 != int(Data[i])):
		if offset != 2:
			offset +=1
		else:
			print('NO')
			exit()

print('YES')

Submission status: Accepted

Solution code:
N = int(input())
P = input().split()
P = [int(s) for s in P]
T = range(1, N+1)
count=[]
for i in range(N):
  if P[i] != T[i]:
    count.append(i)
if len(count) != 2 and len(count) != 0:
  print("NO")
elif len(count) == 0:
  print("YES")
else:
  P[count[0]], P[count[1]] = P[count[1]], P[count[0]]
  if list(T) == P:
    print("YES")
  else:
    print("NO")

Submission status: Accepted

Solution code:



n = int(input())
p = list(map(int, input().split(' ')))

a=0
p2 = sorted(p)

for i in range(n):
    if p[i] != p2[i]:
        a += 1
if(a <= 2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N=int(input())
P=[int(i) for i in input().split()]
cnt=0
for i in range(N):
	if P[i]!=i+1:cnt+=1

if cnt==0:
	print("YES")
elif cnt==2:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
import copy

N = int(input()) 
s = list(map(int, input().split()))
s2 = copy.deepcopy(s)
s.sort()
n = 0
for i in range(0, len(s)):
    if s[i] - s2[i] != 0:
        #print(s[i], s2[i])
        n+=1

if n == 2:
  print("YES")
elif n == 0:
    print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
s = list(map(int, input().split()))
new_s = sorted(s)
count = 0

for i in range(N):
    if s[i]-new_s[i]!=0:
        count += 1

if count>2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
n = int(input())
p = [int(s) for s in input().split()]

num_buf = []
idx_buf = []

for i in range(n):
    if i+1 != p[i]:
        num_buf.append(p[i])
        idx_buf.append(i)


if len(idx_buf)==0:
    print("YES")
elif len(idx_buf) == 2 and num_buf[0] == idx_buf[1] + 1:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:

N = int(input())

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

dis = True
count = 0
for i in range(0, N-1):
    if A[i] > A[i+1]:
        count += 1
        dis = False
        if count == 1:
            tmp1 = i
        elif count == 2:
            tmp2 = i+1

if dis == False and count > 1:
    tmp = A[tmp1]
    A[tmp1] = A[tmp2]
    A[tmp2] = tmp

    count = 0
    for i in range(0, N-1):
        if A[i] > A[i + 1]:
            count += 1
            dis = False
            if count == 1:
                tmp1 = i
            elif count == 2:
                tmp2 = i + 1
    if count == 0:
        dis = True

if dis == True:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import copy
n=int(input())
p=list(map(int,input().split()))
sortp=sorted(p)
cnt=0
for i in range(n):
    if p[i]==sortp[i]: cnt+=1
if cnt==n or cnt==n-2:
    print("YES")
else: print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))

ct = 0
for i in range(n):
    if p[i]-1 != i:
        ct += 1

print('YES' if ct <= 2 else 'NO')

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
sortedL = sorted(L)
count = 0

for i in range(N):
    if L[i] != sortedL[i]:
        count += 1

if count > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
LARGE = 10 ** 9 + 7


def solve_a(test=False, a=2, b=16):

    if not test:
        a, b = map(int, input().split())
    if (b - a) % 2 == 0:
        print((a + b) // 2)
    else:
        print("IMPOSSIBLE")


def solve_b(test=False, n=5, p_list=[5, 2, 3, 4, 1]):

    if not test:
        n = int(input())
        p_list = list(map(int, input().split()))

    r = 0
    for i in range(n):
        if p_list[i] != i + 1:
            r += 1
    if r == 0 or r == 2:
        print("YES")
    else:
        print("NO")


def solve_c(test=False, n=2, a_list=[3, 5, 2], b_list=[4, 5]):

    if not test:
        n = int(input())
        a_list = list(map(int, input().split()))
        b_list = list(map(int, input().split()))

    res = 0
    for i in range(n):
        if b_list[i] <= a_list[i]:
            res += b_list[i]
            a_list[i] -= b_list[i]
        elif b_list[i] < a_list[i] + a_list[i + 1]:
            res += b_list[i]
            a_list[i + 1] -= b_list[i] - a_list[i]
            a_list[i] = 0
        else:
            res += a_list[i] + a_list[i + 1]
            a_list[i] = 0
            a_list[i + 1] = 0

    print(res)


def solve_d(test=False, s='??2??5'):

    if not test:
        s = input()

    l = len(s)
    res_list_new = [0] * 13
    res_list_old = [0] * 13
    zero_mod = int(s.replace('?', '0')) % 13
    res_list_new[zero_mod] += 1

    for i in range(1, l + 1):
        if s[-i] == '?':
            for k in range(13):
                res_list_old[k] = res_list_new[k] % LARGE
            base_mod = pow(10, i-1, 13)
            for j in range(1, 10):
                for k in range(13):
                    res_list_new[(k + base_mod * j) % 13] += res_list_old[k]

    print(res_list_new[5] % LARGE)


# solve_a(test=True, a=2, b=16)
# solve_a(test=True, a=0, b=3)
# solve_a(test=True, a=998244353, b=99824435)

# solve_b(test=True, n=5, p_list=[5, 2, 3, 4, 1])
# solve_b(test=True, n=5, p_list=[2, 4, 3, 5, 1])
# solve_b(test=True, n=7, p_list=[1, 2, 3, 4, 5, 6, 7])

# solve_c(test=True, n=2, a_list=[3, 5, 2], b_list=[4, 5])
# solve_c(test=True, n=3, a_list=[5, 6, 3, 8], b_list=[5, 100, 8])
# solve_c(test=True, n=2, a_list=[100, 1, 1], b_list=[1, 100])

# solve_d(test=True, s='??2??5')
# solve_d(test=True, s='?44')
# solve_d(test=True, s='7?4')
# solve_d(test=True, s='?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???')

solve_b()

Submission status: Accepted

Solution code:
def swap(i, j, arr):
    a = arr[:]
    temp = a[i]
    a[i] = a[j]
    a[j] = temp
    return a

n = int(input())
p = [int(i) for i in input().split()]
a = [int(i) for i in range(1, n+1)]
found = False
if p == a:
    found = True
    print('YES')
else:
    for i in range(n):
        for j in range(i+1, n):
            if swap(i, j, p) == a:
                found = True
                print('YES')
                break
if not found:
    print('NO')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        lines.append(input())
        token = lines[0].split()
        n = int(token[0])
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
        token = lines[0].split()
        n = int(token[0])

    a = [int(i) for i in lines[1].split(" ")]

    return (n, a)


def solve(n, a):

    b = a.copy()
    b.sort()

    if a == b:
        return "YES"

    result = "NO"
    for i in range(0, n):
        for j in range(i+1, n):
            if a[i] > a[j]:
                c = a.copy()
                c[i], c[j] = c[j], c[i]
                if b == c:
                    result = "YES"
                    break

    return result


def main():
    # 出力

    print("%s" % solve(*parse_input()))

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
N = int(input())
i = 0
a = 0
list = input().split()

while i < N:
  if not int(list[i]) == i+1:
    a += 1
  i += 1
if a == 0 or a == 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
p1 = p.copy()
p1.sort()
cnt = 0
for i in range(n):
  if p[i] != p1[i]:
    cnt += 1
    # print(i)
if cnt == 2 or cnt == 0:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys, math, bisect, random, itertools
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if (a + b) % 2:
        print("IMPOSSIBLE")
        return
    print((a + b) // 2)
    return

#B
def B():
    n = II()
    p = LI()
    for j in range(n - 1):
        if p[j] > p[j + 1]:
            break
    else:
        print("YES")
        return
    for i in range(n):
        for k in range(i+1, n):
            pi = p[::1]
            pi[i], pi[k] = pi[k], pi[i]
            for j in range(n-1):
                if pi[j] > pi[j + 1]:
                    break
            else:
                print("YES")
                return
    print("NO")
    return

#C
def C():
    n = II()
    a = LI()
    b = LI()
    ans = 0
    for i in range(n):
        if a[i] >= b[i]:
            ans += b[i]
            continue
        ans += a[i]
        b[i] -= a[i]
        if a[i + 1] >= b[i]:
            ans += b[i]
            a[i + 1] -= b[i]
            continue
        ans += a[i + 1]
        a[i + 1] = 0
    print(ans)
    return

#D
def D():
    s = S()
    s = s[::-1]
    dp = [[0] * 13 for _ in range(len(s))]
    remain = 1
    if s[0] == "?":
        for i in range(10):
            dp[0][i] = 1
    else:
        dp[0][int(s[0])] = 1
    for i in range(1, len(s)):
        remain = (remain * 10) % 13
        dpi = dp[i]
        dpi1 = dp[i-1]
        if s[i] == "?":
            for k in range(10):
                for j in range(13):
                    dpi[(j + remain * k) % 13] += dpi1[j]
                    dpi[(j + remain * k) % 13] %= mod
        else:
            for j in range(13):
                dpi[(j + remain * int(s[i])) % 13] += dpi1[j]
    print(dp[-1][5])
    return

#E
def E():
    return

#F
def F():
    return

#Solve
if __name__ == '__main__':
    B()

Submission status: Accepted

Solution code:
n=int(input())
array=list(map(int, input().split()))
correct=list(range(1,n+1))

incorrect=0
for i in range(n):
    if array[i]!=correct[i]:
        incorrect+=1

if incorrect > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
input()
pp = list(map(int, input().split()))
sorted_pp = sorted(pp)
flag = False
if pp == sorted_pp:
    print('YES')
else:
    for i in range(len(pp) - 1):
        for j in range(len(pp) - 1 - i):
            ppp = pp.copy()
            ppp[i] = pp[j + 1 + i]
            ppp[j + 1 + i] = pp[i]
            if ppp == sorted_pp:
                flag = True
                break
    if flag:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
n = int(input())
plist = list(map(int, input().split()))

slist = sorted(plist)
if plist == slist:
    print("YES")
    exit()
cnt =0
for i in range(1,n+1):
    if cnt >= 3:
        print("NO")
        exit()
    elif i != plist[i-1]:
        cnt += 1
print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p_lst = list(map(int,input().split()))
p_lst_s = sorted(p_lst)
cnt = 0
for i in range(n):
	if p_lst[i] != p_lst_s[i]:
		cnt += 1


print("YES" if cnt == 2 or cnt == 0 else "NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
sortp = sorted(p)
sa = 0

for i in range(n):
  if sortp[i] != p[i]:
    sa += 1

if sa > 2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
x = int(input())
li = list(map(int,input().split()))#listp
li_o = list(range(1,x))

a = 0
for i in range(x-1):
	
	b = 0
	#print (li[i])
    
	if int(li[i]) == int(li_o[i]):
		b=b+1
	else:
		a=a+1
	#print(a)
	#print('endofloop')
    
#print(a)

if a <= 2 :
	print('YES')
else:
	print('NO')

Submission status: Accepted

Solution code:
N = int(input())
lis = list(map(int, input().split()))
list_asc = sorted(lis)
counts = 0
for i in range(0, len(lis)):
  if lis[i] != i + 1:
    counts += 1
  

if counts == 2 or counts == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

tmp = [0]*n

for i in range(len(p)):
    if i+1 == p[i]:
        tmp[i] = 0
    else:
        tmp[i] = 1

ans = sum(tmp)

if ans == 0 or ans == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:

def input_from_console():
    n = int(input())
    p_list = list(map(int, input().split()))
    return n, p_list


def solve(n, p_list):
    p_list_sorted = sorted(p_list)
    diff_count = 0
    for i in range(len(p_list)):
        if p_list_sorted[i] != p_list[i]:
            diff_count += 1

    if diff_count > 2:
        return 'NO'
    return 'YES'


def main():
    n, p_list = input_from_console()
    print(solve(n, p_list))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main()

Submission status: Accepted

Solution code:
import copy
n=int(input())
p=list(map(int,input().split()))
newp=copy.copy(p)
p.sort()
cnt=0
for i in range(n):
    if p[i]!=newp[i]:
        cnt+=1
if cnt==2 or cnt==0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split())) 
counter = 0

for i in range(len(l)):
  if i+1 != l[i]:
    counter += 1
if counter == 2 or counter == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import copy
n=int(input())
in_L=list(map(int,input().split()))
cnt=0
tmp_L=copy.copy(in_L)
sort_L=sorted(in_L)

for i in range(n):
    for j in range(n-i):
        tmp_L[i],tmp_L[i+j]=tmp_L[i+j],tmp_L[i]
        if tmp_L==sort_L:
            cnt=True
            break
        else:
            tmp_L=copy.copy(in_L)
            
            
        
if cnt:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
flag = False
for i in range(n):
    for j in range(n):
        p2 = p.copy()
        p2[i], p2[j] = p2[j], p2[i]
        if sorted(p) == p2:
            flag = True
print("YES") if flag else print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split()))
q= []
count = 0
for i in range(N-1):
    if p[i]>p[i+1]:
        count += 1
        q.append(i)
if count == 0:
    print("YES")
elif count ==2:
    if q[0] == 0 and q[1]+1 ==N-1:
        if p[q[1]+1]<p[q[0]+1] and p[q[1]]<p[q[0]]:
            print("YES")
        else:
            print("NO")
    elif q[0] == 0:
        if p[q[1]+1]<p[q[0]+1] and p[q[1]]<p[q[0]] and p[q[0]]<p[q[1]+2]:
            print("YES")
        else:
            print("NO")
    elif q[1]+1 == N-1:
        if p[q[0]-1]<p[q[1]+1] and p[q[1]+1]<p[q[0]+1] and p[q[1]]<p[q[0]]:
            print("YES")
        else:
            print("NO")
    else:
        if p[q[0]-1]<p[q[1]+1] and p[q[1]+1]<p[q[0]+1] and p[q[1]]<p[q[0]] and p[q[0]]<p[q[1]+2]:
            print("YES")
        else:
            print("NO")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
c = 0
p1 = sorted(p)

for i in range(n):
    if(p1[i]==p[i]):
        continue
    else:
        c=c+1

if c == 2 or c==0:
    ans = 'YES'
else:
    ans = 'NO'
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
x = list(map(int, input().split()))
y = list(range(1,N+1))

cnt = 0
for i in range(N):
  if x[i] != y[i]:
    cnt += 1
    
if cnt > 2:
  print('NO', end='')
else:
  print('YES', end='')

Submission status: Accepted

Solution code:
N = int(input())
p = list(int(x) for x in input().split())
a = 0
for k in range(N):
    k = k + 1
    if k != p[k - 1]:
        a = a + 1
    if a == 3:
        print("NO")
        break
if k == N:
    print("YES")

Submission status: Accepted

Solution code:
import copy
a = int(input())
l = [int(x) for x in input().split()]
m = copy.copy(l)
m.sort()
c = 0

for x in range(a):
    if l[x] != m[x]:
        c += 1

print("YES" if (c <= 2) else "NO")

Submission status: Accepted

Solution code:
import sys

N = int(input())
p = [int(_) for _ in input().split()]

correct = [int(_) for _ in range(1,N+1)]

res = 0
if p == correct:
    res = 1
else:
    for i in range(N):
      for j in range(i+1,N):
        a = p[:]
        c = a[i]
        a[i] = a[j]
        a[j] = c
        if a == correct:
            res = 1

if res == 1:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
lists =[]
for i in range(n):
  lists.append(i+1)
list_2=[int(x)for x in input().split()]

count =0
for s in range(n):
  if lists[s]!=list_2[s]:
    count+=1
if count>2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
s = [i for i in p]
s.sort()
cot = 0
for i in range(n):
    if p[i] != s[i]:
        cot += 1
if cot == 2 or cot == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
P = list(map(int, input().split()))
sorted = sorted(P)
count = 0

for i in range(n):
  if P[i] == sorted[i]:
    count += 1

if count == n or count == n-2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))

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

for i, (x, y) in enumerate(zip(p, q)):
    if x != y:
        if p[x - 1] != (i + 1):
            print("NO")
            quit()
        _ = p[x - 1]
        p[x - 1] = x
        p[i] = _
        break

for i, (x, y) in enumerate(zip(p, q)):
    if x != y:
        print("NO")
        quit()


print("YES")

Submission status: Accepted

Solution code:
import sys

n = int(input())
li = list(map(int, input().split()))

so = sorted(li)
cnt = 0

for l, s in zip(li,so):
 if l!=s:
  cnt += 1
  if cnt > 2:
   print("NO")
   sys.exit()
else:
 print("YES")

Submission status: Accepted

Solution code:
def main():
	import sys
	input = sys.stdin.readline
	N = int(input())
	P = list(map(int, input().split()))
	Q = sorted(P)
	count = 0
	for i in range(N):
		if P[i] != Q[i]:
			count += 1
			if count == 3:
				print('NO')
				sys.exit()
	print('YES')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N =input()
A =[int(i) for i in input().split()]
B =sorted(A)
count = 0
for i in range(len(B)):
    if A[i]!=B[i]:
        count +=1
if count>2:
    print('NO')
else:
    print('YES')