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().strip().split()))
count = 0
for i in range(N):
  if i+1 == A[i]:
    count+=0
  else:
    count+=1
if count == 2:
  print("YES")
elif count ==0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())

# lst = input().split() # ←だとWAになる int として処理する必要がある
lst = list(map(int,input().split()))

cnt = 0

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

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

Submission status: Accepted

Solution code:
N = int(input())

lst = list(map(int,input().split()))
cnt = 0

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

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

Submission status: Accepted

Solution code:
import copy
import sys
n = int(input())
lst1 = list(map(int,input().split()))
lst2 = copy.copy(sorted(lst1))

c=0
for i in range(n):
    if lst1[i]==lst2[i]:
        pass
    else:
        c+=1
        if c > 2:
            print("NO")
            sys.exit()
            
print("YES")

Submission status: Accepted

Solution code:
import sys
N = int(input())
p = list(map(int,input().split()))
l = [0]*3
a = 0
for i in range(1,N+1):
    if p[i-1] != i:
        l[a] = i-1
        a += 1
        if a==3:
            print("NO")
            sys.exit()
temp = p[l[0]]
p[l[0]] = p[l[1]]
p[l[1]] = temp
for i in range(1,N+1):
    if p[i-1] != i:
        print("NO")
        sys.exit()
print("YES")

Submission status: Accepted

Solution code:
n=int(input())
count=0
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:
import sys

N = int(input())
p = list(map(int,input().split()))
p_sorted = sorted(p)
cnt = 0
for i in range(N):
    if p[i] != p_sorted[i]:
        cnt += 1
    if cnt==3:
        print("NO")
        sys.exit()

print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int, input().split()))
s = [x for x in range(1,N+1) if x!=p[x-1]]
if len(s)<3:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

c=0
m = 0

for i in d:
  m = m+1
  if i != m:
    c = c+1
    if c>2:
      print("NO")
      exit()
print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = input().split()
P = sorted(p, key = float)
w = 0
i = 0
while i <= N-1:
  if p[i] == P[i]:
    i += 1
  
  else:
    w += 1
    i += 1
    
if w == 0 or w == 2:
  print('YES')
  
else:
  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 sorted_p[i] != p[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()))
i = 0
k = 0
for x in A:
  i += 1
  if i == x:
    k += 0
  else:
    k += 1
if k <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
arr = list(map(int, input().split()))
# n = len(arr)

graph = defaultdict(int)
for i in range(n):
	graph[i+1] = arr[i]

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

def dfs(src):
	global visited
	stack = [src]
	visited.add(src)
	cycle_size = 0
	while stack:
		v = stack.pop()
		if not graph[v] in visited:
			visited.add(graph[v])
			stack.append(graph[v])
			cycle_size += 1
	return cycle_size

visited = set()
count = 0
for ele in arr:
	if not ele in visited:
		cs = dfs(ele)
		count += cs
print("YES" if count <= 1 else "NO")

Submission status: Accepted

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

sorted_arr = sorted(arr)

count = 0
for i in range(n):
	if arr[i] != sorted_arr[i]:
		count += 1
if count <= 2:
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

num_sort = sorted(num)
ans = 0

for i in range(N):
  if num[i] == num_sort[i]:
    continue
  else:
    ans += 1
    
if ans <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
cnt = 0
for i in range(n):
    if x[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()))

p_sorted = sorted(p)
delta = [x - y for (x, y) in zip(p, p_sorted)]

if sum( x != 0 for x in delta) == 2 or sum( x != 0 for x in delta) == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from copy import deepcopy
from collections import deque
sys.setrecursionlimit(20000000)


def main():
    n = int(input())
    nums = list(map(int, input().split()))
    acs_nums = deepcopy(nums)
    acs_nums.sort()

    count = 0

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

    if count < 3:
        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()]

count = 0
for i, j in enumerate(p):
    if i != j-1:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

p_sort=sorted(p_list)
cnt=0
for a,b in zip(p_list,p_sort):
    if a!=b:
        cnt+=1

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

Submission status: Accepted

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

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

p_ori = p[:]

p.sort()
p_sorted = p[:]

if p_ori == p_sorted:
    print("YES")
else:
    p_ori = np.array(p_ori)
    p_sorted = np.array(p_sorted)
    p_diff = p_ori - p_sorted

    if(sum(p_diff != 0) == 2):
        if p_diff.sum() == 0:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

Submission status: Accepted

Solution code:
input();p=map(int,input().split())
diff=0
for idx,i in enumerate(p,1):
    if idx!=i:
        diff+=1
print("YES"if diff==0 or diff==2 else"NO")

