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.

Solution (Python):
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 == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

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

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

def bubble(input_list):
    cnt = 0
    flg = 0
    for i in range(len(input_list)):
        if i+1 != input_list[i]:
            cnt +=1
            flg = 1
    if flg == 1:
        return False, cnt
    else:
        return True, 0
a, b = bubble(p)

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

Solution (Python):
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")

Solution (Python):
import sys
N = int(input())
P = list(map(int,input().split()))

ans = sorted(P)

if P == ans:
  print("YES")
else:
  for i in range(N-1):
    for j in range(i+1,N):
      check = []
      check += P
      check[i],check[j] = check[j],check[i]
      if check == ans:
        print("YES")
        sys.exit()
  print("NO")

Solution (Python):
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:
    print("YES")
else:
    print("NO")

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

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

Solution (Python):
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==0 or cnt==2:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys

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

pp = [i for i in range(1, n+1)]
cnt = 0

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

print('YES')

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

Solution (Python):




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

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

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

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
s = sorted(p)
cnt = 0
for i in range(N):
    if p[i] != s[i]:
        cnt += 1
        if cnt > 2:
            print('NO')
            exit(0)
print('YES')

Solution (Python):
n = int(input())
num_lists = list(map(int, input().split()))

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

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

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

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("YES") if fl else print("NO")
import collections
import math
import itertools
import heapq as hq
n = ni()
P = lma()
cnt = 0
for i in range(n):
    if P[i] != i+1:
        cnt+=1
f = False
if cnt==2 or cnt==0:
    f=True
yn(f)

Solution (Python):
import numpy as np
n = int(input())
n_list = np.array(list(map(int,input().split())))
syoujun_list =np.array(range(1,n+1))
different = n_list - syoujun_list
if np.count_nonzero(different) ==2 or np.count_nonzero(different) ==0:
    print('YES')
else:
    print('NO')

Solution (Python):
N=int(input())
p=list(map(int,input().split()))
a=0
for i in range(N):
    if p[i]!=i+1:
        a+=1
if a>=3:
    print('NO')
else:
    print('YES')

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

k=0
for i in range(N):
    if i+1!=p[i]:
        if k>2:
            break
        k+=1

print('YNEOS'[k>2::2])

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

k=0
for x,y in zip(p,sorted(p)):
    if x!=y:
        if k>2:
            break
        k+=1

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

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
q = [i + 1 for i in range(N)]

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

ok = cnt == 2 or cnt == 0

print("YES" if ok else "NO")

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

Solution (Python):
import copy

input()
ary = list(map(int, input().split()))
copy = copy.copy(ary)
ary.sort()
ans = 0

for a, c in zip(ary, copy):
  if a != c:
    ans += 1

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

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

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

Q = [i+1 for i in range(N)]
for i in range(N):
    for j in range(i,N):
        P[i], P[j] = P[j], P[i]

        if P == Q:
            print("YES")
            exit()

        P[i], P[j] = P[j], P[i]

print("NO")

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

c = 0

for i in range(n):
    if a[i] != i + 1:
        c += 1

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

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

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

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

Solution (Python):
def main():
    n = int(input())
    p = list(map(int, input().split()))
    count = 0

    for i in range(n):

        if p[i] == i+1:
            count += 1

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

if __name__ == '__main__':
    main()

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

Solution (Python):
N = int(input())
p_ls = list(map(int, input().split(' ')))
cnt = 0

for i in range(len(p_ls) - 1):
    min_idx = i
    for j in range(i + 1, len(p_ls)):
        if p_ls[min_idx] > p_ls[j]:
            min_idx = j
    if min_idx != i:
        p_ls[min_idx], p_ls[i] = p_ls[i], p_ls[min_idx]
        cnt += 1
if cnt > 1:
    print('NO')
else:
    print('YES')

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
p0 = 0
p1 = P[0]
temp = []
for i, p in enumerate(P[1:]):
    if p >= p1:
        p0 = p1
        p1 = p
        continue
    else:
        temp.append(i)
        p1 = p

if len(temp) == 2:
    P[temp[0]], P[temp[1]+1] = P[temp[1]+1], P[temp[0]]

    p0 = 0
    p1 = P[0]
    temp = []
    for i, p in enumerate(P[1:]):
        if p >= p1:
            p0 = p1
            p1 = p
            continue
        else:
            temp.append(i)
            p1 = p
    if len(temp) == 0:
        print("YES")
    else:
        print("NO")
elif len(temp)==0:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
p = list(map(lambda x: int(x), input().split(" ")))

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

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

