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())
a=list(map(int,input().split()))
imp=0
for i,x in enumerate(a,1):
  if i!=x:
    imp+=1

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

Submission status: Accepted

Solution code:
import sys
import copy

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
P = MAP()
P_S = copy.deepcopy(P)
P_S.sort()

count = 0
for p, p_s in zip(P, P_S):
	if p!=p_s: count+=1

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
l = [0] * n
#f = [int(0) for _ in range(n)]
#k = 1

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

sum = 0
for i in range(0, n):
  if l[i]:
    sum += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for _p, i in zip(p, range(1, len(p)+1)):
    if _p != i:
        cnt += 1

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

Submission status: Accepted

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

k = 0

for i, pn in enumerate(p):
    if (i+1) != pn:
        k += 1

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

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
flag=True
for k in range(n-1):
    if p[k+1]<p[k]:
        flag=False
if flag:
    print("YES")
    exit()

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

Submission status: Accepted

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

isYes = False
for i in range(N):
    for j in range(N):
        p_ = p.copy()
        p_[i], p_[j] = p_[j], p_[i] # swap
        for n in range(N-1):
            if p_[n] > p_[n+1]:
                break
            elif n == N - 2:
                isYes = True
if isYes:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

dif = []
for i in range(1,n+1):
  if p[i-1] != i:
    dif.append(i)

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

Submission status: Accepted

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

count = 0
for i, xi in enumerate(lP):
    if i + 1 != xi:
        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) :
  if p[i] != i+1 :
    count += 1
    
if count == 2 or count == 0 :
  print('YES')
else :
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
lis = sorted(list(range(1, n+1)))
l = len(p)
cache = l
for k, m in zip(lis, p):
    if k != m:
        l -= 1

if cache - l <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys

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

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

    if(count>=3):
        print('NO')
        sys.exit()

print('YES')

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
c=sum([x!=p[i] for i,x in enumerate(sorted(p))])
print('YES' if c==0 or c==2 else 'NO')

Submission status: Accepted

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

list_p = [i for i in range(1,N+1) ]
flag = 0

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

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

            if p == list_p:
                flag = 1
                break

            p[i],p[j] = p[j],p[i]
    
        if flag == 1:
            break

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

Submission status: Accepted

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

Submission status: Accepted

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

my_array = list(range(1,N+1))
ans_array = []
num = 0

for i in range(0,N):
	num = p_array[i] - my_array[i]
	ans_array.append(num)

new_lst = [e for e in ans_array if e != 0]
if len(new_lst)==0:
	ans = "YES"
elif len(new_lst)==2:
	ans = "YES"
else:
	ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

l = [0]*n

for i in range(n):
	l[i] = i + 1
	
#print(l)

test = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
for i, p in enumerate(P):
    if p-1 != i:
        c += 1
if c in (0, 2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
p = np.array([int(i) for i in input().split()])
if np.count_nonzero(np.array([i + 1 for i in range(N)]) - p) <= 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 p[i]!=i+1:
    cnt=cnt+1
print("YES" if cnt<=2 else "NO")

Submission status: Accepted

Solution code:
import copy

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

q_list = copy.copy(p_list)
p_list.sort()

cnt = 0
for i in range(n):
  if p_list[i] != q_list[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()))
a = list(p)

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

  

for i in range(n):
  for j in range(n):
    a[i] = p[j]
    a[j] = p[i]
    if a == ans:
      print('YES')
      exit()
    else:
      a = list(p)
       
      
      
      
    
print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

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

  

for i in range(n):
  for j in range(n):
    a[i] = p[j]
    a[j] = p[i]
    if a == ans:
      print('YES')
      exit()
    else:
      a = list(p)
    
      
      
      
    
print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

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

    if n < 2 or 50 < n:
        return

    if isinstance(n, int) == False:
            return

    for i in p:
        if isinstance(i, int) == False:
            return

    num = 0

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
p=[int(i) for i in input().split()]
n1=[i for i in range(1,n+1)]
for i in range(n):
  for k in range(n):
    n1[i],n1[k]=n1[k],n1[i]
    if n1==p:
      print('YES')
      exit()
    n1=[i for i in range(1,n+1)]
else:
  print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

fault_num = 0

for i in range(n):
  if( li[i] != i+1 ):
    fault_num += 1
    
if(fault_num <= 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())
a = list(map(int, input().split()))
b = sorted(a)
x = 0
for i in range(n):
    if a[i] != b[i]:
        x += 1
if x == 0 or x == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

pp = range(1, n+1)

c = 0
ans = 'YES'
for i in range(n):
	if i+1 != p[i]:
		if p[p[i]-1] == i+1 and c == 0:
			p[p[i]-1] = p[i]
			c = 1
		else:
			ans  = 'NO'
			break

