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:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
P = np.array(lr())
Q = P.copy()
Q.sort()
result = P == Q
if N - result.sum() <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

res = sorted(p)
cnt = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import scipy as scp

# 入力の受け取り
n = int(input())
# 配列入力の受け取り
arrA = list(map(int,input().split()))
stdarr = list(range(1,n+1))

checknum = 0
for i in range(n):
    if arrA[i] != stdarr[i]:
        checknum += 1
    else:
        continue

if checknum <=2 and checknum!=1:
    ans = "YES"
else:
    ans = "NO"

print(ans)
# print("{:.10f}".format(ans))
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する

Submission status: Accepted

Solution code:
n = int(input())
plist = list(map(int,input().split()))
ans_count = 0
for i in range(n):
  if plist[i] != i+1:
    ans_count += 1
if ans_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):
  if i + 1 != p[i]:
    count += 1
    if count >= 3:
      print("NO")
      exit()
print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a_sort = sorted(a)
count = 0
ans = sum([0 if i==j else 1 for i,j in zip(a,a_sort)])
if ans <= 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())
l = list(map(int,input().split()))
l_1 = sorted(l)
d = "NO"
for m in range(a):
  for n in range(a):
    x =l[m]
    l[m] = l[n]
    l[n] = x
    if l == l_1 :
      d ="YES"
      break
    else:
      x = l[n]
      l[n]=l[m]
      l[m]=x
print(d)

Submission status: Accepted

Solution code:
n = input()
l = map(int,raw_input().split())
sl = sorted(l)
count = 0
for i in xrange(n):
    if sl[i] != l[i]:
        count += 1
if count >2 :
    print "NO"
else:
    print "YES"

Submission status: Accepted

Solution code:
n = int(input())

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

p_sorted = sorted(p_list)
#print(p_sorted)

ans = 0
for i in range(n):
    if p_list[i] != p_sorted[i]:
        ans += 1

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

Submission status: Accepted

Solution code:
n,*a=open(0)
a=list(map(int,a[0].split()))
b=sorted(a)
c=[a-b for a,b in zip(a,b)]
d=int(n)-c.count(0)
print("YES" if d==0 or d==2 else "NO")

Submission status: Accepted

Solution code:
n,p=open(0)
i=1
k=0
a='YES'
for j in p.split():
  if int(j)!=i:k+=1
  if k>2:a='NO'
  i+=1
print(a)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
a = list(range(1, int(n)+1))
cnt = sum(i != j for i, j in zip(p, a))
print('YES' if cnt == 0 or cnt == 2 else 'NO')

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

Submission status: Accepted

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

Submission status: Accepted

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

def is_in_ascending_order(p):
  res = True
  n = len(p)
  for i in range(n-1):
    if not p[i] <= p[i+1]:
      res = False
  return res

res = False
for i in range(N-1):
  for j in range(i+1, N):
    _p = p.copy()
    _p[i], _p[j] = _p[j], _p[i]
    if is_in_ascending_order(_p):
      res = True

# no swap
if is_in_ascending_order(p):
  res = True

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

def swap(array, i,j,tar1,tar2):
  tmp = tar1 
  array[i] = tar2
  array[j] = tmp
#  print(array)
  return array

def orderd(array):
  orderd_array = sorted(array)
#  print(array,orderd_array)
  for i in range(len(array)):
    if not orderd_array[i]==array[i]:
      return False
  return True

N = int(input())
P = list(map(int,input().split()))
# print(P)
ans = "NO"
flg = False
for i in range(N):
  for j in range(i,N):
    Pi = copy.copy(P)
    Pi = swap(Pi,i,j,Pi[i],Pi[j])
    if orderd(Pi):
      ans = "YES"
      flg=True
      break
  if flg:
    break
#print(P)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input();print('YNEOS'[sum(i+1!=int(j) for i,j in enumerate(input().split()))>2::2])

Submission status: Accepted

