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=[int(x) for x in input().split()]
pp=[int(x+1) for x in range(n)]
 
for i in range(n):
    buf=i+1
    if buf != p[i]:
        p[p.index(buf)]=p[i]
        p[i]=buf
        break
 
if pp==p:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
p=[int(x) for x in input().split()]
pp=[int(x+1) for x in range(n)]

count=0

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

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

Submission status: Accepted

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

l = []
for k in range(1, N+1):
    l.append(k)

count = 0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int, input().split()))
#print(l)
m=sorted(l)
#print(m)
e=0

for i in range(n-1):
    if l[i]!=m[i]:
        e+=1
if e<=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    ok = 1
    flg = 1
    pair = -1
    for i in range(N):
        if P[i] != i+1:
            if flg:
                if P[P[i]-1] == i+1:
                    flg = 0
                    pair = P[i]-1
                else:
                    ok = 0
                    break
            else:
                if i == pair:
                    continue
                ok = 0
                break

    if ok:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

dif = sum(idx != pp for idx, pp in enumerate(p, 1))  # 1からidx
if dif <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

dif = 0
for idx, pp in enumerate(p, 1):  # 1からidx
    dif += (idx != pp)
if dif <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
p = np.array([int(_) for _ in input().split()])
pp = sorted(p)
dif = pp - p
non_zero = len(p)-np.count_nonzero(dif==0)
if (non_zero == 2) or (non_zero == 0):
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
q = sorted(p)

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

Submission status: Accepted

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

p_sort = sorted(p)

ans = 0

for i in range(len(p)):
  if p[i] != p_sort[i]:
    ans += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

cnt = 0
for i in range(len(P)):
    if i+1 != P[i]:
        cnt += 1
        if cnt == 3:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    P = list(map(int, input().split(" ")))
    order = 0
    for i in range(N):
        if P[i] != i + 1:
            order += 1
    if order == 2 or order == 0:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(n):
    p[i]-=1
cnt=0
ans="YES"
for i,p_i in enumerate(p):
    if i==p_i:
        continue
    cnt+=1
    if cnt>1:
        ans="NO"
        break
    for j,p_j in enumerate(p):
        if i==p_j:
            p[j]=p_i
            p[i]=p_j
            break

print(ans)

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  ps = [i for i in map(int, input().split())]
  cou = 0

  for i in range(N):
    if ((i+1) != ps[i]) and cou==0:
      tp = i+1
      cou += 1
    elif ((i+1) != ps[i]) and cou==1:
      if ps[i] != tp:
        return('NO')
      cou += 1
    elif ((i+1) != ps[i]) and cou==2:
      return('NO')
  return('YES')
if __name__ == '__main__':
  print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
num = list(map(int,input().split()))
cnt=0
for i in range(N):
	if not(num[i] == i+1):
		cnt+=1
if(cnt==2 or cnt==0):
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

ans = 0
for a, b in zip(ps, sorted(ps)):
    if a != b:
        ans += 1

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(i) for i in input().split()]
arr = list(range(1, N + 1))
diff = 0
for i in range(N):
    if P[i] != arr[i]:
        diff += 1
if diff >= 3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
cnt = 0

for i, p in enumerate([int(_) for _ in input().split()], 1):
    cnt += i != p
print('YES' if cnt <= 2 else 'NO')

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
k = 0

for i in range(N):
    if A[i] != i+1:
        k += 1

if k <= 2:
    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==2 or count==0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

    diff = 0
    for i in range(N):
        if (i+1) != p[i]:
            diff += 1
    if (diff == 2) | (diff == 0):
        return "YES"
    return "NO"
if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
for i in range(0,N):
  if p[i] != i+1:
    p[p.index(i+1)],p[i] = p[i],p[p.index(i+1)]
    break
if p == sorted(p):
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

notsame = 0
for i in range(N):
    if p[i] != i+1:
        notsame += 1
print('YES' if notsame == 2 or notsame == 0 else 'NO')

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(range(1, n+1)))
if sum(a != b) <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
A = int(input())
B = list(map(int,input().split(" ")))
C = sorted(B)
S = 0
for i in range(A):
    if B[i] != C[i]:
        S += 1