print(ans)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans = count = 0

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

Submission status: Accepted

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

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

if Flag == True :
    print('YES')
else :
    print('NO')

Submission status: Accepted

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

pr = sorted(p)
count = 0
for i in range(len(pr)):
    if pr[i] != p[i]:
        count += 1
    else:
        continue
        
if count == 2 or count == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

p_list_sort = sorted(p_list)

j = 0
for i in range(N):
    if p_list[i] != p_list_sort[i]:
        j += 1
if j <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

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

miss = 0

ans = 'YES'

for i in range(n):
  if i+1 != p[i]:
    miss += 1
  
  if miss > 2:
    ans = 'NO'
    break

print(ans)

Submission status: Accepted

Solution code:
# ABC 135: B – 0 or 1 Swap
n = int(input())
p = [int(s) for s in input().split()]

p_sorted = sorted(p)

difference = 0
same = len(p) - len(set(p))

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

print('YES' if same > 0 or difference <= 2 else 'NO')

Submission status: Accepted

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

res = "YES"
if list(range(1, n+1)) == p:
  res = "YES"
else:
  count = 0
  for n_e, p_e in zip(range(1,n+1), p):
    if n_e != p_e:
      count += 1
    if count == 3:
      res = "NO"
      break


print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
 
for i, j in enumerate(A):
  if i + 1 != j:
    cnt +=1
if cnt <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
P=list(map(int,input().split()))

flag=True
for index,item in enumerate(P):
    #print(index,item)
    if index!=item-1:
        if flag:
            if P[item-1]==index+1:
                P[item-1]=item
                flag=False
            else:
                print("NO")
                exit()
        else:
            print("NO")
            exit()

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

Submission status: Accepted

Solution code:
n=int(input())
ans=0
p=list(map(int,input().split()))
s=sorted(p)
for i ,j in zip(s,p):
    if i!=j:
        ans+=1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split(' ')))
p1 = p.copy()
p1.sort()
for i in range(N):
    for i1 in range(i,N):
        p2 = p.copy()
        p2[i],p2[i1] = p2[i1],p2[i]
        if p2 == p1:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

Solution code:
n = int(input())
*P, = map(int, input().split())
cnt = 0
for i, p in enumerate(P):
    if i + 1 != p:
        cnt += 1
if cnt <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
ans = 'YES'
k = 0
for i in range(n):
    if p[i] != i + 1:
        if k == 1:
            ans = 'NO'
            break
        else:
            if p[p[i] - 1] == i + 1:    
                p[p[i] - 1] = p[i]
                p[i] = i + 1
                k = 1
            else:
                ans = 'NO'
                break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
P = [int(n) for n in input().split()]
X = [n for n in range(1, N+1)]
ans = sum([0 if p==x else 1 for p,x in zip(P, X)])
print('YES' if ans <= 2 else 'NO')

Submission status: Accepted

Solution code:
# 2019-11-15 14:37:47(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *p = [int(x) for x in sys.stdin.read().split()]

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

    print('YES' if p == list(range(1, n+1)) else 'NO')


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().split()))
l = sorted(s)
cnt = 0
for i in range(len(s)):
    if s[i] != l[i]:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

n = inp()
plist = np.array(inpl())
plist_sort = np.sort(plist)
if (plist != plist_sort).sum() in [0,2]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

correct = []

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

difference = 0

for j in range(N):
    if p[j] != correct[j]:
        difference += 1

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

Submission status: Accepted

Solution code:
a=int(input())
count=0
b=list(map(int,input().split()) )
for i in range(0,len(b),1):
    if i+1!=b[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):
  if p[i] != i+1:
     count = count + 1
if count == 0 or count == 2:
   print('YES')
else:
   print('NO')

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
P=list(map(int,input().split(' ')))

res=0
for i,x in enumerate(P):
  if i+1!=x:
    res+=1
if res>2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

Solution code:
#%%
import sys
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    p_list=[int(x) for x in input().split()]

    p_sorted = sorted(p_list)

    cnt = 0
    for i, p in enumerate(p_list):
        if p_sorted[i] != p:
            cnt += 1
    if cnt == 0 or cnt == 2:
        print('YES')
    else:
        print('NO')


# %%
if __name__ == '__main__':
    main()

