Problem:


Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.



Sample input:
5
5 2 3 4 1


Sample output:
YES

Submission status: Accepted

Solution code:
N = int(input())
p = [x for x in 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:
import sys
import numpy as np
input = sys.stdin.readline

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

new=sorted(a)
new=np.array(new)
a=np.array(a)
ans=a-new
if len([i for i in ans if i == 0])==len(a) or len([i for i in ans if i == 0])==(len(a)-2):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
p=[int(i) for i in input().split()]
r=[i for i in range(1,n+1)]
s=[p[i]-r[i] for i in range(n)]
print("YES" if s.count(0)==n-2 or s.count(0)==n else "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, x in enumerate(p):
    err += 0 if i + 1 == x else 1
print('YES' if err <= 2 else 'NO')

Submission status: Accepted

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

l3=[]
for i in range(n):
    l3.append(l[i]-l2[i])

z=l3.count(0)
if n-z==0 or n-z==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(0, n):
    if p[i] != i + 1:
        cnt += 1
if cnt > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

cnt = 0
for i in range(1,n+1):
	if i != array[i-1]:
		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()] 
C = 0
for i in range(N):
	if i+1 != P[i]:
		C += 1

if C >= 3:
	print("NO")
else:
	print("YES")

Submission status: Accepted

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

Ps = sorted(P)

cnt = 0
for i in range(len(P)):
    if P[i] != Ps[i]:
        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()))
f = False
for i in range(N):
    for j in range(N):
        a = []
        for k in range(N):
            a.append(p[k])
        x, y = p[i], p[j]
        a[i] = y
        a[j] = x
        cnt = 0
        for k in range(N-1):
            if a[k] < a[k+1]:
                cnt += 1
        if cnt == N-1:
            print('YES')
            exit()

print('NO')

Submission status: Accepted

Solution code:
from collections import deque
import copy

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

sortedP = sorted(P)

if sortedP == P:
    print("YES")
    exit(0)

diff_count = 0
for p, sp in zip(P, sortedP):
    if p != sp:
        diff_count += 1


print("YES") if diff_count == 2 else print("NO")

Submission status: Accepted

Solution code:
N = int(input()) # 数字を変数に格納
List = list(map(int,input().split())) # 複数の数字をリストに格納

error_list = []

for i in range(N):
    if List[i] != i+1:
        error_list.append(i)
        
if len(error_list) == 0:
    print('YES')
elif len(error_list) == 2:
    first = error_list[0]
    second = error_list[1]
    if List[first] == (second+1) and List[second] == (first+1):
        print('YES')
    else:
        print('NO')
else:
    print('NO')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#B
import sys
#import math


n = int(input())
tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

#print(hoge)

for i in range(0,n-1):
	if(hoge[i]<hoge[i+1]):
		if(i==n-2):
			print("YES")
			sys.exit()
	else:
		break

for i in range(0,n-1):
	for j in range(i+1,n):
		k=hoge[j]
		hoge[j]=hoge[i]
		hoge[i]=k
		for m in range(0,n-1):
			if(hoge[m]<hoge[m+1]):
				if(m==n-2):
					print("YES")
					sys.exit()
			else:
				break
		k=hoge[j]
		hoge[j]=hoge[i]
		hoge[i]=k

print("NO")

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy,copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    n=INT()
    p=LIST()

    p_=p.copy()
    p_.sort()

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

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

if  __name__=='__main__':
    main()

Submission status: Accepted

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

wrong = 0

for i, pi in enumerate(p):
    if (i+1) != pi:
        wrong += 1

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

Submission status: Accepted

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

sorted_list = sorted(arr)
count = 0
for i in range(N):
    if arr[i] == sorted_list[i]:
        pass
    else:
        count += 1

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    P = list(map(int, input().split()))
    for i in range(N - 1):
        if P[i] > P[i + 1]:
            break
    for j in range(i + 1, N - 1):
        if P[j] > P[j + 1]:
            t = P[i]
            P[i] = P[j + 1]
            P[j + 1] = t
            break
    PP = P[::]
    P.sort()
    if P == PP:
        print('YES')
    else:
        print('NO')