if S == 0 or S == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

p_sorted = sorted(p)

ans = 0

for i in range(n):
  if p_sorted[i] != p[i]:
    ans += 1
  else:
    pass

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
	# a,b,k = map(int,input().split())
	n = int(input())
	# s = str(input())
	a = list(map(int,input().split()))
	# l = [list(map(int, input().split())) for _ in range(n-1)]
	# s = list(s)
	
	a_sorted = sorted(a)
	# print(a)
	# print(a_sorted)
	ans = 0
	for i in range(n):
		if a[i] != a_sorted[i]:
			ans += 1
	if ans == 2 or ans == 0:
		print("YES")
	else:
		print("NO")


	
 
 
if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 0 or 1 Swap

# libraries
import numpy as np

# input
N = int(input())
p = np.array(list(map(int, input().split())))

# solve
ideal = np.sort(p)

if sum(p != ideal) <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main(li):
    count = 0
    for i in range(len(li)):
        if li[i] != i + 1:
            count += 1
    if count in [0, 2]:
        return True
    else:
        False


if __name__ == '__main__':
    n = int(input())
    li = list(map(int, input().split()))
    if main(li):
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int, input().split()))
ans = list()
for i in range(N):
  if i+1 != li[i]:
    ans.append('groot')
if len(ans) <= 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 > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

SortedP = P.copy()
SortedP.sort()
count=0
for i in range(N):
    if(P[i]!=SortedP[i]):
        count = count + 1
    if(count>2):
        print("NO")
        break
else:print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
for i in range(N):
    if not P[i] == i + 1:
        c += 1

    if c > 2:
        print("NO")
        break
else:
    print("YES")

Submission status: Accepted

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

for i in range(len(p) - 1):
    for j in range(i + 1, len(p)):
        if p[i] > p[j]:
            tmp = min(tmp, p[j])

    if tmp != 100:
        for k in range(len(p)):
            if p[k] == tmp:
                p[k], p[i] = p[i], p[k]
                break
        break

if p == sorted(p):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

q=[i for i in range(1, N+1)]

count = 0
for n in range(N):
    if p[n] != q[n]:
        count += 1
if count==0 or count==2:
    print('YES')
