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):
import sys
sys.setrecursionlimit(4100000)

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from copy import copy

def resolve():
    N = [int(x) for x in sys.stdin.readline().split()][0]
    p_list = [int(x) for x in sys.stdin.readline().split()]

    

    is_broken = False
    old = p_list[0]
    for p in p_list[1:]:
        if old > p:
            is_broken = True
            break
        old = p

    if not is_broken:
        print("YES")
        return

    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            p_list_alt = copy(p_list)
            p_list_alt[i], p_list_alt[j] = p_list[j], p_list[i]

            is_broken = False
            old = p_list_alt[0]
            for p in p_list_alt[1:]:
                if old > p:
                    is_broken = True
                    break
                old = p

            if not is_broken:
                print("YES")
                return

    print("NO")

            
            

import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5
5 2 3 4 1"""
        output = """YES"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """5
2 4 3 5 1"""
        output = """NO"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """7
1 2 3 4 5 6 7"""
        output = """YES"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """2
1 2"""
        output = """YES"""
        self.assertIO(input, output)
    def test_入力例_5(self):
        input = """2
2 1"""
        output = """YES"""
        self.assertIO(input, output)





if __name__ == "__main__":
    resolve()

Solution (Python):
N = int(input())
arr = input().split(" ")

res = []

for i in range(1,N+1):
  if(int(arr[i-1]) != i):
    res.append(arr[i-1])

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

Solution (Python):
import string
while True:
    try:
        pd=0
        n=int(input())
        l=list(map(int,input().split()))
        L=l[:]
        L.sort()
        for i in range(n):
            for j in range(n):
                l[i],l[j]=l[j],l[i]
                if L==l:
                    pd=1
                    break
                else:
                    l[i], l[j] = l[j], l[i]
            if pd==1:
                break
        if pd==1:print("YES")
        else:print("NO")
    except:break

Solution (Python):
import numpy as np
import math
N =int(input())
c=list(map(int, input().split())) 
a = np.arange(1,N+1)

d = sorted(c-a)
if (abs(d[0])-abs(d[N-1])==0) and (np.count_nonzero(d)==2):
    print('YES')
elif (np.count_nonzero(d)==0):
    print('YES')
else:
    print('NO')

Solution (Python):
from sys import stdin

readline = stdin.readline


def main():
    _, *p = map(int, open(0).read().split())
    count = 0
    sp = sorted(p)
    for a, b in zip(sp, p):
        if a != b:
            count += 1
    print('YES' if count <= 2 else 'NO')


if __name__ == "__main__":
    main()

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

diff = 0
for (p, q) in zip(P, Q):
    if p != q:
        diff += 1

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

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

for i in range(n):
    minj = i
    for j in range(i+1, n):
        if p[j] < p[minj]:
            minj = j
    t = p[i]
    p[i] = p[minj]
    p[minj] = t
    if i != minj:
        counter += 1

if counter <= 1:
    print('YES')
else:
    print('NO')

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

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

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

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
count = 0
for bp, ap in zip(p, sorted(p)):
    if bp != ap:
        count += 1

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

Solution (Python):
import numpy as np

N = int(input())

S = input().split()

array = np.zeros(N)

counter = 0

for i in range(len(S)):
    array[i] = int(S[i])

for i in range(len(S) - 1):
    check = np.min(array[i + 1:])
    if(array[i] > check):
        swap = array[np.argmin(array[i + 1:]) + i + 1]
        array[np.argmin(array[i + 1:]) + i + 1] = array[i]
        array[i] = swap
        counter = counter + 1

if(counter <= 1):
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
N = list(map(int, input().split()))
cnt = 0
for i in range(0, len(N)-1):
    syou = N[i]
    syounum = i
    for j in range(i+1,len(N)):
      if syou > N[j]:
        syounum = j
        syou = N[j]
    if syounum != i:
      cnt+=1
      tmp = N[i]
      N[i] = N[syounum]
      N[syounum] = tmp
if cnt <= 1:
  print("YES")
else:
  print("NO")

Solution (Python):
import copy
def main():
    N = input()
    array1 = list(map(int, input().split()))
    array2 = copy.deepcopy(array1)
    array1.sort()
    n = 0
    for c1,c2 in zip(array1,array2):
        if c1 != c2:
            n += 1
            if n == 3:
                print("NO")
                return
    print("YES")
    return
main()

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]:
        pass
    else:
        cnt+=1

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

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
ANS = list(range(1,N+1))
count = 0 
dif = list()

for i in range(N):
	if ANS[i] != P[i]:
		count += 1 
		dif.append(P[i])

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

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

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

if p==label:
    print("YES")
else:
    import numpy as np
    a=np.array(p)
    b=np.array(label)
    if np.sum(a==b)==n-2:
        index=np.where(a!=b)
        ia=p[index[0][0]]
        ib=p[index[0][1]]
        p[index[0][1]]=ia
        p[index[0][0]]=ib
        if label==p:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

Solution (Python):
n = int(input())
pn = []
pn = [int(x) for x in input().split()]
 
miss = 0
ok = True
for i in range(0,n):
    if i != pn[i]-1:
        if miss == 0:
            miss = pn[i]-1
        else:
            if miss != i:
                ok = False
                break
 
if ok:
    print('YES')
else:
    print('NO')

Solution (Python):
n = int(input())
P = list(map(int,input().split()))
dic ={}
for i, num in enumerate(P):
    i+=1
    if num !=i:
        dic[num]=i
if len(dic)>2:
    print('NO')
    exit()
a = list(dic.keys())
b = list(dic.values())
if len(dic)==0 or (a[0]==b[1] and a[1]==b[0]):
    print('YES')

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

N = P[:]
N.sort()

cnt = 0
for i, j in zip(N, P):
    if i != j:
        cnt += 1

if cnt == 0 or cnt == 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)

flip = np.sum(P!=np.arange(1, 1+N))
if flip<=2:
    print('YES')
else:
    print('NO')

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

N=int(N)

k=0

for i in (range(N)):
    for j in (range(N)):
        p[i],p[j]=p[j],p[i]
        if p == sorted(p):
            k=1
            break
        p[i],p[j]=p[j],p[i]
    if k==1:
        break

if k==1:
    print("YES")
else:
    print("NO")

Solution (Python):
n = int(input())
List = list(map(int, input().split()))
standard = [i+1 for i in range(n)]
gap = [List[i] - standard[i] for i in range(n)]

if gap.count(0)== n-2 or gap.count(0) ==n:
  print('YES')
else:
  print('NO')

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]
count = 0
candidates =[]
for i in range(N):
    if p[i] == i+1:
        continue
    else:
        candidates.append([i+1,p[i]])
if len(candidates) == 0:
    print("YES")
elif len(candidates) ==1:
    print("NO")
elif len(candidates) ==2:
    if candidates[0][0] == candidates[1][1] and candidates[0][1] == candidates[1][0]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Solution (Python):



import math

n = int(input())

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

not_same = 0

sorted_p = sorted(p)

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

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

Solution (Python):
n = int(input())
cl = [i for i in range(1,n+1)]
pl = [int(i) for i in input().split()]
for i in range(1,n+1):
    if not i == pl[i-1]:
        tmp=pl[i-1]
        pl[i-1]=pl[tmp-1]
        pl[tmp-1]=tmp
        break
if cl==pl:
    print("YES")
else:
    print("NO")

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

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

ref = [i for i in range(1, len(p) + 1)]

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

if res < 3:
    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 p[i] != sort_p[i]:
                cnt += 1
if cnt > 2:
        print('NO')
else:
        print('YES')

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

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

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

sub_num = 0

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

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

Solution (Python):
input()
p=list(map(int,input().split()))
t=0
for m,n in zip(p,sorted(p)):
	if m!=n: t+=1
print(("NO","YES")[t<3])

Solution (Python):
import copy

n = int(input())
data = list(map(int, input().split()))
s_data = copy.deepcopy(data)
s_data.sort()
c = 0
for i in range(n):
    if data[i] != s_data[i]:
        c += 1

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

Solution (Python):
N=int(input())
a=input().split()
for i in range (0,N):
  a[i]=int(a[i])
count=0
for i in range(0,N):
  if a[i]!=i+1:
    count=count+1
if count==2:
  print('YES')
elif count==0:
  print('YES')
else:
  print('NO')

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

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

l = [p[n]-num[n] for n in range(N)]
l2 = [j for j in l if j!=0]
if len(l2) == 2:
    if sum(l2) == 0:
        print('YES')
elif len(l2) == 0:
    print('YES') 
else:
    print('NO')

Solution (Python):

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

sorted_p = sorted(p)




















count = 0
for b in range(N):
    if not(p[b] == sorted_p[b]):
        t_index = p.index(sorted_p[b])

        tmp = p[b]
        p[b] = p[t_index]
        p[t_index] = tmp

        count += 1




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

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

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

if len(sw)==0:
  print('YES')
  
if len(sw)==1:
  if (sw[0]>0 and p[sw[0]-1]>p[sw[0]+1]) or (sw[0]<N-2 and p[sw[0]]>p[sw[0]+2]):
    print('NO')
  else:
    print('YES')
if len(sw)==2:
  if (sw[0]>0 and p[sw[0]-1]>p[sw[1]+1]) or (sw[1]<N-2 and p[sw[0]]>p[sw[1]+2]):
    print('NO')
  else:
    print('YES')
if len(sw)>2:
  print('NO')

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

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
ans = sorted(nums)
if nums == ans:
    print("YES")
else:
    count = 0
    for i, j in enumerate(nums, 1):
        if i != j:
            count += 1
            if count >= 3:
                print("NO")
                break
    else:
        print("YES")

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

def check_abc(T):
    c = 0
    a = 0
    for t in T:
        if t < a:
            c += 1
        a = t
    if c == 0:
        print('YES')
        exit()

check_abc(P)
for i in range(N - 1):
    for j in range(i + 1, N):
        T = []
        for p in P:
            T.append(p)
        T[i] = P[j]
        T[j] = P[i]
        check_abc(T)

print('NO')

Solution (Python):
n = input()
n = int(n)
ps = list(map(int, input().split()))
pp = sorted(ps)
remaind = 2
pt = False
for s,p in zip(ps,pp):
    if s != p:
        remaind -= 1

if remaind >= 0:
    print("YES")
else:
    print("NO")

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


p_sorted = sorted(p)


haming = 0
ans = "YES"
for ori_p, sort_p in zip(p, p_sorted):
    if ori_p != sort_p:
        haming += 1
    if haming > 2:
        ans = "NO"
        break

print(ans)

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

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

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())

def solve(N, A):
    problems = []
    for i, x in enumerate(A):
        if x != i+1:
            problems.append(i)
    if len(problems) < 2: return True
    if len(problems) > 2: return False
    i0, j0 = problems
    A[i0], A[j0] = A[j0], A[i0]
    return all(A[k] < A[k+1] for k in xrange(N-1))

print "YES" if solve(N, A) else "NO"

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

Solution (Python):
N = input()
A = list(map(int, input().split()))
B = A.copy()
B.sort()
count = 0
for i in range(int(N)):
    if A[i] != B[i]:
        count += 1
if count == 0 or count == 2:
    print("YES")
else:
    print("NO")

Solution (Python):

import sys

YES = "YES"  
NO = "NO"  


def solve(N: int, p: "List[int]"):
    count = 0

    for i, count_down in enumerate(range(1, N)):

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

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

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    p = [int(next(tokens)) for _ in range(N)]  
    solve(N, p)


if __name__ == '__main__':
    main()

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

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

p_sort=sorted(info)
tem_1=0
tem_2=0
tem=0
for i in range(n-1):
  if info[i]>info[i+1]:
    tem_1=i
    for j in range(n-i-2):
      if info[i+j+1]>info[i+j+2]:
        tem_2=i+j+2
        break
    break
tem=info[tem_1]
info[tem_1]=info[tem_2]
info[tem_2]=tem


for i in range(n):
  if info[i]!=p_sort[i]:
    print("NO")
    break
  
  if i==n-1:
    print("YES")

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

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

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

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

n_slist = sorted(range(n+1)[1:])

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

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

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
Q = list(range(1,N+1))
J = [x - y for (x, y) in zip(P, Q)]
R = [i for i in J if i != 0]
if len(R)<=2:
  print('YES')
else:
  print('NO')

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

data = [int(i) for i in r.split(" ")]
sorted_data = sorted(data)

diff_count = 0
for i in range(0, len(data)):
    if data[i] != sorted_data[i]:
        diff_count += 1

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

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

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

l_sort = sorted(l)


a = 0

for x, y in zip(l, l_sort):
  if x != y:
    a += 1
    
if a == 0 or a == 2:
  print("YES")
else:
  print("NO")

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

ps = sorted(p)

not_match = [x != y for x, y in zip(p, ps)].count(True)

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

Solution (Python):
import itertools

N = int(input())
PS = list(map(int, input().split()))
PSS = sorted(PS)

RES ='NO'
if PS != PSS:
    for i, j in itertools.combinations(range(N), 2):
        PC = PS[:]
        PC[j], PC[i] = PC[i], PC[j]
        if PSS == PC:
            RES = 'YES'
            break
else:
    RES ='YES'

print(RES)

Solution (Python):
import numpy as np
import sys

n = int(input())
p = np.array(list(map(int, input().split())))
p_s = np.array(sorted(p))

if all(p==p_s):
    print("YES")
else:
    diff = p_s - p
    diff_index = np.where(diff!=0)
    
    if len(diff_index[0]) != 2:
        print("NO")
    else:
        p0 = p[diff_index[0][0]]
        p[diff_index[0][0]] = p[diff_index[0][1]]
        p[diff_index[0][1]] = p0
        if all(p == p_s):
            print("YES")
        else:
            print("NO")

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

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

w = []
for i in range(0, len(p)):
  if i+1 != p[i]:
    w.append(i)

if len(w) == 0:
  print("YES")
  exit()

if (len(w) == 2):
  p[w[0]], p[w[1]] = p[w[1]], p[w[0]]
  if (list(range(1, N+1)) == p):
    print("YES")
    exit()

print("NO")

Solution (Python):

a = int(input())
s = list(map(int, input().split(" ")))
t = sorted(s)
ans = 'NO'
all = 0
c = 0

sn = 0
sv = 0
snn = 0

for i in range(a):
    if i == 0:
        continue
    elif s[i] < s[i-1] and c == 0:
        sn = i-1
        sv = s[i-1]
        c = 1
    elif s[i] < s[i-1] and c == 1:
        snn = s[i]
        s[i] = sv
        s[sn] = snn 
        break

for i in range(a):
    if s[i] == t[i]:
        continue
    else:
        all = 1
        break

if all == 0 :
    ans = 'YES'

print(ans)

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
p0 = sorted(p)
key = 0
if p == p0:
    print("YES")
    key = 1
if key == 0:
    cnt = 0
    for i in range(len(p)):
        if p[i] != p0[i] and cnt == 0:
            idx1 = i
            cnt += 1
        elif p[i] != p0[i] and cnt == 1:
            idx2 = i
            cnt += 1
        if cnt >= 2:
            p[idx1], p[idx2] = p[idx2], p[idx1]
            if p == p0:
                print("YES")
                break
            else:
                print("NO")
                break
"""
    for i in range(len(p)):
        pt = p
        for j in range(len(p)):
            if i != j:
                pt[i], pt[j] = pt[j], pt[i]
                if pt == p0:
                    print("YES")
                    key = 1
                    break