main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    p = string_to_int(inputs[0])

    diff_point = []
    for i, num in enumerate(range(1, len(p)+1)):
        if num != p[i]:
            diff_point.append(i)
    if len(diff_point) == 2 or len(diff_point) == 0:
        return "YES"
    return "NO"


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Accepted

Solution code:
a = int(input())
list_a=list(map(int, input().split()))  #リスト入力
count = 0
for i in range(len(list_a)):
#     print(list_a[i],len(list_a)-i)
    if list_a[i] != i+1:
        count += 1
    
if count == 2 or count == 0:
    print("YES")
else :
    print("NO")

Submission status: Accepted

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

ans = 'NO'

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


if count == 0 or count == 2:
    ans = 'YES'

print(ans)

Submission status: Accepted

Solution code:
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

import re
# import numpy as np
# from scipy.misc import comb

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

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e18)
MOD = int(1e9)+7 # 10^9 + 7

# W行H列, x[W][H]
def create_grid(W, H, value = 0):
    return [[ value for _ in range(H)] for _ in range(W)]

########

# A, B = l_inpl()

# if (A+B)%2 == 0:
#     print((A+B)//2)
# else:
#     print("IMPOSSIBLE")

N = i_inpl()
p = l_inpl()

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

cnt = 0
for pi, li in zip(p, l):
    if pi != li:
        cnt += 1

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a_b_sort = a[:]
a.sort()
num=0
flag = 1


if a_b_sort==a:
    print("YES")
    flag = 0


for i in range(len(a)):
    if a[i] != a_b_sort[i]:
        num += 1
        
    if num > 2:
        print("NO")
        flag = 0
        break
        
if flag==1:
    print("YES")

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = sorted(A)

    cnt = 0
    for a, b in zip(A, B):
        if a != b:
            cnt += 1

    if cnt <= 2:
        return 'YES'
    return 'NO'


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

Submission status: Accepted

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

count = 0
for i, x in enumerate(a):
    if i + 1 != x:
        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()))
p = np.array(p)
count = 0

p_sorted = np.sort(p)
for i in range(n):
    if p_sorted[i] != p[i]:
        count += 1

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
s = sorted(p)
d = sum([1 for (a, b) in zip(p, s) if (a - b) != 0])
if d <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
count=0
N=int(input())
p=list(map(int,input().split()))
for i in range(N):
    if p[i]==i+1:
        count+=1
if count==N or count==N-2:
    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.arange(n) + 1 

if sum(p == pp) >= n-2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def cycle_decomp(N, P):
    vis = [False]*N
    res = []
    for i in range(N):
        cur = []
        while not vis[i]:
            cur.append(i)
            vis[i] = True
            i = P[i]
        if len(cur) > 1:
            res.append(cur)
    return res


N = int(input())
P = [int(x)-1 for x in input().split()]
c = cycle_decomp(N, P)
if not c or len(c) == 1 and len(c[0]) == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import copy

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

asc_nums = sorted(nums)


def check():
    """総当たりで入れ替えてチェックする"""
    # 入れ替えずに昇順かチェックする
    if nums == asc_nums:
        return "YES"
    for i in range(N):
        for j in range(i + 1, N):
            nums_copy = copy.copy(nums)
            nums_copy[i] = nums[j]
            nums_copy[j] = nums[i]
            if nums_copy == asc_nums:
                return "YES"
    return "NO"


print(check())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
ls=list(map(int,input().split()))
ref_ls=[i+1 for i in range(n)]
count=0

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

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

Submission status: Accepted

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

for i in range(n):
    if li[i] != s_li[i]:
        c+=1

if c <= 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:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

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

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

Submission status: Accepted

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

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


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

Submission status: Accepted

Solution code:
n = int(input())
P = [int(x) for x in input().split()]
flag = True
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        else:
            copy_P = P[:]
            copy_P[i], copy_P[j] = copy_P[j], copy_P[i]
            if copy_P == sorted(P):
                print("YES")
                flag = False
                break
    else:
        continue
    break
    
if sorted(P) == P:
    print("YES")
    flag = False

if flag:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(raw_input())
p = map(int,raw_input().split())

err_cnt = 0

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

if err_cnt ==0 or err_cnt==2:
    print "YES"
