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

Submission status: Accepted

Solution code:
#ABC 041
N = int(input())
P= [ int(a) for a in (input().split())]
for i in range(len(P)):
    for j in range(len(P)):
        P_tmp=P.copy()
        if i!=j:
            P_tmp[j]=P[i]
            P_tmp[i]=P[j]
        if P_tmp ==sorted(P):
            print("YES")
            exit()
            break
    
print('NO')

Submission status: Accepted

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

cnt = 0
prev = 0

# 数列の各要素を「1,2,3,4,…,N」と比較し、異なっている箇所をカウントする
for i in range(N):
    if P[i] != i + 1:
        cnt += 1

# カウント結果が2箇所以内ならOK（1回のswapで昇順にできる）
if cnt <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n=int(input())
P=list(map(int,input().split()))
Q=list([i+1 for i in range(n)])
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:
import sys
N = int(input())
A = [int(x) for x in input().split()]
count=0
for n in range(N):
  if A[n]!=n+1:
    count+=1
    if count>2:
      print("NO")
      sys.exit()
if count==1:
  print("NO")
else:
  print("YES")

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
s = list(map(int,input().split()))
a = np.array(list(range(1,n+1)))
b = np.array(s)
if len(a[a==b]) >= n-2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(k) for k in input().split()]
#template
N = int(input())
p = inputlist()
pa = p[:]
pa.sort()
count = 0
for i in range(N):
    if p[i] != pa[i]:
        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()))
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:
N = int(input())
A = [int(i) for i in input().split()]

count = 0
for i in range(1, N+1):
    if A[i-1] != 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()))
s = sorted(p)
cnt = 0
for i in range(n):
    if p[i] != i+1:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def main():
    *N, = map(int, open(0).read().split())
    p = N[1:]
    N = N[0]

    I = []
    for i in range(1, N+1):
        I.append(i)
    
    err = 0
    for i in range(0, N):
        if not p[i] == I[i]:
            err += 1

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


main()

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()))
q = p[:]
q.sort()
ans = 0
for i in range(N):
  if p[i] != q[i]:
    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=sorted(p)
cnt=0
for i in range(n):
	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()))
s = [i for i in range(1, n+1)]
count = 0
for i in s:
    if p.index(i) != s.index(i):
        count += 1
if count > 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:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input(). split()))
Q = sorted(P)
cnt = 0
i = 0

while cnt <= 2 and i < len(P):
  if Q[i] != P[i]:
    cnt += 1
  i += 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()]
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()))
c = 0
for i in range(n):
    if p[i] != i + 1:
        c += 1
    if c > 2:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
k=0
a=[0]*n
for i in range(n):
  a[i]=i+1
  
for i in range(n):
  if a[i]!=p[i]:
    k+=1
    
    
if k<=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 += 1
print('YES' if  CNT <=  2 else '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:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
# map(int, input().split())
# int(input())
# input().split()
# mojiretu = ','.join(str_list)
# sorted(input())

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

Submission status: Accepted

Solution code:
n = int(input())
s = input().split()
a = 0
c = 0
while a < n:
  if int(s[a]) != a+1:
    c += 1
  a += 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()))
count = 0
flag = True
for i in range(n):
    if p[i] != i+1:
        index = p[i]-1
        if p[index] == i+1:
            count += 1
        else:
            flag = False
            break
if flag:
    if count == 0 or count == 2:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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


main()

Submission status: Accepted

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

count = 0
for i, m in enumerate(M, 1):
    if i != m:
        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 (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()))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy
n = int(input())
p = list(map(int, input().split()))
copy_p = copy.deepcopy(p)
copy_p.sort()
cnt = 0
f = True
for i in range(len(p)):
    if copy_p[i] != p[i]:
        cnt += 1
        f = False

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

Submission status: Accepted

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

Submission status: Accepted

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

T=sorted(S)

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(p) for p in input().split()]
T = sorted(P)
if P == T:
  print('YES')
  exit() 
for i in range(N):
  for j in range(i,N):
    P[i], P[j] = P[j], P[i]
    if P == T:
      print('YES')
      exit()
    else:
      P[i], P[j] = P[j], P[i]
print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

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())
a = list(map(int,input().split()))

count = 0
for i in range(n):
  if a[i] != i+1:
    rem = a[i]
    if rem == a[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()))
for i in range(N):
    if P[i] != i+1:
        P[P[i]-1] = P[i]
        P[i] = i+1
        break
if P == sorted(P):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

for i in range(N):
    if S[i]!=A[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()))

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

Submission status: Accepted

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

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

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

for i in range(n):
    for j in range(n):
        s = p.copy()
        tmp = 0
        tmp = s[i]
        s[i] = s[j]
        s[j] = tmp
        if s == t:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

Solution code:
c=int(input())
n = list(map(int,input().split()))
x = 0
for i in range(len(n)):
    if n[i]!=i+1:
        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()))
P = sorted(p)
count = 0
for i in range(len(p)):
  if p[i] == P[i]:
    count += 1
if count == len(p) or count == len(p) - 2:
  print('YES') #昇順の数列と並び変える前の数列で一致している要素数が同じか2個だけ違う時条件OK
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

p_list = [int(x) for x in input().split()]
q_list = sorted(p_list)

