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

count =0
for i in range(N):
    if arr[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()))
sp = sorted(p)
ans = []

for i in range(n):
    if p[i] != sp[i]:
        ans.append(p[i])

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b={}
for i in range(len(a)):
    b[a[i]]=i
a.sort()
x=[]
for i in range(n):
    if i!=b[a[i]]:
        x.append([i,b[a[i]]])
if(len(x)>2):
    print("NO")
elif(len(x)==2 and x[0][0]!=x[1][1]):
    print('NO')
else:
    print("YES")

Submission status: Accepted

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

l = list()
for i in range(1,N+1):
    l.append(i)
cnt = 0
for i, c in enumerate(p):
    if c != l[i]:
        cnt += 1
if cnt == 2 or cnt ==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
def or01swap(n, pli):

    plis = copy.copy(pli)
    plis.sort()
    count = 0

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

    return "YES" if count <= 2 else "NO"

def main():
    n = int(input())
    pli = list(map(int, input().split()))
    print(or01swap(n, pli))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

# check
# 考え方参考：https://note.com/tanon_cp/n/n6b51b9a57076
sorted_P = sorted(P)
change_idx_cnt = 0
for sp, p in zip(sorted_P, P):
    if sp != p:
        change_idx_cnt += 1

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,input().split()))
t = 0
for i in range(n):
  s.append(i)

for i in range(n):
  if(s[i] == i + 1):
    t += 0
  else:
    t += 1
if(t == 0 or t == 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+=0
    else :
        ans+=1
print("YES"if ans==0 or ans==2 else "NO")

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
p = list(map(int, input().split()))
p_test = sorted(copy.copy(p))
count = 0

for i, j in zip(p, p_test):
  if(i != j):
    count += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt=0
i=0
while i<N:
    if not List[i]==i+1:
        cnt+=1
    i+=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)
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())
cnt=0
a = list(map(int,input().split()))
for i in range(n):
    if a[i]!=i+1: cnt+=1
if cnt<=2: print("YES")
else: print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def judge(ps):
    sorted_ps = sorted(ps.copy())
    count = 0
    if ps == sorted_ps:
        return True

    for i,p in enumerate(ps):
        if p != sorted_ps[i]:
            count += 1
        if count > 2:
            return False

    if count == 2:
        return True


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

if judge(ps):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys

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

ct = 0
for i in range(n):
    if P[i] != i + 1:
        ct += 1
    if ct > 2:
        print('NO')
        sys.exit()
if ct == 2 or ct == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
geta = lambda fn: map(fn, readline().split())
gete = lambda fn: fn(readline())

# sys.setrecursionlimit(10**5)

def main():
    N = gete(int) 
    p = list(geta(int))
    q = sorted(p)

    cnt = 0
    for pi, qi in zip(p,q):
        if pi != qi:
            cnt += 1
    
    if cnt <= 2:
        print('YES')
    else:
        print('NO')
	
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
ps = li()
cnt = 0
for i in range(n):
    if ps[i] != i+1:
        cnt += 1
if cnt == 0 or cnt == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
s=[0]*n
s[:]=map(int,input().split())
import copy
s0=copy.copy(s)
s.sort()
a=[1 if s[ii]!=s0[ii] else 0 for ii in range(len(s))]
#print(s0)
#print(s)
if sum(a)<=2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
def myAnswer(N:int,P:list) -> str:
   flag = False
   for i in range(N):
      if(i+1 != P[i]):
         if(flag):
            return "NO"
         flag = True
         P[P.index(i + 1)] = P[i]
         P[i] = i + 1
   return "YES"

def modelAnswer():
   return
def main():
   N = int(input())
   P = list(map(int,input().split()))
   print(myAnswer(N,P))
if __name__ == '__main__':
   main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
n=int(input())
a=list(map(int,stdin.readline().split()))
c=0
for i in range(1,n+1):
    if(a[i-1]!=i):
        c+=1
if(c>2):
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri May 15 00:38:52 2020