else :
    print "NO"

Submission status: Accepted

Solution code:
from sys import stdin


def swqp(ps, li, ri):
    result = ps[:]
    tmp = result[li]
    result[li] = result[ri]
    result[ri] = tmp
    return result


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


def main():
    N = int(stdin.readline().rstrip())
    ps = [int(x) for x in stdin.readline().rstrip().split()]
    if check(ps):
        print('YES')
        return
    for i in range(N - 1):
        for j in range(i + 1, N):
            if check(swqp(ps, i, j)):
                print('YES')
                return
    print('NO')


if __name__ == "__main__":
    main()

Submission status: Accepted

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

sort_p = sorted(p)
number = 0

for n in range(len(p)):
  if p[n] != sort_p[n]:
    number += 1

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
for i in range(n):
    if (l[i] != i + 1):
        ind = l.index(i + 1)
        l[i], l[ind] = l[ind], l[i]
        break

ok = 1
for i in range(1, n):
    if (l[i] < l[i - 1]):
        ok = 0
if ok:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

ans = [p[i] for i in range(len(p)) if p[i] != i+1]

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

Submission status: Accepted

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

diff = []
for i in range(n):
	if(a[i] != b[i]):
		diff.append(a[i])

if(len(diff) == 2 or len(diff) == 0):
	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):
    num = i + 1
    if num != 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 not i+1 == P[i]:
        cnt += 1
if cnt < 3:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

P_origin = P[:]
P.sort()

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

if diff_count <= 2:
  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(len(p)):
	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()))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

q = sorted(p)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=input().split()
for i in range(len(p)):
    p[i]=int(p[i])
p1=sorted(p)
count=0
for i in range(n):
    if p[i]!=p1[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()))
ad = 0
ok = list(range(N+1))

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

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

Submission status: Accepted

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

cnt=0
for i,v in enumerate(p):
    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 = [int(i) for i in input().split(' ')]
P_up = sorted(p_list)
count = 0
for i in range(len(p_list)):
    if p_list[i]!=P_up[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()))

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

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

Submission status: Accepted

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

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

if ng_count == 0 or ng_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:
a=[]
c=[]
b=int(input())
for x in input().split(" "):
    a.append(int(x))
    
for x in a:
    c.append(x)

c.sort()

n=0
i=0

while i<b:
    if a[i]!=c[i]:
        n+=1
    i+=1

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

Submission status: Accepted

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

# x = range(1,n+1)

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

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

Submission status: Accepted

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

cnt = 0
for p, q in zip(P, Q):
    if p != q:
        cnt += 1
print("YES" if cnt == 2 or cnt == 0 else "NO")

Submission status: Accepted

Solution code:

n = int(input())

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

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



ans = ''

if len(log)==0:
    ans = 'YES'

elif len(log)==2:
    xv,xi = log[0]
    yv,yi = log[1]

    ans = 'YES' if xv == yi and yv == xi else 'NO'
else :
    ans = "NO"

print(ans)

Submission status: Accepted

Solution code:
import copy
import sys
sys.setrecursionlimit(4100000)

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

d = copy.deepcopy(p)
p.sort()
counter = 0
for i in range(n):
    if p[i] != d[i]:
        counter += 1
    if counter >= 3:
        break
if counter >= 3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

sortarr = sorted(arr)
count = 0

for i in range(n):
    if arr[i] != sortarr[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()))
p_right = sorted(p)
count  =0

for i,o in zip(p, p_right):
  if i != o:
    count += 1
if count <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import defaultdict, deque
import math
from math import factorial
import fractions
import re

BIGNUM = 10 ** 9 + 7

def main():
    N = int(input())
    Ps = list(map(int, input().split()))
    
    num = 0
    for i in range(N):
        pi = Ps[i]
        if pi != i + 1:
            num += 1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(N):
    #print(p[i])
    if p[i] != i + 1:
        cnt -= 1
        #print("del cnt")
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()))

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

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    p = list(map(int, input().split()))

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

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

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

Submission status: Accepted

Solution code:
from sys import stdin

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

    p = sorted(P)
    n = 0
    for p1, p2 in zip(P, p):
        if p1 != p2:
            n += 1
    if n < 3:
        print('YES')
    else:
        print('NO')

