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())
arr = [int(x) for x in input().split(' ')]
ppp = arr.copy()

arr.sort()

dc = 0

for i in range(n):
    if(arr[i] != ppp[i]):
        dc += 1

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

Solution (Python):
N = input()
ps = input().split()
c = 1
diff = 0
for p in ps:
    if c != int(p):
        diff += 1
    c += 1
if diff > 2:
    print("NO")
else:
    print("YES")

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 :
      break

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

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

strange=[]
for i in range(1,1+N):
  if p[i-1]!=i:
    strange.append(i)
if len(strange)==2:
	flag1=strange[0]==p[strange[1]-1]
	flag2=strange[1]==p[strange[0]-1]
	flag=flag1*flag2
if len(strange)==0:
  print('YES')
elif len(strange)!=2:
  print('NO')
elif flag:
  print('YES')
else:
  print('NO')

Solution (Python):
import numpy as np

N = input()
N = int(N)
p = input().split()
p_l = [int(s) for s in p]
p_def = np.array(p_l)
p_sort = np.sort(p_def)

comp = p_def == p_sort
diff = len((np.where(comp==False))[0])
if diff <= 2:
    print("YES")
else:
    print("NO")

Solution (Python):
size = int(input())
tmp = input()
p = tmp.split()
x = 0
for i in range(size):
    if (int(p[i]) != i+1):
        x = x+1
if (x == 2 or x == 0):
    print('YES')
else:
    print('NO')

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import math
import itertools
import string
import queue
import copy
import time
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

n=inp()
p = inpl()
sorted_p = sorted(p)
diff=0
for i in range(n):
    if p[i] != sorted_p[i]:
        diff += 1
if diff == 0 or diff == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
N=list(map(int,input().split()))
N_another=sorted(N)
count=0
if N==N_another:print("YES")
else:
    for i,j in enumerate(N):
        if j!=N_another[i]:
            count +=1
    if count>2:
        print("NO")
    else:
        print("YES")

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

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

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

Solution (Python):
import copy
N=int(input())
p=[int(_) for _ in input().split()]
sorted_p=sorted(p)
if p==sorted_p:
    print('YES')
else:
    flag=0
    for i in range(N):
        for j in range(N):
            new_p=copy.copy(p)
            new_p[i]=p[j]
            new_p[j]=p[i]
            if new_p==sorted_p:
                flag=1
    if flag==0:
        print('NO')
    else:
        print('YES')

Solution (Python):
import numpy as np
N = input()
p= np.array( list(map(int,input().split())))
pp = np.sort(p)
if (np.count_nonzero(pp-p)==2) or (np.count_nonzero(pp-p)==0):
    print("YES")
else:
    print("NO")

Solution (Python):




N=int(input())
p=[int(i) for i in input().split()]
P=sorted(p)
cnt=0
for i in range(N):
    if p[i]!=P[i]:
        cnt+=1
    if cnt>=3:
        break
print("YES" if cnt<=2 else "NO")

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

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

c = 0

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

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

Solution (Python):
n = int(input())
num = [int(_) for _ in input().split()]
exp = num[:]
order = sorted(num)
found = False
for i in range(n):
    for j in range(1,n):
        exp[i] = num[j]
        exp[j] = num[i]
        if exp == order:
            print("YES")
            found = True
            break
        exp = num[:]
    if found:
        break

if not found:
    print("NO")

Solution (Python):
N = int(input())

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

flag = True

tmp = 0
for i in range(N):
    if P[i] > tmp:
        tmp = P[i]
    else:
        flag = False
        break

if flag:
    print('YES')
    exit()

hogehoge = {}

for i in range(N):
    hogehoge[i+1] = P[i]

cnt = 0
for key, value in hogehoge.items():
    if key != value:
        cnt += 1

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

Solution (Python):
N = int(input())
al = list(map(int,input().split()))
ansal = sorted(al)

for i in range(N):
    for j in range(i,N):
        cond_al = al.copy()
        cond_al[i],cond_al[j] = cond_al[j],cond_al[i]
        if(cond_al==ansal):
            print("YES")
            exit(0)

print("NO")

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

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

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

