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:
from enum import Enum
from queue import Queue
import collections

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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:
N = int(input())
P = list(map(int,input().split()))
ans = 'NO'
Pre = sorted(P)
pi = -1
pj = -1
for i in range(N):
  if P[i] != Pre[i]:
    pi = i
    break
for j in range(pi+1,N):
  if P[j] != Pre[j]:
    pj = j
    break
if pi != -1 and pj != -1:
  P[pi], P[pj] = P[pj],P[i]
  
if P == Pre:
  ans = 'YES'
print(ans)

Submission status: Accepted

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

sort = [i for i in range(1, N+1)]
t = 0
for i, n in enumerate(sort):
  if  P[i] != n and t < 2:
    t +=1
    continue
  elif P[i] != n and t > 1:
    print("NO")
    quit()
print("YES")

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

Submission status: Accepted

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

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

    if count > 2:
        flag = False
        print('NO')
        break


if flag:
    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(N):
    if p[i] != q[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()))

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

temp = 0

for i, j in enumerate(p):
    if nP[i] != j:
        temp += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)


def li(): return map(int, stdin.readline().split())


def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())


def lf(): return map(float, stdin.readline().split())


def ls(): return stdin.readline().split()


def ns(): return stdin.readline().rstrip()


def lc(): return list(ns())


def ni(): return int(stdin.readline())


def nf(): return float(stdin.readline())

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

Q = sorted(P)
check = 0
for i in range(len(P)):
    if P[i] != Q[i]:
        check += 1

    if check > 2:
        print("NO")
        exit()

if check == 1:
    print("NO")
    exit()

print("YES")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
p = list(map(int, str(input()).split()))

# 昇順のリストと比較し、２箇所以上異なっていたらソート不可能
# ２箇所ないし０箇所であればYESを出力する

target_list = [i for i in range(1,N+1)]

count = 0
for i in range(N):
    if target_list[i] != p[i]:
        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()))

flag = True
t = []

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


if len(t) == 0 or len(t) == 2:
    ans = "YES"

else:
    ans = "NO"

print(ans)

Submission status: Accepted

Solution code:

N = int(input())
p = list(map(int, input().split()))
count = 0
for i ,pi in enumerate(p):
    if i+1 != pi:
        count += 1

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

Submission status: Accepted

Solution code:
# ABC135b
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

for i in range(len(p)):
    for j in range(i, len(p)):
        p2 = sorted(p)
        tmp = p2[i]
        p2[i] = p2[j]
        p2[j] = tmp
        if (p == p2):
            print("YES")
            exit()
print("NO")

Submission status: Accepted

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

n = int(input())
t = input().split()
p = []
for i in t:
    p.append(int(i))

q = sorted(p)
c = 0
for i,j in enumerate(p):
    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()))

C = [P[i - 1] == i for i in range(1, N + 1)].count(0)

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(i) for i in input().split()]
q = [0] * N
for i in range(N):
    q[i] = i + 1
count = 0
for i in range(N):
    if(p[i] != q[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()))

Ps = sorted(P)

cnt = 0
for i in range(N):
    if Ps[i]!=P[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()))

cnt = 0
for i in range(N):
  if i + 1 != p[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()))
a=sorted(p)

for i in range(n-1):
    for j in range(i,n):
        p[i],p[j]=p[j],p[i]
        if p==a:
            print("YES")
            exit()
        p[i],p[j]=p[j],p[i]
print("NO")

Submission status: Accepted

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

res = []
count = 0
for i in range(int(N)):
    res.append(i+1)
for i,j in zip(res,parents):
    if not i==j:
        count += 1
if count > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
n = int(input())
xs = [int(s) for s in input().split()]
ys = sorted(xs)

cnt = sum(1 for (x, y) in zip(xs, ys) if x != y)

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

Submission status: Accepted

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

diff_count = 0
diff_formar = 0
diff_latter = 0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

if count == 2:
    print('YES')
elif count == 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:
    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())
L=list(map(int,input().split()))
R=[i+1 for i in range(N)]
C=[0 for i in range(N)]

for i in range(N):
  C[i]=L[i]-R[i]

D=np.array(C)
if np.count_nonzero(D)>2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
q = list(range(1, N+1))

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

if cnt in [0, 2]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = input()

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

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


if cnt in [0, 2]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

