Problem:


Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.



Sample input:
5
5 2 3 4 1


Sample output:
YES

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split(" ")))
counter = 0
for i in range(N):
    if p[i] != i+1 and counter <= 1:
        counter = counter + 1
    elif p[i] != i+1 and counter > 1:
        counter = counter + 1
        break;
if counter <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
a = int(input())
list2 = list(map(int,input().split()))
list1 = list(range(1,a+1,1))
b = 0
for i in range(a):
    if list1[i] == list2[i]:
        continue
    else:
        if b == 0:
            for j in range(i+1,a):
                if list1[i] == list2[j]:
                    list2[j] = list2[i]
                    list2[i] = list1[i]
                    b += 1
        else:
            print("NO")
            break
else:
    print("YES")

Submission status: Accepted

Solution code:
i = int(input())
l = list(map(int, input().split()))
s = sorted(l)
f = 0

for n in range(i):
    if l[n] == n+1:
        continue
    else:
        f = f + 1
        continue

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

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
     = sys.stdin.readline().rstrip()
     = list(sys.stdin.readline().rstrip())

## 数値
     = int(sys.stdin.readline())
     = map(int, sys.stdin.readline().split())
     = list(map(int, sys.stdin.readline().split()))
     = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

import copy

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007
# sortedと比較
# 
if __name__ == "__main__":
     N = int(sys.stdin.readline())
     L = list(map(int, sys.stdin.readline().split()))

     cnt = 0

     for i, e in enumerate(L, 1):
          if e != i:
               cnt += 1

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

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
     = sys.stdin.readline().rstrip()
     = list(sys.stdin.readline().rstrip())

## 数値
     = int(sys.stdin.readline())
     = map(int, sys.stdin.readline().split())
     = list(map(int, sys.stdin.readline().split()))
     = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

import copy

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007
# sortedと比較
# 
if __name__ == "__main__":
     N = int(sys.stdin.readline())
     L = list(map(int, sys.stdin.readline().split()))

     cnt = 0
     
     for i, e in enumerate(L):
          if e != i + 1:
               cnt += 1

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

Submission status: Accepted

Solution code:
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]!=0:
    cnt+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(i) for i in input().split()]
count = 0 
res = "YES"
p_sorted = sorted(p)
for i in range(N):
  if p_sorted[i] != p[i]:
    count += 1
    if count > 2:
      res = "NO"
      break
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
p_new = sorted(p)
ans = 'NO'
for i in range(n):
    for j in range(i, n):
        tmp = p.copy()
        tmp[i], tmp[j] = tmp[j], tmp[i]
        if tmp == p_new:
            ans = 'YES'
            break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

correct = [i for i in range(1, N+1)]
count = 0
for p, c in zip(P, correct):
    if p != c:
        count += 1

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

Submission status: Accepted

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

box=[]
#リストの中の１つを入れ替えたら123456・・・になるようにする
for i in range(n):
  if i+1 == a[i]:
    pass
  else:
    box.append(i)
if len(box) == 0 or len(box) == 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

f = 0
ans = 'YES'
for i, v in enumerate(p):
    if i == v:
        continue
    if i != v and f:
        ans = 'NO'
        break
    else:
        p[i], p[v] = p[v], p[i]
        f = 1
            
print(ans)

Submission status: Accepted

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

notmatch = 0
for i,num in enumerate(numlist):
    if num != i+1 :
        notmatch +=1

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

Submission status: Accepted

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

count = 0
for idx, pi in enumerate(p):
    if pi != idx+1:
        count += 1
        if count == 3:
            print("NO")
            exit()
print("YES")

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
def sorted(l):
    last=0
    for i in l:
        if last>i:
            return False
        last=i
    return True
for i in range(n):
    for j in range(i,n):
        l[i],l[j]=l[j],l[i]
        if sorted(l):
            print("YES")
            quit()
        l[i],l[j]=l[j],l[i]
print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
inputs = input().split()
p = [int(n) for n in inputs]
ans = [x for x in range(1, N+1)]
count = 0
result = 'YES'

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

if count > 2:
    result = 'NO'

print(result)

Submission status: Accepted

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

ans = 0
i = 0
while (ans < 3 and i < n):
    if (p[i] != i+1):
        ans += 1
    i += 1

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

Submission status: Accepted

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

p_s = sorted(p)
cnt = 0
for i in range(n):
    if p[i]!=p_s[i]: cnt+=1
print("YNEOS"[cnt!=2 and cnt!=0::2])

Submission status: Accepted

Solution code:
import copy

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

q = copy.copy(p)

q.sort()

kaisu = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 設問注意。ただの数字の配列ではなく、 1,2,3,4,5,...Nと1ずつインクリメントされる配列である.

n = int(input())
ary = [int(i) for i in input().split()]
wrongCount = 0