Solution (Python):
import sys

N = int(sys.stdin.readline().rstrip())
PList=list(map(int,sys.stdin.readline().split()))

PListSort=sorted(PList)

exchangeCounter=0
for i in range(N):
  if PList[i] != PListSort[i]:
    exchangeCounter+=1
    
if exchangeCounter <= 2:
  print('YES')
else:
  print('NO')

Solution (Python):
import copy
N = int(input())
p = list(map(int,input().split()))
q = copy.copy(p)
q.sort()
ans = "NO"
for i in range(N):
    for j in range(i,N):
        r = copy.copy(p)
        a,b = r[i],r[j]
        r[i],r[j] = b,a
        if r==q:ans="YES"
print(ans)

Solution (Python):
import copy

n = int(input())
lis = list(map(int, input().split()))
list = copy.deepcopy(lis)
true_list = [ i+1 for i in range(n)]
k = 0

for i in range(n):
    for j in range(n):
        if k == 0:
            list = copy.deepcopy(lis)
            list[i], list[j] = list[j], list[i]
            if list == true_list:
                print("YES")
                k = 1


if k == 0:
    print("NO")

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

Solution (Python):
n = int(input())

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

w = 0

n_list = [i for i in range(1,n+1)]

for i in range(n):
    if l[i] != n_list[i]:
        w += 1

if w <= 2:
    print('YES')

else:
    print('NO')

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

s = sorted(l)

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

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

Solution (Python):
n = int(input())
pn = [int(i) for i in input().split()]
cnt = 0

for i in range(1,n+1):
  if i != pn[i-1]:
    cnt += 1
if cnt > 2:
  print('NO')
else:
  print('YES')

Solution (Python):
import sys
import numpy as np
from scipy.misc import comb
sys.setrecursionlimit(1000000)
 
n = int(input())

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

miss = 0

for i in range(n):
    if miss >= 3:
        print("NO")
        exit()
    if p[i] != i+1:
        miss += 1
    

print("YES")
exit()

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

f = False

pre = (0,0)
for i in range(N):
    if not l[i]==i+1:
        if f:
            print('NO')
            exit()
        if not pre[0]==0:
            if pre[0] == i+1 and pre[1]+1==l[i]:
                f = True
                continue
            else:
                print('NO')
                exit()
        else:
            pre = (l[i],i)
print('YES')

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

a=0
cnt=0
for i in range(N):
   
    if P[i]!=i+1:
        if cnt==0:
            a=P[i]
            ai=i
            cnt=1
        elif cnt==1:
            P[ai]=P[i]
            P[i]=a
            break

for i in range(N):
    if P[i]!=i+1:
        print("NO")
        break
    if i+1==N:
        print("YES")

Solution (Python):
N = int(input())
p = [x for x in 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):
import sys
import numpy as np
input = sys.stdin.readline

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

new=sorted(a)
new=np.array(new)
a=np.array(a)
ans=a-new
if len([i for i in ans if i == 0])==len(a) or len([i for i in ans if i == 0])==(len(a)-2):
    print("YES")
else:
    print("NO")

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

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
err = 0
for i, x in enumerate(p):
    err += 0 if i + 1 == x else 1
print('YES' if err <= 2 else 'NO')

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l2=sorted(l)

l3=[]
for i in range(n):
    l3.append(l[i]-l2[i])

z=l3.count(0)
if n-z==0 or n-z==2:
    print("YES")
else:
    print("NO")

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

Solution (Python):
n = int(input())
tarray = input().split(' ')
array = [int(i) for i in tarray]

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

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

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

if C >= 3:
	print("NO")
else:
	print("YES")

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

Ps = sorted(P)

cnt = 0
for i in range(len(P)):
    if P[i] != Ps[i]:
        cnt += 1
if cnt in [0, 2]:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
f = False
for i in range(N):
    for j in range(N):
        a = []
        for k in range(N):
            a.append(p[k])
        x, y = p[i], p[j]
        a[i] = y
        a[j] = x
        cnt = 0
        for k in range(N-1):
            if a[k] < a[k+1]:
                cnt += 1
        if cnt == N-1:
            print('YES')
            exit()