count = 0
for i, p in enumerate(P):

    # if i == 0:
    #     continue
    if i+1 != P[i]:
        count += 1

    if count > 2:
        break

if count > 2:
    print('NO')

else:
    print('YES')

Submission status: Accepted

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


psort = sorted(p)

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

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

Submission status: Accepted

Solution code:
N = int(input())

S = input()
S = S.split()

P = list(map(int, S))

P_sort = sorted(P)
count = 0


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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
import sys
N=int(input())
P=list(map(int,input().split()))
for i in range(N):
    for j in range(N):
        Pt=copy.copy(P)
        Pt[j]=P[i]
        Pt[i]=P[j]

        tmp=Pt[0]
        for k,t in enumerate(Pt[1:]):
            if t>tmp:
                tmp=t
            else:
                break
            if k==N-2:
                print('YES')
                sys.exit()
            
print('NO')

Submission status: Accepted

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

cnt = 0
for n in range(1, N+1):
    if p[n-1] != n:
        cnt += 1
if cnt == 2 or cnt == 0:
    result = 'YES'
else:
    result = 'NO'
print(result)

Submission status: Accepted

Solution code:
from sys import stdin

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

a=0

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

if a==n or a==n-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] is not i+1:
        count += 1

if count <= 2:
    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(0,n):
	if a[i]!=i+1:
		count += 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

l = list(range(1, n+1))

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

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

if ans <= 2:
    print('YES')
elif ans > 2:
    print('NO')

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
import copy
B = copy.deepcopy(A)
B.sort()
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())
l=list(map(int,input().split()))
t=[i+1 for i in range(n)]
c=0
for i in range(n):
  if t[i]!=l[i]:c+=1
if c>2:print("NO")
else:print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p = [int(_) for _ in input().split()]
flg = False
flg1 = True
for i in range(n):
    for j in range(n):
        flg1 = True
        q = p[:]
        q[i]=p[j]
        q[j]=p[i]
        for k in range(n):
            for l in range(n):
                if k < l and q[k]>q[l]:
                    flg1 = False
                    break
                if l==n-1 and k==n-1 and flg1:
                    flg = True
        
if flg:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p_list = list(map(int, input().split()))
# print(p_list)
p_list_sorted = sorted(p_list)
# print(p_list_sorted)
diff_num = 0
for i in range(n):
    if (p_list[i] - p_list_sorted[i]) != 0:
        diff_num += 1

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

Submission status: Accepted

Solution code:
import sys,heapq,math,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def setrlim():
  sys.setrecursionlimit(250000)
  soft,hard = resource.getrlimit(RLIMIT_STACK)
  # setrlimit works on ubuntu (and atcoder), but not on WSL
  #resource.setrlimit(RLIMIT_STACK, (128*1024*1024,hard))

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

Submission status: Accepted

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

q=sorted(p)

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
P = list(map(int,input().split()))
count = 0

for i in range(len(P)):
	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())
p=list(map(int,input().split()))
_p=sorted(p)
c=0
for a,b in zip(p,_p):
    if a!=b:
        c+=1
print('YES' if c<=2 else 'NO')

Submission status: Accepted

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

ans = 'NO'
p_sorted = sorted(p)

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

if cnt < 3:
    ans = 'YES'
print(ans)

Submission status: Accepted

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

N = int(input())
P = inpl()
Q = sorted(P)

OK = False
for i in range(N):
    for j in range(N):
        P[i], P[j] = P[j], P[i]
        if P == Q:
            OK = True
        P[i], P[j] = P[j], P[i]
print("YES" if OK else "NO")

Submission status: Accepted

Solution code:
#B
n = int(input())
l = list(map(int,input().split()))
count = 0

l_asc = sorted(l)
    
for i in range(n):
    if l[i] != l_asc[i]:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

for idx,v in enumerate(t):
  if count > 2:
    break
  if idx+1 != v:
    count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=input()
p=np.array([int(n) for n in input().split()])
s=sorted(p)
r=p-s
d=np.count_nonzero(r!=0)
if (d>2):
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
pn=list(map(int,input().split()[:N]))
an=list(range(1,N+1))

con=[(x-y)!=0 for (x,y) in zip(pn,an)].count(True)
if con==2 or con ==0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
len_arr = int(input())
arr = [int(x) for x in input().split(' ')]
increasing = range(1, len_arr+1)

rep_inc = 0