Solution (Python):
N, *P = map(int, open(0).read().split())

ans = 0
for i in zip(P, range(1,N+1)):
    if i[0] != i[1]:
        ans += 1

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

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
sorted_P = sorted(P)

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

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

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

ps = sorted(p)

count = 0
for q, qs in zip(p, ps):
  if q != qs:
    count += 1
    
print('YES' if count <= 2 else 'NO')

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

ps = sorted(p)

d_p = []
d_ps = []
for i,q in enumerate(p):
  if q != ps[i]:
    d_p.append(q)
    d_ps.append(ps[i])
if len(d_p) > 2:
  print('NO')
elif len(d_p) == 2:
  d_p.sort()
  if d_p == d_ps:
    print('YES')
  else:
    print('NO')
else:
  print('YES')

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

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

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
p_s=sorted(p)
s=sum((1 for i in range(n) if p[i]!=p_s[i]) )
print("YES" if s<=2 else "NO")

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
sor_P = sorted(P)
count = 0
for i in range(n):
    if P[i] != sor_P[i]:
        count += 1
    if count > 2:
        print('NO')
        exit()
print('YES')

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
A = []
y = 0

for n in range(1,N+1):
  A.append(n)

for p,a in zip(P,A):
  if p!=a:
    y+=1
    

if y<3:
  print("YES")
else:
  print("NO")

Solution (Python):
def main():
    n = int(input())
    p = [int(i) for i in input().split()]
    for i in range(n):
        for j in range(i, n):
            p[i], p[j] = p[j], p[i]
            if is_sorted(p):
                print("YES")
                return
            p[i], p[j] = p[j], p[i]
    print("NO")


def is_sorted(p):
    for i in range(len(p) - 1):
        if p[i] > p[i + 1]:
            return False
    return True


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N = int(input())
xlist = list(map(int, input().split()))
ans = "NO"

minus_gaps = np.count_nonzero(np.diff(xlist)<0)

if minus_gaps == 0:
    ans = "YES"

elif minus_gaps == 2:
    tmpList = np.where(np.diff(xlist)<0)
    xlist[tmpList[0][0]],xlist[tmpList[0][1]+1] = xlist[tmpList[0][1]+1], xlist[tmpList[0][0]]
    if np.count_nonzero(np.diff(xlist)<0) == 0:
        ans = "YES"

print(ans)

Solution (Python):
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")
elif cnt <= 2:
    print("YES")

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

Solution (Python):
n = int(input())
s = list(map(int, input().split()))

S = sorted(s)

count=0
for i in range(n):
  if s[i] != S[i]:
    count += 1
    
if count == 0 or count == 2:
  print('YES')
else:
  print('NO')

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

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
diff = []
for i in range(n):
    if p[i] != q[i]:
        diff.append(i)
print("YES" if len(diff) <= 2 else "NO")

Solution (Python):
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 == 0 or cnt == 2:
  print('YES')
  
else:
  print('NO')

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







def solve():
    N = II()
    P = LI()

    cnt = 0
    for i, p in enumerate(P):
        if (i+1) != p:
            cnt += 1
    if cnt <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    solve()

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

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

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

Solution (Python):
N = int(input())
MM = input().split()
count = 0
list1 =[]
for i in MM:
  a = int(i)
  list1.append(a)
list2 = sorted(list1)
for i,j in zip(list1,list2):
  
  if i != j:
    
    count +=1
if count >2:
  print('NO')
else:
  print('YES')

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

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

Solution (Python):







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
if (ans <= 2):
    print("YES")
else:
    print("NO")

Solution (Python):



'''
解説見て書き直し、
数列の要素と順番(i+1)が一致していない個数を数える。
これが3以上であれば"NO"
3未満(0か2)であれば"YES"
'''


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

count = 0

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

if count < 3 :
    print('YES')
else :
    print('NO')

Solution (Python):



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

S={}
s=N+1

for i in range (N):
    if p[i]==i+1:
        pass
    else :
        S[i+1]=p[i]
        s=i+1


if len (S)==0:
    print('YES')
    exit()
elif len(S)>2:
    print('NO')

else:
    n=S[s]
    if S[n]==s:
        print('YES')
    else:
        print('NO')

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

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

    def hantei(A):
        for i in range(1,len(A)):
            if not A[i-1]<A[i]:
                return False
        return True
    
    if hantei(A):
        print('YES')
        return
    x=list(range(N))
    for i,j in combinations(x,2):
        cA=A.copy()
        cA[i],cA[j]=cA[j],cA[i]
        if hantei(cA):
            print('YES')
            return
    print('NO')



