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()))

cnt=0
for i in range(1,n+1):
    if p[i-1]==i:continue
    cnt+=1
print('YES' if cnt<=2 else 'NO' )

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

count = 0
for p1, p2 in zip(p_list, p_sorted):
	if p1 != p2:
		count += 1
if count <= 2:
	print("YES")
else:
	print("NO")

Solution (Python):
def is_sorted(A):
    return sorted(A) == A

def f(n,A):
    if is_sorted(A):
        return True
    for i in range(n):
        for j in range(i):
            B = A[:]
            B[i],B[j] = B[j],B[i]
            if is_sorted(B):
                return True
    return False

n = int(input())
A = [int(x) for x in input().split()]
print("YES" if f(n,A) else "NO")

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

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

Solution (Python):


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

    sorted_pn = sorted(pn)

    diff = 0
    for (p, sp) in zip(pn, sorted_pn):
        if (p != sp):
            diff += 1

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


if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
import sys

def debug(*args):
    print(*args, file=sys.stderr)


def solve(N, A):
    S = sorted(A)
    cnt = sum(x != y for x, y in zip(A, S))
    if cnt in (0, 2):
        return "YES"
    return "NO"

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    print(solve(N, A))

main()

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

matigai=0

for al in range(len(alist)):
    if alist[al]!=al+1:
        matigai+=1



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

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

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



sp = sorted(p)


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

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

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

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

Solution (Python):
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    n = int(input())
    p_list = li()

    p_sort = sorted(p_list)
    diff = 0
    for p1, p2 in zip(p_list, p_sort):
        if p1 != p2:
            diff = diff + 1
    if diff <= 2:
        print("YES")
    else:
        print("NO")

Solution (Python):











import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    P = LI()
    cor = sorted(P)
    for i in range(N):
        for j in range(N):
            ok = True
            for k,c in enumerate(cor):
                a = P[k]
                if k == i:
                    a = P[j]
                elif k == j:
                    a = P[i]
                if a!=c:
                    ok=False
                    break
            if ok:
                print("YES")
                exit()
    print("NO")
if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
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 S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
p = LIST()
s = sorted(p)


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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline

n=int(input())
ps=list(map(int,input().split()))
cnt=0
for i,p in enumerate(ps):
    if i+1!=p:
        cnt+=1
    if cnt>2:
        print("NO")
        break
else:
    print("YES")

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

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

a_list = p_list.copy()
a_list.sort()

ans = 0

for i in range(N):
    p = p_list[i]
    a = a_list[i]
    if p != a:
        ans += 1

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
for k in range(n - 1):
    if a[k] >= a[k + 1]:
        break
else:
    print('YES')
    exit(0)
for i in range(n):
    for j in range(i + 1, n):
        tmp = a.copy()
        tmp[i], tmp[j] = tmp[j], tmp[i]
        for k in range(n - 1):
            if tmp[k] >= tmp[k + 1]:
                break
        else:
            print('YES')
            exit(0)
print('NO')

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
s = tuple(sorted(P))
if tuple(P) == s:
  print('YES')
  exit()
for i in range(N):
  for j in range(i+1, N):
    if tuple(P[:i] + [P[j]] + P[i+1:j] + [P[i]] + P[j+1:]) == s:
      print('YES')
      exit()
print('NO')

Solution (Python):

def main():
    n = int(input())
    P = tuple(map(int, input().rstrip().split()))
    dif = []
    for i, j in enumerate(P, start=1):
        if i != j:
            dif += [(i, j)]
    if len(dif) == 0:
        print("YES")
    elif len(dif) != 2:
        print("NO")
    elif dif[0] != dif[1][::-1]:
        print("NO")
    else:
        print("YES")


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
pli=list(map(int,input().split()))
cnt=0
for i,j in zip(pli,sorted(pli)):
	if i!=j: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 = p.copy()
q.sort()

count = 0
for i in range(N):
    if p[i] != q[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()))
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 = list(map(int, input().split()))

cnt = 0

for idx, pp in enumerate(p):
    if idx + 1 != pp:
        cnt += 1
    
    if cnt >= 3:
        print('NO')
        exit()
print('YES')

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

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

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

Solution (Python):
N = int(input())
p = list(map(int, input().split()))
if p == list(range(1, N + 1)):
    result = True
else:
    result = False
    for i in range(N):
        for j in range(i + 1, N):
            q = [p[k] for k in range(N)]
            q[i], q[j] = q[j], q[i]
            if q == list(range(1, N + 1)):
                result = True
print("YES" if result else "NO")

Solution (Python):
N=int(input())
p=[int(i) for i in input().split()]
flag=1
for k in range(N-1):
    if p[k]>p[k+1]:
        flag=0
        break
if flag==1:
    print("YES")
    exit()
for i in range(N):
    for j in range(N):
        if i>=j:
            continue
        q=[p[i] for i in range(N)]
        q[i],q[j]=q[j],q[i]
        flag=1
        for k in range(N-1):
            if q[k]>q[k+1]:
                flag=0
                break
        if flag==1:
            print("YES")
            exit()