input = lambda: stdin.readline()
main()

Submission status: Accepted

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

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

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


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

Submission status: Accepted

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

for i in range(n):
    if a[i]-1 == i:
        continue
    a[a[i]-1], a[i] = a[i], a[a[i]-1]
    break

if a == sorted(a):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
sort_p=sorted(p)
if p==sort_p:
  print('YES')
  exit()
for i in range(n-1):
  for j in range(i+1,n):
    k=p[i]
    l=p[j]
    p[i]=l
    p[j]=k
    if p==sort_p:
      print('YES')
      exit()
    p[i]=k
    p[j]=l
print('NO')

Submission status: Accepted

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

INF = 10 ** 10

N = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]

def isSorted(num_list):
    for index in range(N-1):
        if num_list[index+1] < num_list[index]:
            return False

    return True

if isSorted(p):
    print("YES")
    exit()

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

        if isSorted(tmp_p):
            print("YES") 
            exit()

print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = []
p = list(map(int, input().split()))
_p = sorted(p)
count = 0

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

Submission status: Accepted

Solution code:
N = int(input())
P = map(int, input().split())
a = []
for i, p in enumerate(P):
    if p != i+1:
      a.append((p,i+1))
if len(a)==0 or len(a)==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):
    for j in range(i, n):
        p_c = p.copy()
        p_c[i], p_c[j] = p_c[j], p_c[i]
        if p_c == sorted(p_c):
            print('YES')
            exit()
print('NO')

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
p = np.array(input().split(), dtype=np.int64)

q1 = np.sort(p)

q2 = q1 - p

if np.sum(q2 != 0) == 0 or np.sum(q2 != 0) == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
r = sum(x != i for i, x in enumerate(map(int, input().split()), 1))
print('YNEOS'[0 != r != 2::2])

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int,input().split()))
li2 = [i for i in range(1,N+1)]
cnt = 0
li3 = []
for i in range(N):
    if li[i] == li2[i]:
        pass
    else:
        cnt += 1
        li3.append(i)
if cnt > 2:
    ans = "NO"
elif cnt == 1:
    ans ="NO"
elif cnt == 0:
    ans = "YES"
else:
    ind = li3[0]
    ind2 = li3[1]
    if li[ind] == ind2+1:
        ans ="YES"
    else:
        ans = "NO"
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
P = [int(i) for i in input().split()]
M = [int(i+1) for i in range(N)]
# print(N,P,M)
diff = 0
for i in range(N):
    if not P[i]==M[i]:
        diff+=1

# print(diff)
if diff >2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

YES = "YES"  # type: str
NO = "NO"  # type: str


def solve(N: int, p: "List[int]"):
    count = 0

    p_ = [i for i in range(1, N+1)]
    for i in range(N):
        if p[i] != p_[i]:
            count += 1
            
    if count > 2:
        print(NO)
    else:
        print(YES)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    p = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans=0
for i in range(0,len(p),1):
    if(p[i]!=p_sort[i]):
        ans = ans+1
if (ans >=3):
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
N = int(input())
p = [int(i) for i in input().split()]
ct = 0
for i in range(N):
  if p[i] == i+1:
    continue
  elif p[i] > i+1:
    if p[p[i]-1] == i+1:
      ct += 1
    else:
      print("NO")
      exit()
if ct <= 1:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

# print(ps)
# print(correct)

count = 0
for i in range(N):
    if ps[i] != correct[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 = 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()))
A = [i+1 for i in range(N)]
c = 0

for p, a in zip(P, A):
    if p != a:
        c += 1

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

Submission status: Accepted

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

sorted_p = sorted(P)

count = 0
for n in range(N):
    if sorted_p[n] != P[n]:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

    n = sum(1 for _ in filter(lambda x: x[0] != x[1], zip(P, sorted(P))))
    print('YES' if n <= 2 else 'NO')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
p = [int(x) for x in input().split()]
a = list(range(1,N+1))
counter = 0
for j in range(N):
    if p[j] != a[j]:
        counter += 1
    if counter > 2:
        print('NO')
        exit()
else:
    if counter in {0, 2}:
        print('YES')
    else:
        print('NO')