if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
q = 0
for i in range(n):
 for j in range(i, n):
  t = a[i]
  a[i] = a[j]
  a[j] = t
  f = 1  
  lst = - 1100
  for k in a:
   f &= k > lst
   lst = k
  q |= f
  t = a[i]
  a[i] = a[j]
  a[j] = t
print("YES" if q else "NO")

Solution (Python):
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')

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

Solution (Python):
import sys
n=int(input())
p=list(map(int,input().split()))
q=sorted(p)
for i in range(n-1):
  for j in range(i,n):
    s=p[i]
    p[i]=p[j]
    p[j]=s
    if p==q:
      print("YES")
      sys.exit()
    s=p[i]
    p[i]=p[j]
    p[j]=s
      
print("NO")

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

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

Solution (Python):
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=count+1
    else:
        pass
if count==0 or count==2:
    print("YES")
else:
    print("NO")

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

Solution (Python):
from copy import copy
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
ret = 'NO'
for i in range(n-1):
    for j in range(i, n):
        r = copy(p)
        r[i] = p[j]
        r[j] = p[i]
        if q == r:
            ret = 'YES'
            break
print(ret)

Solution (Python):
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:
    print("NO")
else:
    print("YES")

Solution (Python):

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

miss_cnt = 0

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

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

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

swap = 0

for i in range(n):
    min_num = p[i]
    min_j = i
    for j in range(i, n):
        if min_num > p[j]:
            min_num = p[j]
            min_j = j
    if min_j != i:
        p[i] ,p[min_j] = p[min_j], p[i] 
        swap += 1
    
    
if swap <= 1:
    print("YES")
else:
    print("NO")

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

wrong = 0
for i in range(1, n + 1):
    if i == p[i - 1]:
        pass
    else:
        wrong += 1

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

Solution (Python):
n, p = int(input()), list(map(int, input().split()))
px = sorted(p)
a = 0
for i in range(n):
  if p[i] != px[i]:
    a += 1
if a/2 <= 1:
  print('YES')
else:
  print('NO')

Solution (Python):
n=int(input());a=list(map(int,input().split()))
print('NYOE S'[sum([i==j for i,j in zip(a,sorted(a))])>=n-2::2])

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

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

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

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

Solution (Python):

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

Solution (Python):
n = int(input())
pl = list(map(int,input().split()))

temp = 0

for i in range(n):
    if pl[i] != i+1:
        temp += 1

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

Solution (Python):
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 or cnt == 0:
    print("YES")
else:
    print("NO")

Solution (Python):






















































global FLAG_LOG
FLAG_LOG = False


def log(value):
    
    
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    
    N = int(lines[0])
    
    values = list(map(int, lines[1].split()))
    
    
    
    
    
    
    

    values_c = values.copy()
    values_c.sort()

    cnt = 0

    for n in range(N):
        if values[n] != values_c[n]:
            cnt += 1
    
    if cnt <= 2:
        result = 'YES'
    else:
        result = 'NO'

    return [result]



def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines



def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['5', '5 2 3 4 1']
        lines_export = ['YES']
    if pattern == 2:
        lines_input = ['5', '2 4 3 5 1']
        lines_export = ['NO']
    if pattern == 3:
        lines_input = ['7', '1 2 3 4 5 6 7']
        lines_export = ['YES']
    return lines_input, lines_export



def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode



def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    
    
    
    
    
    
    
    
    
    
    



if __name__ == '__main__':
    main()

Solution (Python):
import math
def main():
  n=int(input())
  p = list(map(int,input().split()))
  first=-1
  last=-1
  
  for i in range(0,n-1):
    if p[i]>p[i+1]:
      first=i
      break
  for i in reversed(range(1,n)):
    if p[i]<p[i-1]:
      last=i
      break
  
  if first!=-1 and last!=-1:
    tmp=p[first]
    p[first]=p[last]
    p[last]=tmp

  for i in range(0,n-1):
    if p[i]>p[i+1]:
      print("NO")
      break
    if i==n-2:
      print("YES")  
main()

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

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

cnt = 2
for i in range(1,N+1):
    cnt -= 1 if p_list[i-1] != i else 0

if cnt < 0:
    print("NO")
else:
    print("YES")

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

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

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

Solution (Python):
N = int(input())
count = 0
Nlist = list(map(int, input().split()))
for i in range(1, N+1):
    count += Nlist[i-1] == i
if count+2 >= N:
    ans = "YES"
else:
    ans = "NO"
print(ans)

Solution (Python):
N=int(input())
p1=list(map(int,input().split()))
p2=sorted(p1)
count=0
for i in range(N):
  if p1[i]!=p2[i]:count+=1