@author: Kanaru Sato
"""

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

yesflag = 1
a = [p[i] for i in range(n)]
for k in range(n-1):
    if a[k] > a[k+1]:
        yesflag = 0
        break

if yesflag == 0:
    for i in range(n-1):
        for j in range(i+1,n):
            a = [p[i] for i in range(n)]
            tmp = a[i]
            a[i] = a[j]
            a[j] = tmp
            yesflag = 1
            for k in range(n-1):
                if a[k] > a[k+1]:
                    yesflag = 0
                    break
            if yesflag == 1:
                break
        if yesflag == 1:
            break

if yesflag == 1:
    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 <= 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
    
if cnt <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
n = int(input())
p = list(map(int, input().split()))
count = 0
for i in range(n):
    if p[i] != i+1:
        count += 1
if count == 2 or count == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:

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

ps=sorted(p)
cnt=0
for i in range(len(p)):
    if p[i]!=ps[i]:
        cnt+=1
if cnt<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input().rstrip())
l1 = [int(i) for i in input().rstrip().split()]
l2 = sorted(l1)
result = 'YES'
dnum = 0
for i in range(N):
    if l1[i] != l2[i]:
        dnum = dnum + 1

    if dnum > 2:
        result = 'NO'
        break

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input());lista_1=[];contador=0;index=0
lista=list(map(int,input().split()))
for i in range(1,n+1):
    lista_1.append(i)
for j in lista:
    aaa=lista_1[index]
    if(j!=aaa):
        contador=contador+1
    index=index+1
if(contador<=2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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
if ans==n-2 or ans==n:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import sys
import math
import bisect

def solve(A):
    n = len(A)
    B = []
    for i in range(1, n + 1):
        B.append(i)
    if A == B:
        return True
    for i in range(n):
        for j in range(i + 1, n):
            A[i], A[j] = A[j], A[i]
            if A == B:
                return True
            A[i], A[j] = A[j], A[i]
    return False

def main():
    n = int(input())
    A = list(map(int, input().split()))
    if solve(A):
        print('YES')
    else:
        print('NO')

if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############
N = inputI()
P = inputIL()

memo = []
for i in range(1,N+1):
  if P[i-1] != i:
    memo.append((P[i-1],i))
    
if len(memo) == 0:
  print("YES")
  exit()
if len(memo) == 2:
  if memo[0][0] == memo[1][1]:
    print("YES")
    exit()
print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from math import factorial as fact
import math
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
from collections import deque


#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(range(n),r))



def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets

#公約数の列挙
def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	#x:縦，y:横
	return fact(x+y)/fact(x)/fact(y)

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
	
def count_str(m):
	tmp={}
	for i in range(10):
		if i in tmp:
			tmp[i]=1
		else:
			tmp[i]+=1
	return tmp

def is_prime(n):
	if n==1: return True

	for i in range(2,int(math.sqrt(n))+1):
		if n%i==0:
			return False
	return True

n=input1()
p=input_array()
count=0
for i in range(n):
	if i+1!=p[i]:
		count+=1
	if count>2:
		print("NO")
		break
if count==2 or count==0:
	print("YES")

Submission status: Accepted

Solution code:
def solve(n, ls):
    cnt = 0
    for i in range(n):
        if ls[i] != i + 1:
            cnt += 1
        if cnt > 2:
            return False
    return True


n = int(input())
ls = list(map(int, input().split()))
if solve(n,ls):
    print("YES")
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#from math import gcd

from math import factorial as f

from math import ceil,floor,sqrt


import bisect
import re
import heapq


from copy import deepcopy
import itertools

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

yes = "Yes"
no = "No"

def main():
    n=ii()
    p = li()
    cnt = 0
    for i in range(1,n+1):
        if i!=p[i-1]:
            cnt+=1
    if cnt<=2:
        print('YES')
    else:
        print('NO')



main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
p = list(map(int, input().split()))
a = np.arange(1, n+1)
p = list(p)
p = np.array(p)
match = a == p
ans = 'YES'
if sum(match) == n - 2:
    pos = np.where(match == False)
    pos1 = pos[0][0]
    pos2 = pos[0][1]
    p[pos1], p[pos2] = p[pos2], p[pos1]
    if sum(a == p) != n:
        ans = 'NO'
elif sum(match) != n:
    ans = 'NO'
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
P=sorted(p)
n=0
for i in range(N):
    if p[i]!=P[i]:
        n+=1
if n<=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:
        x = p.index(i+1)
        p[i], p[x] = p[x], p[i]
        break
ok = True
for i in range(n):
    if p[i] == i+1:
        continue
    ok = False
    break
if ok:
    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()))
p_sort =sorted(p)
"""
max_p =max(p)
max_index =p.index(max_p)
min_p =min(p)
min_index =p.index(min_p)