for i, inc in zip(arr, increasing):
    if i != inc:
        rep_inc += 1

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

exit(0)

Submission status: Accepted

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

Submission status: Accepted

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

counts = []

for i in range(len(p)):
    if p[i] == i + 1:
        counts.append(i)

if len(counts) == N - 2:
    print('YES')
elif len(counts) == N:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())

p = list(int(_) for _ in input().split()) 
q = p.copy()
p.sort(reverse=False)
#print(p)
#print(q)
cnt = 0

for i in range(n):
	if p[i] != q[i]:
		cnt += 1
#print(cnt)
if cnt >= 3:
	print('NO')
else:
	print('YES')

Submission status: Accepted

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

l = sorted(p)
count = 0

for a, b in zip(p, l):
    if (a is not b): count += 1

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

Submission status: Accepted

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

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

diff_count = 0
for i, p_i in enumerate(p):
    if i + 1 != p_i:
        diff_count += 1

print('YES' if diff_count in [0, 2] else 'NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
m=int(input())
n=input().split(" ")
for i in range(m):
    n[i]=int(n[i])
  
s=sorted(n,reverse=False)
d=0
for i in range(m):
    if not (n[i]==s[i]):
        d=d+1
#print(d)
if(d < 3):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
l1=list(map(int,input().split()))
l2=sorted(l1)
if l1==l2:
        print("YES")
        exit()
k=0
for i in range(n):
        if l1[i]!=l2[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()))

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

count=0
for i in range(0,N):
  diff=S[i]-p[i]
  #print(diff)
  if diff!=0:
    count=count+1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sortP = sorted(P)
sumChk = sum([a != b for a, b in zip(P, sortP)])

# 2箇所を入れ替えたら昇順になる ＝ 昇順に並べ替えた配列と要素ごとに比較して、違いは2箇所だけ。または同一についてもYES判定
if sumChk == 2 or sumChk == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

origin_p = P[:]
sorted_p = sorted(P[:])

#print(origin_p)
#print(sorted_p)

counter = 0
for a, b in zip(origin_p, sorted_p):
    if a == b:
        continue
    else:
        counter += 1
        
if counter <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split(" ")))
X = sorted(L)
cnt = 0
for a,b in zip(L,X):
    if a != b:
        cnt += 1
        if cnt >= 3:
            print("NO")
            break
if cnt <= 2:
    print("YES")

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=k+1

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

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(x) for x in input().split(' ')]
ppp = arr.copy()

arr.sort()

dc = 0

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

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

Submission status: Accepted

Solution code:
N = input()
ps = input().split()
c = 1
diff = 0
for p in ps:
    if c != int(p):
        diff += 1
    c += 1
if diff > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

c = 0

for i in range(n) :
  if p[i] != i+1 :
    c += 1
    if c>2 :
      break

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

strange=[]
for i in range(1,1+N):
  if p[i-1]!=i:
    strange.append(i)
if len(strange)==2:
	flag1=strange[0]==p[strange[1]-1]
	flag2=strange[1]==p[strange[0]-1]
	flag=flag1*flag2
if len(strange)==0:
  print('YES')
elif len(strange)!=2:
  print('NO')
elif flag:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import numpy as np

N = input()
N = int(N)
p = input().split()
p_l = [int(s) for s in p]
p_def = np.array(p_l)
p_sort = np.sort(p_def)

comp = p_def == p_sort
diff = len((np.where(comp==False))[0])
if diff <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

n=inp()
p = inpl()
sorted_p = sorted(p)
diff=0
for i in range(n):
    if p[i] != sorted_p[i]:
        diff += 1
if diff == 0 or diff == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
N=list(map(int,input().split()))
N_another=sorted(N)
count=0
if N==N_another:print("YES")
else:
    for i,j in enumerate(N):
        if j!=N_another[i]:
            count +=1
    if count>2:
        print("NO")
    else:
        print("YES")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy
N=int(input())
p=[int(_) for _ in input().split()]
sorted_p=sorted(p)
if p==sorted_p:
    print('YES')
else:
    flag=0
    for i in range(N):
        for j in range(N):
            new_p=copy.copy(p)
            new_p[i]=p[j]
            new_p[j]=p[i]
            if new_p==sorted_p:
                flag=1
    if flag==0:
        print('NO')
    else:
        print('YES')