print('NO')

Solution (Python):
from collections import deque
import copy

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

sortedP = sorted(P)

if sortedP == P:
    print("YES")
    exit(0)

diff_count = 0
for p, sp in zip(P, sortedP):
    if p != sp:
        diff_count += 1


print("YES") if diff_count == 2 else print("NO")

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

error_list = []

for i in range(N):
    if List[i] != i+1:
        error_list.append(i)
        
if len(error_list) == 0:
    print('YES')
elif len(error_list) == 2:
    first = error_list[0]
    second = error_list[1]
    if List[first] == (second+1) and List[second] == (first+1):
        print('YES')
    else:
        print('NO')
else:
    print('NO')

Solution (Python):


import sys



n = int(input())
tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))



for i in range(0,n-1):
	if(hoge[i]<hoge[i+1]):
		if(i==n-2):
			print("YES")
			sys.exit()
	else:
		break

for i in range(0,n-1):
	for j in range(i+1,n):
		k=hoge[j]
		hoge[j]=hoge[i]
		hoge[i]=k
		for m in range(0,n-1):
			if(hoge[m]<hoge[m+1]):
				if(m==n-2):
					print("YES")
					sys.exit()
			else:
				break
		k=hoge[j]
		hoge[j]=hoge[i]
		hoge[i]=k

print("NO")

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n=INT()
    p=LIST()

    p_=p.copy()
    p_.sort()

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

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

if  __name__=='__main__':
    main()

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

wrong = 0

for i, pi in enumerate(p):
    if (i+1) != pi:
        wrong += 1

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

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

sorted_list = sorted(arr)
count = 0
for i in range(N):
    if arr[i] == sorted_list[i]:
        pass
    else:
        count += 1

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

Solution (Python):
def main():
    N = int(input())
    P = list(map(int, input().split()))
    for i in range(N - 1):
        if P[i] > P[i + 1]:
            break
    for j in range(i + 1, N - 1):
        if P[j] > P[j + 1]:
            t = P[i]
            P[i] = P[j + 1]
            P[j + 1] = t
            break
    PP = P[::]
    P.sort()
    if P == PP:
        print('YES')
    else:
        print('NO')


main()

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
count = 0
for i, p in enumerate(P):
    if(p != (i+1)):
        count += 1
if(count > 2):
    print('NO')
else:
    print('YES')

Solution (Python):
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    p = string_to_int(inputs[0])

    diff_point = []
    for i, num in enumerate(range(1, len(p)+1)):
        if num != p[i]:
            diff_point.append(i)
    if len(diff_point) == 2 or len(diff_point) == 0:
        return "YES"
    return "NO"


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Solution (Python):
a = int(input())
list_a=list(map(int, input().split()))  
count = 0
for i in range(len(list_a)):

    if list_a[i] != i+1:
        count += 1
    
if count == 2 or count == 0:
    print("YES")
else :
    print("NO")

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

ans = 'NO'

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


if count == 0 or count == 2:
    ans = 'YES'

print(ans)

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re



def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 


def create_grid(W, H, value = 0):
    return [[ value for _ in range(H)] for _ in range(W)]










N = i_inpl()
p = l_inpl()

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

cnt = 0
for pi, li in zip(p, l):
    if pi != li:
        cnt += 1

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
a_b_sort = a[:]
a.sort()
num=0
flag = 1


if a_b_sort==a:
    print("YES")
    flag = 0


for i in range(len(a)):
    if a[i] != a_b_sort[i]:
        num += 1
        
    if num > 2:
        print("NO")
        flag = 0
        break
        
if flag==1:
    print("YES")

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = sorted(A)

    cnt = 0
    for a, b in zip(A, B):
        if a != b:
            cnt += 1

    if cnt <= 2:
        return 'YES'
    return 'NO'


if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()] 

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

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

Solution (Python):
import numpy as np
n = int(input())
p = list(map(int, input().split()))
p = np.array(p)
count = 0

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

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

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
s = sorted(p)
d = sum([1 for (a, b) in zip(p, s) if (a - b) != 0])
if d <= 2:
    print('YES')
else:
    print('NO')

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