#max minの位置を入れ替える
if max_index != N and min_index !=0:
p[max_index] =min_p
p[min_index] =max_p
"""
#昇順か判定
count =0
ans ="YES"
for i in range(N):
    if p[i] !=p_sort[i]:
        count +=1
        if 2 < count:
            ans ="NO"

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N-1):
  if A[i] == i+1:
    pass
  else:
    cnt += 1
    
if cnt <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

l=list(range(1,n+1))
k=0
for i in range(n):
    if p[i]!=l[i]:
        k+=1
if k==0 or k==2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
#from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n = I()
P = LI()
T = sorted(P)
for i in range(n):
    for j in range(n):
        tmp = deepcopy(P)
        tmp[i], tmp[j] = tmp[j], tmp[i]
        if T == tmp:
            print("YES")
            exit()

print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
print("YES" if (np.arange(1, int(input()) + 1, dtype=np.int8) != np.array(input().split(), dtype=np.int8)).sum() in [0, 2] else "NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
ps=list(map(int,input().split()))
c=0
for i,p in enumerate(ps, start=1):
    if not p==i:
        c+=1
print('YES' if c<3 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

k = s.copy()

k.sort(reverse = False)

t = 0

for i in range(n):
    if s[i] != k[i]:
        t += 1
        
if t<3:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys
import copy
N = int(input())
p = [int(x) for x in input().split()]
comp = [i for i in range(1, N+1)]

if p==comp:
    print("YES")
    sys.exit()

for i in range(N-1):
    for j in range(i+1, N):
        tmp = copy.copy(p)
        x = tmp[i]
        tmp[i] = tmp[j]
        tmp[j] = x
        if tmp==comp:
            print("YES")
            sys.exit()

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
print('NO' if cnt>2 else 'YES')

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    P = list(map(int, input().split()))
    bp = [False]*N
    for i in range(N):
        if P[i] == i+1:
            bp[i] = True
    counter = 0
    for b in bp:
        if not b:
            counter += 1
    if counter > 2:
        print('NO')
    else:
        print('YES')


solve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# B - 0 or 1 Swap

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

Submission status: Accepted

Solution code:
n = input()
a = tuple(map(int, input().split()))
p = sorted(a)
ans = 0
for x, y in zip(a, p):
    if x != y:
        ans += 1
if ans == 0 or ans == 2:
    print("YES")
else:
    print("NO")
from sys import argv
debug = 'a' in argv
if debug:
    print(ans)

Submission status: Accepted

Solution code:
import sys, heapq, bisect, math, fractions
from collections import deque

N = int(input())
P = list(map(int, sys.stdin.readline().rsplit()))

def ok():
    for i in range(N - 1):
        if P[i + 1] < P[i]:
            return False
    return True

if ok():
    print("YES")
else:
    for i in range(N - 1):
        for j in range(i + 1, N):
            P[i], P[j] = P[j], P[i]
            if ok():
                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()))
P = sorted(p)
cnt = 0
for i in range(len(p)):
    if P[i] != 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() ) )

P_ = sorted( P )

cnt = 0
for i in range( len( P )):
  if P[i] != P_[i]:
    cnt += 1
if cnt in [ 0, 2 ]:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    p = [int(x) for x in input().split()]
    count = 0
    for i, v in enumerate(p):
        if i + 1 != v:
            count += 1
    if count == 0 or count == 2:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rf = lambda: float(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]
rf_ = lambda: [float(_) for _ in rs().split()]

N = ri()
p = ri_()
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 = input()
p = list(map(int, input().split()))
sp = sorted(p)
swaped = 0
for i in range(len(p)):
  if p[i] != sp[i]:
    swaped += 1
    
if swaped <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

num = 0
for i, P in enumerate(Ps, start=1):
    if i != P:
        num += 1

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

Submission status: Accepted

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

print('YES' if sum(map(lambda x: x[0] == x[1], zip(p, sorted(p)))) >= N - 2 else 'NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

if len(swap_idx) == 2 or len(swap_idx) == 0:
    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())))

pp = np.array(sorted(p))-p
sum_=0
for i in pp:
  if i!=0: sum_+=1
if sum_>2: print('NO')
else: print('YES')

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(int(i) for i in input().split())
s = l.copy()
s.sort()

flag = False
if s == l:
  flag = True
else:
  for i in range(n):
    for j in range(i+1, n):
      t = l.copy()
      t[i], t[j] = t[j], t[i]
      if s == t:
        flag = True

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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