Solution code:
input();print('YNEOS'[sum(i+1!=int(j) for i,j in enumerate(map(int,input().split())))>2::2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, *p = map(int, open(0).read().split())
t = 0
for i in range(n):
    t += p[i] != sorted(p)[i]
print("YES" if t == 0 or t == 2 else "NO")

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

s = sorted(p)
ok = False
if s == p:
    ok = True
for i in range(len(p)):
    for j in range(i + 1, len(p)):
        q = p[:]
        q[i], q[j] = q[j], q[i]
        if q == s:
            ok = True
print('YES' if ok else 'NO')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
B - 0 or 1 Swap
https://atcoder.jp/contests/abc135/tasks/abc135_b

"""
import sys


def solve(N, arr):
    res = sum(a != b for a, b in zip(arr, sorted(arr)))
    return 'YES' if res <= 2 else 'NO'


def main(args):
    N = int(input())
    arr = [int(p) for p in input().split()]
    ans = solve(N, arr)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

cnt = 0
for i in range(N):
    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('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 'YES'
    c = 0
    for i, a in enumerate(A):
        if i+1 != a:
            c += 1
    
    return 'YES' if c == 0 or c == 2 else 'NO'

print(solve())

Submission status: Accepted

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

sorted_p = sorted(P)

c=0
for i in range(N):
	if P[i]!=sorted_p[i]:
		c+=1
		
if c==0 or c==2:
	print('YES')
else:
	print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
s = sorted(p)
cnt = 0
ans = 'YES'

for (pi, si) in zip(p, s):
    if pi != si:
        cnt += 1

if cnt != 0 and cnt != 2:
    ans = 'NO'

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
pre = 0
onef = False
twef = False
badf = False
for i,num in enumerate(a):
    if i+1 != num:
        if not onef:
            onef = True
            badf = True
        elif not twef:
            twef = True
            badf = False
        else:
            badf = True
if not badf:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

P1 = sorted(P)

fcount = 0
for count in range(N):
    if P[count] != P1[count]:
        fcount += 1

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

Submission status: Accepted

Solution code:
N = int(input())
P = input().split()
flag = "OK"
count = 0
for i in range(N):
  if i+1 != int(P[i]) :
    count = count + 1
if count >= 3:
  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 == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split()))
p_sort = sorted(p)
count = 0
for i,j in zip(p,p_sort):
  if i != j:
    count += 1
if count == 0 or count == 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ps = list(map(int, input().split()))
    ps_sort = sorted(ps)

    Count = 0
    for i, j in zip(ps, ps_sort):
        if i != j:
            Count += 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
List = [int(n)-1 for n in input().split(" ")]

flag = 0
count = 0

for i in range(N):
    l = List[i]
    if l!=i:
        if List[l] == i:
            count+=1
            List[i] = i
            List[l] = l
        else:
            flag=1
            break
        if count > 1:
            flag=1
            break
if flag==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
p = list(map(int, input().split()))
p_new = copy.copy(p)
for i in range(n-1):
  if p[i] > p[i+1]:
    min_val = min(p[i+1:])
    for j in range(n):
      if p[j] == min_val:
        p_new[i] = min_val
        p_new[j] = p[i]
        break
    break

if p_new == sorted(p):
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
N = int(input())
number = input().split(" ")
ans = 0
for i in range(1,N+1):
  if str(i) == str(number[i-1]):
    pass
  else:
    ans += 1
if ans <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:

n = int(input())
p = list(map(int,input().split()))
q = p.copy()
q.sort()
status = 0

for i in range(n):
    for j in range(n):
        r = []
        r = p.copy()
        if i == j:
            pass
        else:
            tmp = r[i]
            r[i] = r[j]
            r[j] = tmp

        if q == r:
            status = 1
            break

if status == 0:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC135 B

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

wrong_order = 0

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

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

Submission status: Accepted

Solution code:
import math
import sys

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

s = 0 
for i in range(0,N):
    if i+1 != P[i]:
        s = s + 1
        if s > 2:
            print('NO')
            exit(0)

print('YES')

Submission status: Accepted

Solution code:
#135_B
n=int(input())
p=list(map(int,input().split()))
q=sorted(p.copy())

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
sort_p = sorted(p)
ans = "YES"
c = 0
for n,m in zip(p,sort_p):
  if n != m:
    c += 1
if c > 2:
  ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
'''a,b= map(int,input().split())'''
#a, b, c = [list(map(int, input().split())) for _ in range(3)]
#li0= [int(x) for x in input().split()]
a=int(input())
li0= [int(x) for x in input().split()]
count=0
for i in range(1,1+a):
  if i!=li0[i-1]:
    count+=1
if count<=2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

l = [i != pi for i, pi in zip(range(1, N+1), p)]
if sum(l) <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]
pn = [int(x) for x in range(1, n+1)]

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
sort_p = sorted(p)
ans = "YES"
c = 0
for n,m in zip(p,sort_p):
  if n != m:
    c += 1
if c > 2:
  ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
m=sorted(l)
cnt=0
for i in range(n):
  if l[i]!=m[i]:
    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()))
cnt = 0
for i in range(n):
    if(data[i] != i + 1):
        cnt += 1


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

Submission status: Accepted

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

p_ans = sorted(p)
# print(p_ans)
cnt=0
for i in range(len(p)):
    if p[i] == p_ans[i]:
        pass
    else:
        cnt+=1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
k=[]
for i in range(1,n+1):
    k.append(i)

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

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

Submission status: Accepted

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

p_s = sorted(p)

b = True
cnt = 0

for p1, p2 in zip(p, p_s):
    if p1 != p2:
        cnt += 1
    
    if cnt >= 3:
        b=False
        break

if b:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

mx = 0
for k in range(n):
    pk = p[k]
    mx = max(mx, pk)
    if pk < mx:
        break
else:
    print('YES')
    sys.exit()

for i in range(n):
    for j in range(i+1,n):
        mx = 0
        for k in range(n):
            pk = p[k]
            if k == i: pk = p[j]
            if k == j: pk = p[i]
            mx = max(mx, pk)
            if pk < mx:
                break
        else:
            print('YES')
            sys.exit()
print('NO')

Submission status: Accepted

Solution code:
import sys
def chk(v):
  for i in range(1,len(v)):
    if v[i-1]>v[i]:
      return False
  return True
n=int(input())
p=list(map(int,input().split()))
if chk(p):
  print("YES")
  sys.exit()
for i in range(n):
  for j in range(i+1,n):
    #swap i,j
    v=[]
    for k in p:
      v.append(k)
    v[i],v[j]=v[j],v[i]
    if chk(v):
      print("YES")
      sys.exit()
print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = map(int, input().split())
a = [i + 1 for i in range(n)]
cnt = 0
for ai, pi in zip(a, p):
    if ai != pi:
        cnt += 1
if cnt <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

flag = True
for i in range(n):
    for j in range(i+1,n):
        tmp = p[i]
        p[i] = p[j]
        p[j] = tmp
        if p == sorted(p):
            print('YES')
            flag = False
            tmp = p[i]
            p[i] = p[j]
            p[j] = tmp
            break
        tmp = p[i]
        p[i] = p[j]
        p[j] = tmp

if p == sorted(p):
    print('YES')
    flag = False

if flag:
    print('NO')

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#import math
#import bisect
#import numpy as np
#import itertools
#import copy
import sys

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n = int(input())
    p = list(map(int,ipti().split()))

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

not_same = 0
for i, j in zip(p, range(n)):
    if i != j+1:
        not_same += 1
if not_same < 3:
    ans = 'YES'
else:
    ans = 'NO'
print(ans)

Submission status: Accepted

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

l=sorted(a)

cnt=0
for i in range(n):
            if int(a[i])!=int(l[i]):
                        cnt+=1
                   
if cnt>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(1,n+1):
  if p[i-1] != i:
    c += 1
if c == 2 or c == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
# coding: utf-8

item_count = int(input().rstrip())

numbers = [int(i) for i in input().rstrip().split(" ")]
cnt = 0
for i in range(item_count):
    if numbers[i] != i+1:
        cnt += 1

print("YES" if cnt in {0,2} else "NO")

Submission status: Accepted

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

t = sorted(p)
l = 0
for i in range(n):
  if p[i] != t[i]:
    l += 1
    if l > 2:
      print('NO')
      break
else:
  print('YES')

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

Submission status: Accepted

Solution code:
n = int(input())
plst = list(map(int,input().split()))
plst_sort = sorted(plst)
cnt = 0
for i in range(len(plst)):
    if plst[i] != plst_sort[i]:
        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()))
q = sorted(p)
cnt = 0
for i in range(N-1):
  if p[i] != q[i]:
    cnt += 1
if cnt <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:

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

q=sorted(p)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
print("YES" if sum([1 for i in range(N) if A[i] != i+1]) <= 2 else "NO")

Submission status: Accepted

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

P=sorted(p)
diff=[]
index=[]

#print(P)

if P==p:
    print("YES")
else:
    for i in range(N):
        if p[i]!=P[i]:
            diff.append(p[i])
            index.append(i)
    if len(diff)>2:
        print("NO")
    else:
        p[index[0]]=diff[1]
        p[index[1]]=diff[0]
        if P==p:
            print("YES")
        else:
            print("NO")

Submission status: Accepted

Solution code:
N = int(input())
ps = list(map(int, input().split()))
diffs = []
for i, p in enumerate(ps):
    index = i + 1
    if index != p:
        diffs.append((index, p))

diffs_size = len(diffs)
if diffs_size == 0:
    print("YES")
elif diffs_size == 2:
    a, b = diffs[0], diffs[1]
    if a[0] == b[1] and a[1] == b[0]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

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

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

if c <= 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()))
sort_P = sorted(P)
cnt = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
temp = 0
i = 1
while i <= n:
    if i != p[i-1]:
        temp += 1
    i += 1
print("YES" if temp < 3 else "NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n, *p = map(int, sys.stdin.read().split())


def main():
    cnt = 0
    for i in range(n):
        if p[i] != i + 1:
            cnt += 1
    
    if not cnt or cnt == 2:
        return 'YES'
    return 'NO'

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

Submission status: Accepted

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

p_sort = sorted(p)
key = [x-y for x,y in zip(p,p_sort)]
cnt = 0
for w in key:
    if w != 0:
        cnt += 1
        if cnt > 2:
            break
if cnt > 2:
  print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
def readints():
    return list(map(int, input().split()))


n = int(input())
p = readints()
# print(sorted(p))
q = sorted(p)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
newp=sorted(p)
cnt=0
for i,j in zip(p,newp):
    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 i==(p.index(i+1)):
        count+=1
print('YES' if count==N-2 or count==N else '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==0 or cnt==2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import copy
N=int(input())
p=list(map(int,input().split()))
s=sorted(p)
if s==p:
  print('YES')
else:
  flag=0
  tmp=[]
  for i in range(N):
    for j in range(i+1,N):
      tmp=copy.deepcopy(p)
      tmp[i],tmp[j]=tmp[j],tmp[i]
      if tmp == s:
        flag=1
  print('NYOE S'[flag::2])