Solution (Python):
import numpy as np

n = int(input())
p = np.array(list( map(int, input().split())) )
pp = np.arange(n) + 1 

if sum(p == pp) >= n-2:
    print('YES')
else:
    print('NO')

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

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

Solution (Python):
def cycle_decomp(N, P):
    vis = [False]*N
    res = []
    for i in range(N):
        cur = []
        while not vis[i]:
            cur.append(i)
            vis[i] = True
            i = P[i]
        if len(cur) > 1:
            res.append(cur)
    return res


N = int(input())
P = [int(x)-1 for x in input().split()]
c = cycle_decomp(N, P)
if not c or len(c) == 1 and len(c[0]) == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
import copy

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

asc_nums = sorted(nums)


def check():
    """総当たりで入れ替えてチェックする"""
    
    if nums == asc_nums:
        return "YES"
    for i in range(N):
        for j in range(i + 1, N):
            nums_copy = copy.copy(nums)
            nums_copy[i] = nums[j]
            nums_copy[j] = nums[i]
            if nums_copy == asc_nums:
                return "YES"
    return "NO"


print(check())

Solution (Python):
N = int(input())
p_list = list(map(int,input().split()))
cnt = 0
for idx, p in enumerate(p_list):
    if p != idx+1:
        cnt += 1
if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
ls=list(map(int,input().split()))
ref_ls=[i+1 for i in range(n)]
count=0

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

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

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

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

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

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

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

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

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


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

Solution (Python):
n = int(input())
P = [int(x) for x in input().split()]
flag = True
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        else:
            copy_P = P[:]
            copy_P[i], copy_P[j] = copy_P[j], copy_P[i]
            if copy_P == sorted(P):
                print("YES")
                flag = False
                break
    else:
        continue
    break
    
if sorted(P) == P:
    print("YES")
    flag = False

if flag:
    print("NO")

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
b=a[::]
a.sort()
d=0
for i in range(n):
  if(a[i]!=b[i]):d+=1
if(d<3):print("YES")
else:print("NO")

Solution (Python):

n = int(raw_input())
p = map(int,raw_input().split())

err_cnt = 0

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

if err_cnt ==0 or err_cnt==2:
    print "YES"
else :
    print "NO"

Solution (Python):
from sys import stdin


def swqp(ps, li, ri):
    result = ps[:]
    tmp = result[li]
    result[li] = result[ri]
    result[ri] = tmp
    return result


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


def main():
    N = int(stdin.readline().rstrip())
    ps = [int(x) for x in stdin.readline().rstrip().split()]
    if check(ps):
        print('YES')
        return
    for i in range(N - 1):
        for j in range(i + 1, N):
            if check(swqp(ps, i, j)):
                print('YES')
                return
    print('NO')


if __name__ == "__main__":
    main()

Solution (Python):

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

sort_p = sorted(p)
number = 0

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

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

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
for i in range(n):
    if (l[i] != i + 1):
        ind = l.index(i + 1)
        l[i], l[ind] = l[ind], l[i]
        break

ok = 1
for i in range(1, n):
    if (l[i] < l[i - 1]):
        ok = 0
if ok:
    print('YES')
else:
    print('NO')

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

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

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

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

diff = []
for i in range(n):
	if(a[i] != b[i]):
		diff.append(a[i])

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

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

for i in range(n):
    num = i + 1
    if num != p[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()))

cnt = 0
for i in range(N):
    if not i+1 == P[i]:
        cnt += 1
if cnt < 3:
    print("YES")
else:
    print("NO")

Solution (Python):
N = int(input())
P = [int(i) for i in input().split()]

P_origin = P[:]
P.sort()

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

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

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

p_sorted = sorted(p)

count = 0

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

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

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

a = [i for i in p]
a.sort()
count = 0
for i in range(n):
    if a[i] != p[i]:
        count += 1
if count <=2:
    print("YES")
else:
    print("NO")

Solution (Python):
inpl = lambda: list(map(int,input().split()))
N = int(input())
p = inpl()
m = 0
for i in range(N):
    if p[i] != i+1:
        m += 1
        if m > 2:
            print('NO')
            break
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:
        cnt += 1

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