print("NO")

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

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
sP = sorted(P)
for i in range(N):
    for j in range(N):
        P[i], P[j] = P[j], P[i]
        if P==sP:
            print("YES")
            exit()
        P[i], P[j] = P[j], P[i]
print("NO")

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

Solution (Python):
def swap(arr,i,j):
    arr1 = arr[:]
    arr1[i],arr1[j] = arr1[j],arr1[i]

    return arr1

def increasing(arr):
    for i in range(len(arr)-1):
        if arr[i+1] <= arr[i]:
            return False

    return True

def main():
    n = int(input())
    arr = list(map(int,input().split()))
    if increasing(arr):
        print('YES')
        return

    for i in range(n):
        for j in range(n):
            arr1 = swap(arr,i,j)
            if increasing(arr1):
                print('YES')
                return

    print('NO')

main()

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

ps = sorted(p)
c = 0
for i, v in enumerate(p):
    if v != ps[i]:
        c += 1
    if c > 2:
        print("NO")
        exit()
print("YES")

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

num_list = [ int(v) for v in input().split() ]
right_list = [ i+1 for i in range(n) ]

wrong = 0
for i in range(n):
    if num_list[i] != right_list[i]:
        wrong += 1
if wrong in [0,2]:
    print("YES")
else:
    print("NO")

Solution (Python):
from heapq import heappush, heappop
from collections import deque
import itertools
from itertools import permutations
import sys
import bisect
sys.setrecursionlimit(10**6)
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YN=['YES','NO']
YNeos='YNeos'
mo=10**9+7
imp='IMPOSSIBLE'


n=I()
a=LI()
ans=1
c=[i for i in a]
c.sort()
def rp(i,j):
    b=[i for i in a]
    b[i],b[j]=b[j],b[i]
    return b
for i in range(n):
    for j in range(i,n):
        d=rp(i,j)
        if d==c:
            ans=0

print(YN[ans])

Solution (Python):

import sys
import copy

YES = "YES"  
NO = "NO"  

def solve(N: int, p: "List[int]"):
    p_sort = copy.deepcopy(p)
    p_sort.sort()

    n = 0
    for a,b in zip(p,p_sort):
        if a != b:
            n += 1

        if n > 2:
            print(NO)
            return

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



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

def ok(Qs):
    N = len(Qs)
    for i in range(N - 1):
        if Qs[i] > Qs[i + 1]:
            return False
    return True

if ok(Ps):
    print("YES")
else:
    flag = False
    for i in range(N - 1):
        for j in range(i + 1, N):
            Qs = Ps[:]
            Qs[i], Qs[j] = Qs[j], Qs[i]
            if ok(Qs):
                flag = True
    if flag:
        print("YES")
    else:
        print("NO")

Solution (Python):
def solve(n, ps):
    qs = sorted(ps)
    count = sum(p != q for p, q in zip(ps, qs))
    return count == 0 or count == 2


def main():
    n = int(input())
    ps = [int(p) for p in input().split()]
    print("YES" if solve(n, ps) else "NO")


if __name__ == "__main__":
    main()

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

flag = all(p[i] <= p[i+1] for i in range(n - 1))

for i in range(n):
	for j in range(i + 1, n):
		p[i], p[j] = p[j], p[i]
		if all(p[i] <= p[i+1] for i in range(n - 1)):
			flag = True
			break
		p[i], p[j] = p[j], p[i]
	
if flag:
	print("YES")
else:
	print("NO")

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

count = 0

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

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

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

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

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

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

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

def judge(p, x, y):
  if x == 0:
    re = p[y]
  else:
    re = p[0]
  for i in range(N):
    if i == x:
      t = p[y]
    elif i == y:
      t = p[x]
    else:
      t = p[i]
    if re > t:
      return False
    re = t
  return True

for i in range(N):
  for j in range(i, N):
    if judge(p, i, j):
      print("YES")
      exit()
print("NO")

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
c = 0
for i, x in enumerate(a):
    if i + 1 != x:
        c += 1

print(['NO', 'YES'][c == 0 or c == 2])

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

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

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

for i, j in zip(p, sorted(p)):
    if i != j:
        c += 1

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

Solution (Python):

N = int(input())

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

q = p.copy()

q.sort()

now = 0

for i in range(N):

    if p[i] != q[i]:

        now += 1

if now == 2 or now == 0:

    print ("YES")

else:

    print ("NO")

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

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

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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()
p = LI()

n = [i for i in range(1, N+1)]
tmp = 0
for pi, i in zip(p, n): 
    tmp += (not pi==i)
print("YES" if tmp<=2 else "NO")

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
cp = P.copy()
P.sort()
count = 0
for i in range(N):
        if P[i] != cp[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(1,n+1):
    if i != p[i-1]:
        cnt +=1

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

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

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

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

Solution (Python):
import math
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()
aa = sorted(a)

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

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

Solution (Python):


from math import ceil


def solve():
    N = int(input())
    P = list(map(int, input().split()))
    count = 0
    for i, p in zip(range(1, N + 1), P):
        if i != p:
            count += 1
        if count >= 3:
            break
    return 'YES' if count <= 2 else 'NO'


def main():
    print(solve())


if __name__ == "__main__":
    main()

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

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

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

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

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

Solution (Python):

import sys
import math
from collections import defaultdict


sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))



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

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

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