count = 0
ans = "YES"
for p, q in zip(p_list, q_list):
    if p != q:
        count += 1
        if count > 2:
            ans = "NO"
            break
print(ans)

Submission status: Accepted

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

UP = []
for i in range(N):
  UP.append(i+1)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sa = sorted(a)
if a==sa:
  print("YES")
else:
  for ai,si,i in zip(a,sa,range(n)):
    if ai!=si:
      a[ai-1],a[i]=a[i],a[ai-1]
      break
  if a==sa:
    print("YES")
  else:
    print("NO")

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

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

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)

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

k = 0

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

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

Submission status: Accepted

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

a=sorted(p)
b=0

import sys
for i in range(N):
    if p[i]!=a[i]:
        b+=1
        if b>2:
            print('NO')
            sys.exit()
print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

n_l = [i for i in range(1, n+1)]
n_l.reverse()
n_l_a = list(reversed(n_l))

if (n_l_a == p):
    print('YES')
    sys.exit()
else:
    for i in range(n):
        for j in range(n):
            p[i], p[j] = p[j], p[i]
            if (n_l_a == p):
                print('YES')
                sys.exit()
            else:
                p[i], p[j] = p[j], p[i]

print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

k=0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*p=map(int,open(0).read().split())
q=sorted(p)
for i in range(n):
  for j in range(n):
    r=p[:]
    r[i],r[j]=r[j],r[i]
    if r==q:
      print('YES')
      exit()
print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
p = LIST()

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

for i in range(N-1):
  for j in range(i+1, N):
    q = p.copy()
    q[i] = p[j]
    q[j] = p[i]
   
    if q == sorted(p):
      print("YES")
      exit()
      
print("NO")

Submission status: Accepted

Solution code:
N= int(input())

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

count=0
for i in range(len(p)):
  if p[i]!=q[i]:
    count += 1
#print(N,p,q,count, sep="\n")

#zipを使う
count=0
for a,b in zip(p,q):
  if a!=b:
    count += 1
#print(N,p,q,count, sep="\n")

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

Submission status: Accepted

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

asc_list = sorted(p)
count = 0
for a, b in zip(p, asc_list):
    if a != b:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for x, y in zip(p, p_sort):
    if x != y:
        cnt += 1

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
from itertools import permutations,  combinations, accumulate
from functools import *
from collections import deque, defaultdict, Counter
from heapq import heapify, heappop, heappush, heappushpop

INF = float('inf')
NIL = - 1

""" Input
S = input().rstrip()
N, A, B = map(int, input().split())
A = np.array(input().split(), dtype=np.float64)
D = [int(input()) for _ in range(N)]
AB = [[int(x) for x in input().split()] for _ in range(N)]
PX = [[int(x) for x in input().split()] for _ in range(Q)]
"""

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

p_sort = sorted(P)

counter = 0
for x, y in zip(P, p_sort):
    if x != y:
        counter += 1

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

# def main():

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    p = [int(a) for a in input().split()]
    p_ = sorted(p)
    cnt = 0
    for i in range(N):
        if p[i] != p_[i]:
            cnt += 1
    if cnt <= 2:
        print("YES")
    else:
        print("NO")
main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    p_l = list(map(int, input().split()))
    cnt = 0
    for i in range(n):
        if p_l[i] == i + 1: continue
        else: cnt += 1
    if cnt > 2: print("NO")
    else: print("YES")
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7


def main():
  N = int(readline())
  P = np.array(readline().split(), np.int64)
  A = np.array(range(1,N+1), np.int64)
  B = np.count_nonzero(P-A)
  if B==0 or B==2:
    ans = 'YES'
  else:
    ans = 'NO'
  print(ans)
  
  
  
  
      
if __name__ == '__main__':
  main()

Submission status: Accepted

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

cnt = sum([p1 != p2 for p1, p2 in zip(P, sorted((P)))])
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(0, N):
    if p[i] != i + 1:
        count += 1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    p = lmi()
    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 print('NO')

if __name__ == "__main__":
    main()

Submission status: Accepted

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

print("YES")

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(1, N + 1):
    if cnt == 3:
        print("NO")
        exit()
    elif i != p[i - 1]:
        cnt += 1

print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
count=0
num=0
for i in p:
    if i!=num+1:
        count+=1
    num+=1
if count>2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

wrong = 0
for i, p in enumerate(map(int, input().split()), start=1):
  wrong += int(i != p)

if wrong == 0 or wrong == 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:
    continue
  else:
    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()))
p1 = sorted(p)
count = 0
for i in range(n):
  if p[i]!=p1[i]: 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()]
z = 0
for i in range(n):
  if p[i] != i + 1:
    z += 1
if z <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def is_sorted(t_list):
    for i in range(len(t_list)):
        if t_list[i] != i + 1:
            return False
    return True


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

if is_sorted(p_list):
    print('YES')
    exit()

for i in range(n):
    for j in range(i+1, n):
        p_list[i], p_list[j] = p_list[j], p_list[i]
        if is_sorted(p_list):
            print('YES')
            exit()
        else:
            p_list[i], p_list[j] = p_list[j], p_list[i]

print('NO')

Submission status: Accepted

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

sorted_p = sorted(p)

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

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