Submission status: Accepted

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

count = 0
for i in range(n):
    if nums[i] != i + 1:
        count += 1
if count > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

q = []
for x in range(N):
    q.append(x+1)

i = 0
c = 0
ans = "NO"
while(i < N):
    if p[i] != q[i]:
        c += 1
    if c > 2:
        break
    i += 1

if (c == 2) or (c == 0):
    ans = "YES"

print(ans)

Submission status: Accepted

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

i=1
count=0
for item in r:
  if item!=i:count+=1
  i+=1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
L = list(map(int, input().split()))
Ls = sorted(L)

cnt = 0
for i in range(N):
    if L[i] != Ls[i]:
        cnt += 1
    if cnt > 2:
        print("NO")
        sys.exit()

print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for l in range(len(li)):
  if li[l] != li_sorted[l]:
    cnt += 1
    if cnt > 2:
      print('NO')
      exit()
print('YES')

Submission status: Accepted

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

n = int(input())

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

for i in range(len(p) - 1):
    pi = p[i]
    if p[i] != i+1:
        if p[p[i] - 1] == i + 1:
            p[i], p[pi - 1] = p[pi - 1], p[i]
            f += 1
        else:
            f = 2

if 0 <= f <= 1:
    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(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()))
max_p=p[0]
cnt=0
for i in range(n):
    if p[i]!= i+1:
        cnt+=1
if cnt>2:
    print('NO')
else :
    print('YES')

Submission status: Accepted

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

counter = 0
for i in range(len(numbers)):
    if numbers[i] != sorted_numbers[i]:
        counter += 1

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

Submission status: Accepted

Solution code:
n=int(input())
p = list(map(int,input().split()))	
ans="NO"
tank=0
check=[(i+1) for i in range(n)]
if(p==check):
    ans="YES"
    
for i in range(n-1):
    for j in range(n-i-1):
        j+=i+1
        tank=p[i]
        p[i]=p[j]
        p[j]=tank
        if(p==check):
            ans="YES"
        tank=p[i]
        p[i]=p[j]
        p[j]=tank
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def is_sorted(li):
  for i in range(len(li)-1):
    if li[i] > li[i+1]:
      return False
  return True

ans = is_sorted(p)

def check():
  for i in range(N-1):
    for j in range(i+1, N):
      new = p[:]
      new[j] = p[i]
      new[i] = p[j]
      if is_sorted(new):
        return True
  return False

if ans:
  print("YES")
else:
  if check():
    print("YES")
  else:
    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 == 0 or k == 2:
    print("YES")

else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

ans = False
for i in range(N):
    for j in range(i, N):
        p[i], p[j] = p[j], p[i]
        for k in range(N):
            if p[k] != k+1:
                p[i], p[j] = p[j], p[i]
                break;
            if k == N-1: ans = True
if ans: print('YES')
else: print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

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

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

Submission status: Accepted

Solution code:
#B問題
N = int(input())
P = list(map(int,input().split()))
Q = P[::]

P.sort()
diff = 0
for i in range(N):
    if P[i] != Q[i]:
        diff+=1
        
if diff <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split()))
q = list(p)
ans = 'NO'
# i<j piとpjを入れ替える

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

Submission status: Accepted

Solution code:
import copy
n = int(input())
p = list(map(int,input().split()))
r = list()
for k in range(n):
    r.append(k+1)
if p == r:
    print("YES")
    exit()
for i in range(0,n-1):
    for j in range(i+1,n):
        q = copy.copy(p)
        q[j] = p[i]
        q[i] = p[j]
        if q == r:
            print("YES")
            exit()

print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
    if ps[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()))
#b=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())
P=list(map(int,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()))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(p) for p in input().split()]
sorted_P = sorted(P)
counter = 0
for p,sp in zip(P, sorted_P):
    if p != sp:
        counter += 1
        if counter > 2:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
P = list(map(int, input().split()))
cnt = 0
for i in range(1, N+1):
    if P[i-1] != i:
       cnt += 1 