# %%
# from atcoder_test import doTest
# doTest("abc135","b",main)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def print_ans(N, p):
    """Test Case
    >>> print_ans(7, "1 2 3 4 5 6 7")
    YES
    >>> print_ans(5, "2 4 3 5 1")
    NO
    >>> print_ans(5, "5 2 3 4 1")
    YES
    """
    pr = list(map(int, p.split()))
    flg = 0
    for i in range(N):
        if i + 1 != pr[i]:
            if flg == 0:
                flg = 1
            elif flg == 1:
                flg = 2
            else:
                flg = 3
    if flg > 2:
        print("NO")
    else:
        print("YES")



if __name__ == '__main__':
    N= int(input().rstrip())
    p = input().rstrip()
    print_ans(N, p)

Submission status: Accepted

Solution code:
N=input()
p=list(map(int,input().split()))
p_sample=sorted(p)
ans=[]

for i ,j in zip(p,p_sample):
    if i == j:
        ans.append(0)
    else:
        ans.append(1)

if sum(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 i + 1 - p[i] != 0:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

for i in range(N):
	if p[i] != i + 1:
		j = p.index(i + 1)
		p[i], p[j] = p[j], p[i]
		break
		
if p == [i for i in range(1, N + 1)]:
	print('YES')
else:
	print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
for i in range(N):
    for j in range(i, N):
        A[i], A[j] = A[j], A[i]
        if sorted(A) == A:
            print("YES")
            quit()
        A[i], A[j] = A[j], A[i]
print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve():
    _, *p_l = map(int, open(0).read().split())
    count = 0

    for i, p in enumerate(p_l):
        if i + 1 != p:
            count += 1
            if count > 2:
                print('NO')
                exit()
    print('YES')


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

cnt = 0
for i in range(n):
    if pl[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()))

ps = [i for i in p]
p.sort()
cnt = 0

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

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

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


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


def ceil(a, b):
    return (a + b - 1) // b


# 初項s, 交差dのn個の数列の和
def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

    s = list(sorted(P[::]))

    for i in range(N):
        for j in range(N):
            P[i], P[j] = P[j], P[i]
            if P == s:
                print("YES")
                return
            P[i], P[j] = P[j], P[i]

    print("NO")



if __name__ == '__main__':
    main()

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
out = "NO"
if count == 0 or count == 2:
    out = "YES"

print(out)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
s_p = sorted(p)
c = 0

for i in range(len(p)):
  if p[i] != s_p[i]:
    c += 1
    if c > 2:
      print('NO')
      exit(0)
      
print('YES')

Submission status: Accepted

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

cnt = 0
res = 'YES'

for i in range(n-1):
    if i+1 != p[i]:
        cnt+=1
        
if cnt >= 3:
    res = 'NO'
        
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    score=0
    for i,a in enumerate(A):
        score+=i+1!=a
    if(score<=2):
        print("YES")
    else:
        print("NO")
resolve()

Submission status: Accepted

Solution code:
N = int(input())

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

p_sorted = sorted(p)

lst = []

for i, j in zip(p, p_sorted):
    if i != j:lst.append(i)
if len(lst) <= 2: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(n):
    if p[i]!=i+1:
        c+=1
if c<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A = list(map(int,input().split()))
B = sorted(A)
cnt = 0
for i in range(N):
  if A[i] != B[i]:
    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()))
count = 0
for i in range(N):
    if not i+1==P[i]:
        count+=1
if count>=3:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
n=list(map(int,list(open(0))[1].split()))
m=sorted(n)
a=b=0
for _ in range(len(n)):a+=0if n[_]==m[_]else 1
b=1if a>2else 0
print('YNEOS'[b::2])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!


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

point = 0
for idx, p in enumerate(pl):
    if idx +1 != p:
        point += 1
if point == 2 or point == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
cnt = 0
for i in range(n):
    if a[i] != i+1:
        cnt += 1
if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import operator

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

tf = list(map(operator.ne, p, range(1, N + 1)))
count = tf.count(True)

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

Submission status: Accepted

Solution code:
n = int(input().strip())
p = list(map(int, (input().split())))
 
ans = []
 
for i in range(n):
    ans.append(p[i] - (i + 1))
 
ans = list(filter(lambda x: (x != 0), ans))
 
if (len(ans) == 0) or (len(ans) == 2 and sum(ans) == 0):
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

for j in range(N):
    if p[j] != col[j]:
        count += 1
if count == 0 or count == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N=int(input())
P=list(map(int,input().split()))
L=sorted(P)
cnt=0
for i in range(N):
  if P[i]!=L[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()))
sorted_P = sorted(P)
check = [i != j for (i, j) in zip(P, sorted_P)]

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

Submission status: Accepted

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

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

ans='YES'
k=len(mistake)
if k>2:
  ans='NO'
elif k==2:
  if mistake[0][0]!=mistake[1][1]:
    ans='NO'

print(ans)

Submission status: Accepted

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

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

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