Submission status: Accepted

Solution code:
import numpy as np
N = input()
p= np.array( list(map(int,input().split())))
pp = np.sort(p)
if (np.count_nonzero(pp-p)==2) or (np.count_nonzero(pp-p)==0):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
#[int(i) for i in input().split()]
#int(input())
#list(input())

N=int(input())
p=[int(i) for i in input().split()]
P=sorted(p)
cnt=0
for i in range(N):
    if p[i]!=P[i]:
        cnt+=1
    if cnt>=3:
        break
print("YES" if cnt<=2 else "NO")

Submission status: Accepted

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

q = list(range(1, n+1))

c = 0

for i in range(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()))
q=sorted(p)
d=0
for i in range(len(p)):
    if p[i] != q[i]:
        d += 1
if d <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
num = [int(_) for _ in input().split()]
exp = num[:]
order = sorted(num)
found = False
for i in range(n):
    for j in range(1,n):
        exp[i] = num[j]
        exp[j] = num[i]
        if exp == order:
            print("YES")
            found = True
            break
        exp = num[:]
    if found:
        break

if not found:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())

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

flag = True

tmp = 0
for i in range(N):
    if P[i] > tmp:
        tmp = P[i]
    else:
        flag = False
        break

if flag:
    print('YES')
    exit()

hogehoge = {}

for i in range(N):
    hogehoge[i+1] = P[i]

cnt = 0
for key, value in hogehoge.items():
    if key != value:
        cnt += 1

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

Submission status: Accepted

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

for i in range(N):
    for j in range(i,N):
        cond_al = al.copy()
        cond_al[i],cond_al[j] = cond_al[j],cond_al[i]
        if(cond_al==ansal):
            print("YES")
            exit(0)

print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline().rstrip())
PList=list(map(int,sys.stdin.readline().split()))

PListSort=sorted(PList)

exchangeCounter=0
for i in range(N):
  if PList[i] != PListSort[i]:
    exchangeCounter+=1
    
if exchangeCounter <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import copy
N = int(input())
p = list(map(int,input().split()))
q = copy.copy(p)
q.sort()
ans = "NO"
for i in range(N):
    for j in range(i,N):
        r = copy.copy(p)
        a,b = r[i],r[j]
        r[i],r[j] = b,a
        if r==q:ans="YES"
print(ans)

Submission status: Accepted

Solution code:
import copy

n = int(input())
lis = list(map(int, input().split()))
list = copy.deepcopy(lis)
true_list = [ i+1 for i in range(n)]
k = 0

for i in range(n):
    for j in range(n):
        if k == 0:
            list = copy.deepcopy(lis)
            list[i], list[j] = list[j], list[i]
            if list == true_list:
                print("YES")
                k = 1


if k == 0:
    print("NO")

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

Submission status: Accepted

Solution code:
n = int(input())

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

w = 0

n_list = [i for i in range(1,n+1)]

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

if w <= 2:
    print('YES')

else:
    print('NO')

Submission status: Accepted

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

s = sorted(l)

dif = 0
for i in range(len(l)):
    if l[i]!=s[i]:
        dif += 1

if dif == 0:
    print("YES")
elif dif == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

for i in range(1,n+1):
  if i != pn[i-1]:
    cnt += 1
if cnt > 2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
import sys
import numpy as np
from scipy.misc import comb
sys.setrecursionlimit(1000000)
 
n = int(input())

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

miss = 0

for i in range(n):
    if miss >= 3:
        print("NO")
        exit()
    if p[i] != i+1:
        miss += 1
    

print("YES")
exit()

Submission status: Accepted

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

f = False
#間違い,本来
pre = (0,0)
for i in range(N):
    if not l[i]==i+1:
        if f:
            print('NO')
            exit()
        if not pre[0]==0:
            if pre[0] == i+1 and pre[1]+1==l[i]:
                f = True
                continue
            else:
                print('NO')
                exit()
        else:
            pre = (l[i],i)
print('YES')

Submission status: Accepted

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

a=0
cnt=0
for i in range(N):
   
    if P[i]!=i+1:
        if cnt==0:
            a=P[i]
            ai=i
            cnt=1
        elif cnt==1:
            P[ai]=P[i]
            P[i]=a
            break

for i in range(N):
    if P[i]!=i+1:
        print("NO")
        break
    if i+1==N:
        print("YES")