else:
    print('NO')

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('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
input()
p = 0
A = list(map(int,input().split()))
a = sorted(A)
for i,j in zip(A,a):
  if i != j:
    p += 1
if p == 0 or p == 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = sorted(A)
c = 0
for i, j in zip(A, B):
    if (i !=j):
        c += 1
if c < 3:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
c = []
for i in range(n):
  c.append(i+1)
  
d = 0
for i in range(n):
  if p[i] != c[i]:
    d += 1

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

Submission status: Accepted

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

for i in range(n):
    if p[i] == sorted(p)[i]:
        tmp += 1

print('YES' if tmp==n or tmp==n-2 else 'NO')

Submission status: Accepted

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

lst = []
for i in range(n):
    lst.append(i+1)

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

print('YES' if tmp==n or tmp==n-2 else 'NO')

Submission status: Accepted

Solution code:
_ = input()
P = [int(i) for i in input().split()]

swap = 0
for i, p in enumerate(P):
    if p != i + 1:
        swap += 1

ans = "YES" if swap <= 2 else "NO"
print(ans)

Submission status: Accepted

Solution code:
_ = input()
P = [int(i) for i in input().split()]
P_sorted = sorted(P)
swap = 0
for p, s in zip(P, P_sorted):
    if p != s:
        swap += 1

ans = "YES" if swap <= 2 else "NO"

print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    p = list(map(int, input().split()))

    c = 0
    for i, v in enumerate(p):
        if (i + 1) != v:
            c += 1
        if c > 2:
            print('NO')
            quit()
    print('YES')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

p_sorted = sorted(p)
count = 0

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

Submission status: Accepted

Solution code:
import copy

def is_increase(arr):
    for i in range(1, len(arr)):
        if arr[i] < arr[i-1]:
            return False
    return True

N = int(input())
p = list(map(int, input().split()))
for i in range(N-1):
    for j in range(i, N):
        p2 = copy.deepcopy(p)
        p2[i], p2[j] = p2[j], p2[i]
        if is_increase(p2):
            print('YES')
            exit(0)
print('NO')

Submission status: Accepted

Solution code:
N, *L,  = [list(map(int, s.split())) for s in open(0)]
A = L[0]

A_sorted = sorted(A)
flag = 0
if A == A_sorted:
    flag += 1
for i in range(len(A) - 1):
    for j in range(i+1, len(A)):
        A_ = A.copy()
        tmp = A_[i]
        A_[i] = A_[j]
        A_[j] = tmp
        if A_sorted == A_:
            flag += 1
if flag:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
li_p = list(map(int, input().split()))
count = 0
flag = True
for i in range(N):
    if li_p[i] != i+1:
        count += 1
        if count > 2:
            flag = False
            break
if flag:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    n = int(input())
    p = list(map(int, input().split()))

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if p == sorted(p):
    print("YES")
    exit()
else:
    for i in range(n):
        for j in range(n):
            if i != j:
                q = p.copy()
                q[i], q[j] = q[j], q[i] 
                if q == sorted(q):
                    print("YES")
                    exit()

print("NO")

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
t=l.copy()
t.sort()
s=0
for i in range(n):
    if t[i]!=l[i]:
        s+=1
if s>2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for i, num in enumerate(a):
  if i+1 != num:
    count += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0; p_sorted = sorted(p)
for i in range(N):
    if p[i] != p_sorted[i]:
        count += 1
    
if count == 2 or count == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

memo = []

for i in range(1, n):
    if p[i - 1] > p[i]:
        memo.append(i)

flag = True

if len(memo) >= 3:
    flag = False
elif len(memo) == 0:
    flag = True
elif len(memo) == 1:
    pp = p
    pp[memo[0] - 1], pp[memo[0]] = pp[memo[0]], pp[memo[0] - 1]
    for i in range(1, n):
        if pp[i - 1] > pp[i]:
            flag = False
            break
elif len(memo) == 2:
    pp = p
    pp[memo[0] - 1], pp[memo[1]] = pp[memo[1]], pp[memo[0] - 1]
    for i in range(1, n):
        if pp[i - 1] > pp[i]:
            flag = False
            break

print('YES') if flag else print('NO')

Submission status: Accepted

Solution code:
n=int(input()) 
P=list(map(int, input().split()))
P_sort= sorted(P)
count = 0
for i in range(n):
    if P[i] != P_sort[i]:
        count+=1
    else:
        pass
if count <=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
if count <=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys

def fi(li):
    for i in range(1,len(li)):
        if li[i-1] > li[i]:
            return False
    return True

def main():
    n=int(input())
    ps=list(map(int,input().split()))
    if fi(ps):
        print("YES")
        return 0
    for i in range(len(ps)):
        for j in range(i,len(ps)):
            if ps[i]>ps[j]:
                ps[i],ps[j]=ps[j],ps[i]
                if fi(ps):
                    print("YES")
                    return 0
                ps[i],ps[j]=ps[j],ps[i]
    print("NO")
    return 0

if __name__ == "__main__":
    sys.exit(main())

Submission status: Accepted

Solution code:
import copy
n = int(input())
l = list(map(int, input().split()))
m = sorted(copy.copy(l))
check = [True] * n
for i in range(n):
    if not (l[i] == m[i]):
        check[i] = False
if check.count(False) <= 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, v in enumerate(p):
    if v != i + 1:
        cnt += 1

print('YES' if cnt in (0, 2) else 'NO')

Submission status: Accepted

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

sorted_numbers = sorted(numbers)

diff = 0
for i in range(N):
    if numbers[i] != sorted_numbers[i]:
        diff += 1
        
if diff == 2 or diff == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

cnt = 0
for i, num in enumerate(p):
    if i + 1 != num:
        cnt += 1
if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
a=int(input())
A=list(map(int,input().split()))
B=[i for i in range(1,a+1)]

cnt=0
for i in range(a):
  if A[i]!=B[i]:
    cnt+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
P=list(map(int,input().split()))
a=0
for p,q in zip(P,sorted(P)):
    if p!=q:
        a+=1
print("YES" if a<3 else "NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from operator import ne

N = int(input())
P = list(map(int, input().split()))
Q = sorted(P)
print("YES" if sum(map(ne, P, Q)) <= 2 else "NO")

Submission status: Accepted

Solution code:
n=int(input())
plist = list(map(int,input().split(" ")))
nlist = list()
count = 0
for i in range(n):
  nlist.append(i+1)
  
for j in range(len(nlist)):
  if plist[j]!=nlist[j]:
    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()))