"""

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

Solution (Python):
import sys
n = int(input()) 
p = [int(r) for r in sys.stdin.read().split()]
for i, v in enumerate(p[:-1]):
    if p[i] > p[i + 1]:
        for j in range(i + 1, n - 1):
            if p[j] > p[j + 1]:
                if (i == 0 or p[i - 1] < p[j + 1]) and p[i + 1] > p[j + 1] and (j == n - 2 or p[j + 2] > p[i]) and p[j] < p[i]:
                    print('YES')
                    quit()
                else:
                    print('NO')
                    quit()
            if j == n - 2:
                print('NO')
                quit()
    if i == n - 2:
        print('YES')
        quit()

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
P = list(range(1,N+1))
if p == P:
    print('YES')
    exit()

tmp = [i for i in range(N) if P[i] - p[i] != 0]


if len(tmp) != 2:
    print('NO')
else:
    p[tmp[0]], p[tmp[1]] = p[tmp[1]], p[tmp[0]]
    if p == P:
        print('YES')
    else:
        print('NO')

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

c = 0
for p, q in zip(P, sorted(P)):
  if p != q:
    c += 1
    if c > 2:
      break

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

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

cnt = 0
for i in range(N):
	if args[i] != sort_list[i]:
		cnt += 1
	
if cnt == 0 or cnt == 2:
	print("YES")
else:
	print("NO")

Solution (Python):
import copy
n=int(input())
numl=list(map(int,input().split()))	
chk=sorted(numl)

flag=True
if chk==numl :
  flag=True
else :
  flag=False

for i in range(n) :
  
  for j in range(n):
    
    temp=copy.deepcopy(numl)
    temp[i]=numl[j]
    temp[j]=numl[i]
    
    if temp==chk :
      flag=True
      break

if flag==True:
  print("YES")
else :
  print("NO")

Solution (Python):
N = int(input())
p = list(map(int,input().split()))
inc = []
for i in range(1,N+1):
    if p[i - 1] == i:
        continue
    else:
        inc.append(p[i-1])
if len(inc) <= 2:
    print('YES')
else:
    print('NO')

Solution (Python):
N = int(input())
ps = [int(x) for x in input().split()]

for i in range(N):
    min_n = ps[i]
    index = i
    for j in range(i+1, N):
        if ps[j] < min_n:
            min_n = ps[j]
            index = j

    if index != i:
        tmp = ps[i]
        ps[i] = ps[index]
        ps[index] = tmp
        break
    
sorted_ps = sorted(ps)
if ps == sorted_ps:
    print('YES')
else:
    print('NO')

Solution (Python):
A = int(input())
re = 0
B = list(map(int, input().split()))
C = [i for i in range(1, A+1)]
for B, C in zip(B, C):
  if B != C:
    re += 1

if re >= 3:
  print('NO')
else:
  print('YES')

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

Solution (Python):
def main():
    N = int(input())
    P = list(map(int, input().split()))
    A = list(range(1, N + 1))
    x = 0

    if P == A:
        print("YES")
        return

    for i in range(len(P)):
        if P[i] != A[i]:
            x += 1

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

    return


if __name__ == "__main__":
    main()

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

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

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

for i in range(len(a)):
    if i+1 != a[i]:
        flag += 1

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

Solution (Python):
import numpy as np
n = int(input())

p = np.array(list(map(int, input().split())))
ps = sorted(p)
bools = p == ps

if sum(bools) >= len(p) - 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 i+1 != p[i]:
        count += 1

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

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

list_1 = input().split()
list_2 = []
x = 0

for i in range(N):
    list_2.append(i+1)

for i in range(N):
    if int(list_1[i]) == list_2[i]:
        x += 0
    else:
        x += 1

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

Solution (Python):
N = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in range(1,N+1)]
count = 0
for i in range(N):
  if p[i] == q[i]:
    continue
  else:
    count += 1
if count <= 2:
  print("YES")
else:
  print("NO")

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

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    ps = [int(i) for i in input().split()]
    sps = [i for i, j in zip(ps, sorted(ps)) if i != j]
    print("YES" if len(sps) <= 2 else "NO")


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
order= list(map(int,input().split()))
new=sorted(order)
counter=0
for i in range(len(order)):
  if order[i]!=new[i]:
  	counter+=1
if counter in [0,2]:
  print("YES")
else:
  print("NO")

Solution (Python):
a=int(input())
b=list(map(int,input().split()))
c=-1
for i in range(a-1):
  if b[i]>b[i+1]:
    if c==-1:
      c=i
    else:
      b[c],b[i+1]=b[i+1],b[c]
      break
      
for i in range(a-1):
  if b[i]>b[i+1]:
    print("NO")
    exit()
    
print("YES")

Solution (Python):
n=input()
n=int(n)
l=[]
q=list(map(int,input().split()))
for i in range(n):
  p=q[i]
  if i+1!=p:
    l.append(i+1)
if len(l)>=3:
  print("NO")
else:
  print("YES")

Solution (Python):
n = int(input())
p = [int(i) for i in 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):


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

    target_i = None
    for i in range(1, N):
        if P[i-1] > P[i]:
            target = P[i-1]
            target_i = i-1
            if i-2 >= 0:
                low = P[i-2]
            else:
                low = None
            high = P[i]
            break
    
    
    if target_i is not None:
        for i in range(target_i+2, N):
            if P[i] < high:
                if low is None:
                    P[target_i] = P[i]
                    P[i] = target
                    break
                else:
                    if low < P[i]:
                        P[target_i] = P[i]
                        P[i] = target
                        break
                    else:
                        continue
    
    
    for i in range(N):
        if P[i] != i+1:
            print("NO")
            return
    print("YES")




if __name__ == "__main__":
    solve()

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

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

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

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
tigai = []
a = 1

for i in lis:
    if i == a:
        pass
    else:
        tigai.append([a,i])
    a = a+1
if len(tigai) == 0:
    print('YES')
elif len(tigai) != 2:
    print('NO')
else:
    if tigai[0][0] == tigai[1][1]:
        print('YES')
    else:
        print('NO')

Solution (Python):
n = int(input())
p = [int(x) for x in input().split(' ')]

s = p.copy()
s.sort()
cnt = 0

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


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

Solution (Python):

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

p_sort = sorted(p)
diff = 0

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



if p == p_sort:
    print("YES")

elif diff == 2:
    print("YES")
else:
    print("NO")

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
AA=sorted(A)
li=[]
for i in range(N):
  if A[i]!=AA[i]:
    li.append(A[i])
if len(li)>2:
  print("NO")
else:
  print("YES")

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

p = sorted(a)

if a == p:
    print "YES"
    exit()

flag = 0
for i in xrange(n):
    for j in xrange(i + 1, n):
        a[i], a[j] = a[j], a[i]
        if (a == p):
            flag = 1
            break
        a[i], a[j] = a[j], a[i]

    if flag: break

if flag:
    print "YES"
else:
    print "NO"

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

for i in range(n):
    for j in range(n):
        temp = p.copy()
        temp[i], temp[j] = temp[j], temp[i]
        sorted_temp = sorted(temp)

        if sorted_temp  == temp:
            print("YES")
            exit()

print("NO")

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

c = 0
var = 0
t = -1
for k in range(N):
  i = k+1
  if i == t:
    if p[i-1] == s:
      c = c+1
    else:
      var = var+1
  elif p[i-1] == i:
    c = c+1
  else:
    var = var+1
    s = i
    t = p[i-1]

if var <= 1:
  print("YES")
else:
  print("NO")

Solution (Python):
import sys
import copy
N = int(input())
p = list(map(int, input().split()))
A = sorted(p)
if p == A:
  print("YES")
  sys.exit()
  
for i in range(N):
  for j in range(i+1,N):
    newp = copy.copy(p)
    newp[i], newp[j] = newp[j], newp[i]
    if newp == A:
      print("YES")
      sys.exit()
print("NO")

Solution (Python):
N = int(input())
ps = list(map(int,input().split()))
sort_ps = sorted(ps)
tmp = ps.copy()
c = False
if ps == sort_ps:
  print("YES")
else:
  for x in range(len(ps)):
    for y in range(x+1,len(ps)):
      tmp[x], tmp[y] = tmp[y], tmp[x]
      if tmp == sort_ps:
        c = True
      tmp = ps.copy()
  print('YES' if c == True else 'NO')

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

a = 0
b = []
c = []
for i in range(N):
    if i+1 != p[i]:
        a += 1
        b.append(p[i])
        c.append(i+1)

if a == 0:
    print("YES")
elif a == 2:
    if b[0] == c[1] and b[1] == c[0]:
        print("YES")
else:
    print("NO")