# wrongCountの数が0か2ならYES
# 3を超えた時点でNOを出力して終了する
for i in range(n):
    if ary[i] != i + 1:
        if wrongCount == 2:
            print('NO')
            exit()
        wrongCount += 1

print('YES')

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
for i in range(N-1):
    for j in range(i+1, N):
        q = p[:]
        q[i] = p[j]
        q[j] = p[i]
        if q == list(range(1, N+1)):
            print("YES")
            exit()
if p == list(range(1,N+1)):
    print("YES")
    exit()
print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

b=P.copy()

b.sort()
count=0

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

Submission status: Accepted

Solution code:
def solve(string):
    n, *p = map(int, string.split())
    return "NO" if sum([i != _p for i, _p in enumerate(p, start=1)]) > 2 else "YES"


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Accepted

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

Submission status: Accepted

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

if numlist==p:
        output="YES"
else: 
    dist=0    
    for s,t in zip(numlist,p):
        if s!=t:
                dist=dist+1
        else:
#                dist=dist+0
            pass
    if dist==2:
        output="YES"
    else :
        output="NO"        
print(output)

Submission status: Accepted

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

ans = sorted(p)

if p == ans:
    print ('YES')
    exit()

for i in range(N -1):
    for j in range(i + 1, N):
        p[i], p[j] = p[j], p[i]
        if p == ans:
            print ('YES')
            exit()
        else:
            p[i], p[j] = p[j], p[i]

print ('NO')

Submission status: Accepted

Solution code:
N=int(input())
P=input().split(" ")

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

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

Submission status: Accepted

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

v = sorted(p)
cou=0
for i in range(n):
    if not p[i] == v[i]:
        cou+=1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(p) for p in input().split()]
 
num = 0
for i in range(N):
    if P[i] == i+1:
        num += 1
 
if num >= N-2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

tmp = sorted(p)

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

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

Submission status: Accepted

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

ooo = 0

for i in range(0, n):
    if arr[i] != i+1:
        ooo += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

swap = 0
done = False
ok = True
for i in range(n):
    p = next(ps)
    if p != i+1:
        if done:
            ok = False
            break
        elif swap != 0:
            if swap == i+1:
                done = True
                swap = 0
            else:
                ok = False
                break
        else:
            swap = p

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = sys.stdin.readline

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

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

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

Submission status: Accepted

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

ans = 0
flug = 0

for i in range(N):
  if P[i] != i+1:
    ans += 1
      
if ans <= 2:
  print("YES")
elif ans >= 3:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = sum([int(a)-i-1!=0 for i,a in enumerate(input().split())])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

mustswap = 0
for i, x in enumerate(p):
    if x != (i + 1):
        mustswap+=1


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

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
sorted_nums = sorted(nums)

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))

sorted_b = [i+1 for i in range(a)]

diff = [x-y for (x,y) in zip(sorted_b, b)]

if len([i for i in diff if i != 0]) <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

flag = 0
p_ = sorted(p)
for i in range(len(p)):
    if p_[i] != p[i]:
        if flag > 1:
            print('NO')
            exit()
        flag += 1
print('YES')

Submission status: Accepted

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

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

if num >= N-2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

status = 0
for i in range(0, N):
    if (i + 1) != ll[i]:
        status += 1

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

Submission status: Accepted

Solution code:
"""
- https://atcoder.jp/contests/abc136/tasks/abc135_b
- reg:2≤N≤50
"""
n = int(input())
p = [int(e) for e in input().split()]

z = [i for i in range(n) if p[i] != (i+1)]
ans = "YES" if len(z) <= 2 else "NO"
print(ans)

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    p = list(map(int, input().split()))
    irregular = []
    for i in range(n):
        if p[i] != i + 1:
            irregular.append([i + 1, p[i]])
    if len(irregular) == 0 or (len(irregular) == 2 and irregular[0][1] == irregular[1][0] and irregular[0][1] == irregular[1][0]):
        return "YES"
    else:
        return "NO"


if __name__ == "__main__":
    print(solve())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

Submission status: Accepted

Solution code:
n=int(raw_input())
p = list(map(int,raw_input().split()))
sorted_p = sorted(p)

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

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

Submission status: Accepted

Solution code:
#a,b=map(int,input().split())
n=int(input())
p=list(map(int,input().split()))
import copy
P=copy.deepcopy(p)
P.sort()
cnt=0
for i,j in zip(p,P):
        if i!=j:
                cnt+=1
print("YES") if cnt<=2 else print("NO")

Submission status: Accepted

Solution code:
#a,b=map(int,input().split())
n=int(input())
p=list(map(int,input().split()))
import copy
P=copy.deepcopy(p)
P.sort()
cnt=0
for i,j in zip(p,P):
        if i!=j:
                cnt+=1
if cnt<=2:
        print("YES")
else:
        print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
numbers = input().split()

counter = 0

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

Submission status: Accepted