Solution (Python):
N = int(input())

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

q = sorted(p)

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

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

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

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
ad = 0
ok = list(range(N+1))

for i in range(N):
    if p[i] == ok[i+1]:
        continue
    else:
        ad += 1

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

Solution (Python):
N = int(input())
p = [int(c) for c in input().split()]

cnt=0
for i,v in enumerate(p):
    if p[i] != i + 1:
        cnt += 1

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

Solution (Python):
N = input()
p_list = [int(i) for i in input().split(' ')]
P_up = sorted(p_list)
count = 0
for i in range(len(p_list)):
    if p_list[i]!=P_up[i]:
        count+=1
        
if count==2 or count==0:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())

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

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

if count in(0,2):
    print("YES")
else:
    print("NO")

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

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

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

Solution (Python):
a=[]
c=[]
b=int(input())
for x in input().split(" "):
    a.append(int(x))
    
for x in a:
    c.append(x)

c.sort()

n=0
i=0

while i<b:
    if a[i]!=c[i]:
        n+=1
    i+=1

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

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



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

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

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

cnt = 0
for p, q in zip(P, Q):
    if p != q:
        cnt += 1
print("YES" if cnt == 2 or cnt == 0 else "NO")

Solution (Python):

n = int(input())

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

log = []
for i in range(n):
    if pn[i] != i+1:
        log.append( (pn[i],i+1) )



ans = ''

if len(log)==0:
    ans = 'YES'

elif len(log)==2:
    xv,xi = log[0]
    yv,yi = log[1]

    ans = 'YES' if xv == yi and yv == xi else 'NO'
else :
    ans = "NO"

print(ans)

Solution (Python):
import copy
import sys
sys.setrecursionlimit(4100000)

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

d = copy.deepcopy(p)
p.sort()
counter = 0
for i in range(n):
    if p[i] != d[i]:
        counter += 1
    if counter >= 3:
        break
if counter >= 3:
    print("NO")
else:
    print("YES")

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

sortarr = sorted(arr)
count = 0

for i in range(n):
    if arr[i] != sortarr[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()))
p_right = sorted(p)
count  =0

for i,o in zip(p, p_right):
  if i != o:
    count += 1
if count <= 2:
  print("YES")
else:
  print("NO")

Solution (Python):

from collections import defaultdict, deque
import math
from math import factorial
import fractions
import re

BIGNUM = 10 ** 9 + 7

def main():
    N = int(input())
    Ps = list(map(int, input().split()))
    
    num = 0
    for i in range(N):
        pi = Ps[i]
        if pi != i + 1:
            num += 1

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

if __name__ == '__main__':
    main()

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

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):
n = int(input())
p = list(map(int,input().split()))

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

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

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    p = list(map(int, input().split()))

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

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

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

Solution (Python):
from sys import stdin

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

    p = sorted(P)
    n = 0
    for p1, p2 in zip(P, p):
        if p1 != p2:
            n += 1
    if n < 3:
        print('YES')
    else:
        print('NO')

input = lambda: stdin.readline()
main()

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

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

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


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

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

for i in range(n):
    if a[i]-1 == i:
        continue
    a[a[i]-1], a[i] = a[i], a[a[i]-1]
    break

if a == sorted(a):
    print("YES")
else:
    print("NO")

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
sort_p=sorted(p)
if p==sort_p:
  print('YES')
  exit()
for i in range(n-1):
  for j in range(i+1,n):
    k=p[i]
    l=p[j]
    p[i]=l
    p[j]=k
    if p==sort_p:
      print('YES')
      exit()
    p[i]=k
    p[j]=l
print('NO')

Solution (Python):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]

def isSorted(num_list):
    for index in range(N-1):
        if num_list[index+1] < num_list[index]:
            return False

    return True

if isSorted(p):
    print("YES")
    exit()

for i in range(N-1):
    for j in range(i+1, N):
        tmp_p = p[:]
        tmp = tmp_p[i]
        tmp_p[i] = tmp_p[j]
        tmp_p[j] = tmp

        if isSorted(tmp_p):
            print("YES") 
            exit()

print("NO")