flg = 0
for i in range(0,len(p)):
    #print(i)
    if (p[i] != i + 1):
        flg += 1
       

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
p = list(map(int, input().split()))
t = copy.deepcopy(p)

for i in range(n):
    for j in range(n):
        t[i], t[j] = t[j], t[i]
        if sorted(t) == t:
            print("YES")
            exit()
        t[j], t[i] = t[i], t[j]
        #t = p


print("NO")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-S
N = int(input())
p = [int(i) for i in input().split()]

q = sorted(p)
X = 0
for i in range (N):
    if p[i] != q[i]:
        X += 1
if X == 2 or X == 0:    #X <= 2 のかわりに　X == 0 or X == 2 でもできる？？
    print("YES")
else:
    print("NO")

#X=1になることはあるの？　なんでWAになったの？？

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
cnt = 0
for num, val in enumerate(p):
    if val != num + 1:
        cnt += 1

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

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int,input().split()))
B = list(reversed(S))
B.sort()
count = 0
for i in range(len(S)):
    if S[i] != B[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()))
q = list(range(1,n+1))
if p == q:
    print("YES")
    exit(0)
cnt = 0
for i in range(n):
    if p[i] != q[i]:
        cnt += 1
        if cnt == 1:
            t = i
        if cnt == 2:
            if p[t] != q[i]:
                print("NO")
                exit(0)
    if cnt > 2:
        print("NO")
        exit(0)
print("YES")

Submission status: Accepted

Solution code:
N=int(input())
P=list(map(int,input().split()))
cnt=0
for n in range(N):
    if P[n]!=n+1:
        cnt+=1
if cnt<=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import copy
n = int(input())
L = list(map(int, input().split()))
flag = 1
if L == sorted(L):
  print('YES')
  exit()
for i in range(n-1):
  for j in range(i+1, n):
    R = copy.copy(L)
    a = L[i]
    b = L[j]
    R[i] = b
    R[j] = a
    if R == sorted(R):
      flag *= 0
if flag == 0:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

count = 0
for i in range(N):
    if p[i] == psort[i]:
        continue
    else:
        count += 1

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

Submission status: Accepted

Solution code:
def at_most_1_swap(N: int, P: list)->bool:
    swapped = 0
    for i, p in enumerate(P):
        if i+1 != p:
            swapped += 1
    return swapped <= 2


if __name__ == "__main__":
    N = int(input())
    P = list(map(int, input().split()))
    ans = at_most_1_swap(N, P)
    print('YES' if ans else 'NO')

Submission status: Accepted

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

sorted_P = sorted(P)

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

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

Submission status: Accepted

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

q = sorted(p)
count = 0
for i in range(len(p)):
    if p[i]!=q[i]:
        count += 1
if count <=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

sum = 0

for i,j in zip(p_list,sorted(p_list)):
  if i != j:
    sum += 1

if sum in [0,2] :
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import sys

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

try:

    if sorted(p_list) == p_list:
        print("YES")
        sys.exit()

    sum = 0

    for i,j in zip(p_list,sorted(p_list)):
        if i != j:
            sum += 1

    if sum == 2:
        print("YES")
    else:
        print("NO")

except EOFError:
    pass

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
false_num=0
for i in range(N):
    if p[i] != i+1:
        
        false_num+=1

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