if count in [0,2]:print('YES')
else:print('NO')

Solution (Python):
N=int(input())
List = list(map(int, input().split()))
flag = 0
for i in range(N):
  if List[i] == i+1:
    pass
  else:
    flag += 1
if flag==2 or flag == 0:
  print("YES")
else:
  print("NO")

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
print('YES' if sum(p[i] != i + 1 for i in range(n)) <= 2 else 'NO')

Solution (Python):
n = int(input())
p = [int(i) for i in input().split()]
sp = sorted(p)
print('YES' if sum(p[i] == sp[i] for i in range(n)) >= n - 2 else 'NO')

Solution (Python):
N = int(input())
ppp = list(map(int, input().split()))
cnt = 0
for i, p in enumerate(ppp, 1):
    if i != p:
        cnt += 1
print('YES' if cnt < 3 else 'NO')

Solution (Python):
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):
  print("YES")
else:
  print("NO")

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
count = 0

sort_p = sorted(p)

for i in range(n):
  if p[i] != sort_p[i]:
    count += 1
    
if count  == 2:
  print("YES")
  exit()
elif count == 0:
  print("YES")
  exit()
else:
  print("NO")
  exit()

Solution (Python):
def main():
    N = int(input())
    p = list(map(int, input().split()))
    c = {}
    f = []
    for i in range(N):
        if p[i] != i+1:
            c[p[i]] = i+1
            f.append(p[i])
    if len(c) > 2:
        print('NO')
    elif len(f) == 0 or (c[f[0]] == f[1] and c[f[1]] == f[0]):
        print('YES')
    else:
        print('NO')

main()

Solution (Python):
n = int(input())
pl = list(map(int,input().split()))

temp = 0

for i in range(n):
    if pl[i] != i+1:
        temp += 1

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

Solution (Python):
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 == 3:
            break
if count < 3:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
cnt = sum(p[i] != q[i] for i in range(n))
print(["NO", "YES"][cnt <= 2])

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

Solution (Python):
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 == 0:
  print("YES")
elif cnt == 2:
  print("YES")
else:
  print("NO")

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

Solution (Python):
import sys
import math

def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    P = list(map(int,input().split()))
    PS = sorted(P)
    count =0
    for i in range(N):
        if P[i]!=PS[i]:
            count +=1

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



if __name__ == "__main__":
    main()

Solution (Python):
import sys
N = int(input())
array = list(map(int,input().split()))
array_base = [ I for I in range(1,N+1) ]

dic = {}
for I in range(N):
    if array[I] != I + 1:
        dic[I] = array[I]

if not (len(dic) == 2 or len(dic) == 0):
    print('NO')
else:
    print('YES')

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

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
s = sorted(p)
c = 0
for i in range(len(p)):
    if p[i] == s[i]:
        c += 1
if c >= len(p)-2:
    print('YES')
else:
    print('NO')

Solution (Python):
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')

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
x = 0

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

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

Solution (Python):
input()
*p,=map(int,input().split())
print('YNEOS'[2<sum(i!=j for i,j in zip(p,sorted(p)))::2])

Solution (Python):
input()
*p,=map(int,input().split())
print('YNEOS'[2<sum(1 for i,j in zip(p,sorted(p)) if i!=j)::2])

Solution (Python):
input()
*p,=map(int,input().split())
print('YNEOS'[2<sum([1 for i,j in zip(p,sorted(p)) if i!=j])::2])

Solution (Python):
input()
*p,=map(int,input().split())
print('NO' if 2<sum([1 for i,j in zip(p,sorted(p)) if i!=j]) else 'YES')

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

Solution (Python):
n=int(input())
li=list(map(int,input().split()))
lis=sorted(li)
dif=0
for (x,y)in zip(li,lis):
  if x!=y:
    dif+=1
if dif<3:
  print("YES")
else:
  print("NO")

Solution (Python):
import sys

N = int(sys.stdin.readline())
ps = [int(x) for x in sys.stdin.readline().split(" ")]


wrong_count = 0
for i in range(0, N):
    if ps[i] == i + 1:
        continue
    wrong_count += 1

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

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
order = [i for i in range(1, N+1)]
count = 0

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

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

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

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
cnt = sum([(x - i) != 0 for i, x in enumerate(P, 1)])
if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

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

Solution (Python):
input()
s = list(map(int,input().split()))
t = sorted(s)
if s==t:
    print('YES')
else:
    ans = sum(1 for i,j in zip(s,t) if i!=j)
    print('NO' if ans>2 else 'YES')