Q = P.copy()
Q.sort()

n = [p == q for (p, q) in zip(P, Q)].count(False)
print('YES' if n <= 2 else 'NO')

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

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

cnt = 0
for i, a in enumerate(A):
  if a != x[i]:
    cnt += 1
if cnt <= 2:
  print("YES")
else:
  print("NO")

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())
lst = [int(v) for v in input().split()]

count = 0
for i, p in enumerate(lst):
    if i + 1 != p:
        count += 1
    if count > 2:
        break

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

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
MOD = 10**9 + 7
sys.setrecursionlimit(10**7)

dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())


N = ii()
Ps = list(mi())

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

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

Solution (Python):



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

l=[]

for i in range(1,n+1):
	l.append(i)

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

Solution (Python):
ll= lambda : list(map(int,input().split()))
testcases=1

for testcase in range(testcases):
    [n]=ll()
    a=ll()
    a=[0]+a
    dif=0
    for i in range(1,n+1):
        if a[i]!=i:
            dif+=1
    if dif>2:
        print("NO")
    else:
        print("YES")

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

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

if a == sorted(a):
  print('YES')
  raise SystemExit

for i in range(N):
  for j in range(i + 1, N):
    b = a[:]
    b[i], b[j] = b[j], b[i]
    if b == sorted(a):
      print('YES')
      raise SystemExit

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

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
diff = 0
for p,q in zip(P,list(range(1,N+1))):
    if p!=q : diff+=1

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

Solution (Python):
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]






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

r = p[:]
r.sort()

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

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

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

dif = 0

for i,p in zip(range(1,N+1),p):
	if i != p:
		dif += 1
	if dif > 2:
		print("NO")
		exit()
print("YES")

Solution (Python):

n = int(input())

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

def nobori():
    for i in range(n-1):
        if B[i]>B[i+1]:
            return 0
    return 1

ans ="NO"
for i in range(n):
    for j in range(i,n):
        B = A[:]
        B[i],B[j] = B[j],B[i]
        if nobori()==1:
            ans = "YES"
print(ans)

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

Solution (Python):
import sys

fin = sys.stdin.readline
N = [int(elem) for elem in fin().split()][0]
p_list = [int(elem) for elem in fin().split()]

sorted_p = sorted(p_list)

count = 0
for i in range(len(p_list)):
    if p_list[i] != sorted_p[i]:
        count += 1

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

Solution (Python):
import copy
N=int(input())
A=list(map(int,input().split()))

B=copy.deepcopy(A)
B.sort()

cnt=0
for i in range(N):
    if A[i]!=B[i]:
        cnt+=1
if cnt<=2:
    print("YES")
else:
    print("NO")

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

Solution (Python):

from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    p = [int(x) for x in input().split()]
    if p == sorted(p):
        print('YES')
        return
    for i in range(n):
        for j in range(i + 1, n):
            q = p[:]
            q[i], q[j] = q[j], q[i]
            if q == sorted(q):
                print('YES')
                return
    print('NO')




BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

Solution (Python):
def ok():
    print("YES")
    exit(0)
    
def ng():
    print("NO")
    exit(0)

n = int(input())
sorted_p = list(range(1, n + 1))
p = list(map(int,input().split()))

for i in range(n):
    for j in range(i+1, n):
        p[i], p[j] = p[j], p[i]
        if p == sorted_p:
            ok()
        p[i], p[j] = p[j], p[i]

if p == sorted_p:ok()
else: ng()

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
cnt = 0
for i,p in enumerate(P) :
    if p != i+1 :
        cnt += 1

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

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

if(count==2 or count==0):
  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()))
t = 0
for k in range(N):
    if p[k] != (k+1):
        t += 1
print("YES" if t <= 2 else "NO")

Solution (Python):
n=int(input())
p=[int(i) for i in input().split()]
l=0
for i in range(n):
    if i+1!=p[i]:
        l+=1
if l<=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("NO")
else:
    print("YES")

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
if L==sorted(L):
	print("YES")
	exit(0)
for i in range(N):
	for j in range(i+1,N):
		t=L[::]
		t[i],t[j]=t[j],t[i]
		if t==sorted(t):
			print("YES")
			exit(0)
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
print('YES' if count<=2 else 'NO')

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    lp = rli()
    ls = sorted(lp)
    cnt = 0
    for p, s in zip(lp, ls):
        if p != s:
            cnt += 1
    if cnt <= 2:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True


def dprint(*objects):
    if debug == True:
        print(*objects)

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

    sortedp = sorted(p_list)

    cnt = 0
    for p, sp in zip(p_list, sortedp):
        if p != sp:
            cnt += 1

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

solve()