print("YES" if cnt < 3 else "NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding:utf-8 -*-

def swap_0_1(p):
    num_change = len([i for i in range(len(p)) if p[i] != i+1])
    if num_change in [0, 2]: return True
    return False

def main():
    n = int(input())
    p = list(map(int, input().split()))
    if(swap_0_1(p)): print("YES")
    else: print("NO")

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(n):
    if err == 3:
        print("NO")
        sys.exit()
    if not p[i] == i+1:
        err += 1
print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
N = int(input())
M = list(map(int,input().split(" ")))
M_sorted=copy.copy(M)
M_sorted.sort()
bool_list=[]
 
for i in range(N):
  if M[i]==M_sorted[i]:
    bool_list.append(False)
  else:
    bool_list.append(True)
if sum(bool_list) in [0,2]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
sample = sorted(p)
count = 0
for j in range(len(p)):
  if p[j] != sample[j]:
    count += 1
if count < 3:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import copy
N = int(input())
M = list(map(int,input().split(" ")))
M_sorted=copy.copy(M)
M_sorted.sort()
bool_list=[]

for i in range(N):
  if M[i]==M_sorted[i]:
    bool_list.append(False)
  else:
    bool_list.append(True)
if sum(bool_list) in [0,2]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
#N = 要素数　p = 配列
N = int(input())
p = list(map(int, input().split()))
#count= 番地と入力番号が一致しない数字の数
count = 0 

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

Submission status: Accepted

Solution code:
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:
import sys
input = sys.stdin.readline

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:
# coding: utf-8
n = int(input())
p = list(map(int,input().split()))
count = 0
list_base = []
index = []
for i in range(n):
    list_base.append(i+1)

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

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

Submission status: Accepted

Solution code:
import copy 

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

true_num = copy.deepcopy(num)
true_num = sorted(true_num)

swap_org=[]
swap_true=[]

count=0
for x,y in zip(num,true_num):
    if x != y:
        count += 1
        swap_org.append(x)
        swap_true.append(y)


for i in range(len(swap_org)-1):
    if swap_org[i] == swap_true[i+1] and swap_org[i+1] == swap_true[i]:
            count-=2
if count>0:
    print("NO")
else:
    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]!=sorted(P)[i]:
        a+=1
print("YES" if a==0 or a==2 else "NO")

Submission status: Accepted

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

new_p = sorted(p)
c = 0

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

Submission status: Accepted

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

a = []
for i in range(len(p)-1):
	if p[i]>=p[i+1]:
		a.append(i)
if len(a)==2:
	c = p[a[0]]
	d = p[a[1]+1]
	_p[a[0]]=d
	_p[a[1]+1]=c
	p.sort()
	if _p==p:
		print("YES")
	else:
		print("NO")
elif len(a)==0:
	print("YES")
else:
	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(N):
  if Q[i]!=P[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, j in zip(p, sorted(p)):
    if i != j:
        count += 1
ans = 'YES' if count <= 2 else 'NO'
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
input()
P = list(map(int, input().split()))

Ps = list(sorted(P))

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

print('YES' if c <= 2 else '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 == 3:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

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

N = int(input())
p = getlist()

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

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

Submission status: Accepted

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

p_sort = sorted(p)
x = 0

for i in range(N):
    if p[i] == p_sort[i]:
        continue
    else:
        x += 1

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split(' ')))
ans = 0
P_r = sorted(P)
for i in range(0,N):
  if P[i] != P_r[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()))

sp = list(range(1,N+1))

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

Submission status: Accepted

Solution code:
def solve(l:list):
    count = 0
    for a, b in zip(l, sorted(l)):
        if a != b:
            count += 1
    print("YES") if count <= 2 else print("NO")
    return

def main():
    input()
    l = list(map(int, input().split(" ")))
    solve(l)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans_mix=[]
is_possible = False

if p==ans:
    is_possible = True

for i in range(0, n-1):
    for j in range(1, n):
        a=ans.copy()
        tmp=a[i]
        a[i]=a[j]
        a[j]=tmp
        if a==p:
            is_possible = True

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

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
p=np.array(list(map(int, input().split())))
q=np.arange(N)+1

temp=np.sum(~(p==q))
if temp==0 or temp==2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
sorted_l = sorted(l)
flag = 0

for num, s_num in zip(l, sorted_l):
  if num != s_num:
    flag += 1

if flag == 0 or flag == 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(1, n+1):
  if p[i-1] != 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()))
n = 1
ans = 0

for Pi in p:
    if Pi != n:
        ans += 1
    if ans > 2:
        print("NO")
        exit()
    n += 1
print("YES")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
P = list(map(int, 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')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
_ = input()
p = list(map(int, input().strip().split(' ')))
print('NO' if sum(1 for a,b in zip(sorted(p),p) if a != b) > 2 else 'YES')

Submission status: Accepted

Solution code:
import numpy as np
if __name__ == '__main__':
	N = int(input())
	P = np.array(list(map(int, input().split())))
	Tr = np.array([i+1 for i in range(N)])
	ans = Tr - P
	ans = N - np.count_nonzero(ans == 0)
	if ans == 2 or ans == 0:
		print('YES')
	else:
		print('NO')

Submission status: Accepted

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

pis = sorted(pi)
#print(pis)

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

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