Solution code:
n=input()
p=list(map(int,input().split()))
def func(i,j):
    if i == len(p) :
        return "YES"
    elif p[i]==i+1 :
        return func(i+1,j)
    elif p[p[i]-1]==i+1 and j<2 :
        j+=1
        return func(i+1,j)
    else :
        return "NO"
print(func(0,0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
ls = list(map(int, input().split(" ")))

diff = 0
for i in range(num):
    if (ls[i] != i+1):
        diff += 1

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

Submission status: Accepted

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


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

Submission status: Accepted

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

expect = sorted(ps)

count = 0
for i, num in enumerate(ps):
    if expect[i] != num:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int, input().split()))
line = list(range(1, n+1))

cnt = 0
not_matches = []
for i, j in zip(data, line):
  if i != j:
    cnt += 1
  if cnt >= 3:
    print('NO')
    break
else:
  print('YES')

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int, input().split()))
line = list(range(1, n+1))

cnt = 0
not_matches = []
for i, j in zip(data, line):
  if i != j:
    cnt += 1
    not_matches.append(i)
    not_matches.append(j)
  if cnt >= 3:
    print('NO')
    break
else:
  if len(not_matches) == 0:
    print('YES')
  elif not_matches[0] == not_matches[3] and not_matches[1] == not_matches[2]:
    print('YES')
  else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_=int(input())
ps=list(map(int, input().split()))
cnt=0
for i, p in enumerate(ps, 1):
    if i != p:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
c = 0
for i in range(N):
  if l[i] != i + 1:
    c += 1
if c <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

rv = sorted(v)

f = []
for i in range(0, len(v)):
 if v[i] != rv[i]:
  f.append(1)
  if len(f) >2:
   print("NO"); exit()

print("YES")

Submission status: Accepted

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

count = 0
ans = "YES"
for i,p in enumerate(P,1):
	if count > 2:
		ans = "NO"
		break
	if i != p:
		count += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    NUM = input()
    
    numary = list(map(lambda x: int(x), input().split()))
    cnt = 0
    for i in range(len(numary)):
        if numary[i] != i + 1:
            cnt = cnt + 1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

count = 0
for i in Pi:
    s = Ps.pop(0)
    if i != s:
        count += 1

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

Submission status: Accepted

Solution code:
import numpy as np
import copy

def main():
    n = int(input())
    ns = list(input().split(' '))
    ns = ns[0:n]
    ns = [int(i) for i in ns]

    ns2 = copy.deepcopy(ns)

    ns2.sort()

    count =0
    for i ,v in enumerate(ns):
        if v != ns2[i]:
            count =count + 1
    
    if count >= 3:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
List = [int(i) for i in input().split()]
count = 0
for j in range(N):
    if(List[j] != j+1):
        count+=1
if(count < 3):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
List = [int(i) for i in input().split()]
count = 0
for j in range(N):
    if(List[j] != j+1):
        count+=1
if(count < 3):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
List = [int(i) for i in input().split()]
count = 0
for j in range(N):
    if(List[j] != j+1):
        count+=1
if(count == 2):
    print("YES")
elif(count == 0):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys

def isInc(a, n):
    for i in range(n-1):
        if a[i] > a[i+1]:
            return False
    return True


n = int(input())

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

if isInc(a,n):
    print("YES")
    sys.exit(0)

for i in range(n):
    for j in range(i+1,n):
        a[i], a[j] = a[j], a[i]
        if isInc(a,n):
            print("YES")
            sys.exit(0)
        a[i], a[j] = a[j], a[i]

print("NO")

Submission status: Accepted

Solution code:
a=int(input())
N=list(map(int,input().split()))
#A=list(map(int,input().split()))

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import itertools

def isAscending(p):
    temp = float('-inf')
    for e in p:
        if temp >= e:
            return False
        temp = e
    else:
        return True

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

if isAscending(p):
    print('YES')
    sys.exit(0)

for i, j in itertools.combinations(range(n), 2):
    p[i], p[j] = p[j], p[i]
    # print(p)
    if isAscending(p):
        print('YES')
        break
    p[i], p[j] = p[j], p[i]
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mao=sorted(li)
z=0
for i in range (0,len(li)):
  if (li[i]!=mao[i]):
    z=z+1

if (z==0 or z==2):
  print ("YES")

else:
  print("NO")

Submission status: Accepted

Solution code:
import itertools
import copy
N = int(input())
p = list(map(int,input().split()))
p_new = copy.copy(p)
t = 0
arg_combination = list(itertools.combinations(range(N),2))

for k in range(N-1):
	if p[k] > p[k+1]:
		t = 1
if t == 0:
	print("YES")
	exit()

for a in arg_combination:
	p_new = copy.copy(p)
	p_new[a[0]] = p[a[1]]
	p_new[a[1]] = p[a[0]]
	t = 0
	for k in range(N-1):
		if p_new[k] > p_new[k+1]:
			t = 1
	if t == 0:
		print("YES")
		exit()
print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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