Problem:


Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 50

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

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

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

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

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

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

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

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

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



Sample input:
5
5 2 3 4 1


Sample output:
YES

Submission status: Accepted

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

swap = 0
result = True
for i in range(1, N + 1):
    if p_list[i - 1] != i:
        swap += 1
    if swap > 2:
        result = False
        break

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict

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

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







if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, math

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

    sP = sorted(P)

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

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

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
size = int(input())

numbers = [int(s) for s in input().split()]

unexpected_times = 0

for i in range(size):
    if i + 1 != numbers[i]:
        unexpected_times += 1


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

Submission status: Accepted

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

num = 0
for p, s in zip(P, S):
    if p != s:
        num += 1

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

Submission status: Accepted

Solution code:
n=int(input())
p=input().rstrip().split(' ')
C=list(p)
B=0;
C.sort(key=int)
for i in range(0,len(p)):
    if int(p[i])!=int(C[i]):
        B+=1;
if B<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

pl_sorted = sorted(pl)
n_diff = 0
for i in range(N):
    if pl[i] != pl_sorted[i]:
        n_diff += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def INT_(n): return int(n)-1
def MI(): return map(int, input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_, input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n: int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split())) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')


def main():
    N = I()
    ps = LI()
    cnt = 0
    for i, p in enumerate(ps):
        if i+1 != p:
            cnt += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

p2 = sorted(p)
cnt = 0

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

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

Submission status: Accepted

Solution code:
import itertools
import sys

n = int(input())
p_list = list(map(int, input().split()))
is_sorted = all(p_list[k] < p_list[k+1] for k in range(n - 1))
if is_sorted:
    print('YES')
    sys.exit()
for i, j in itertools.combinations(range(n), 2):
    p_list[i], p_list[j] = p_list[j], p_list[i]
    is_sorted = all(p_list[k] < p_list[k+1] for k in range(n - 1))
    if is_sorted:
        print('YES')
        sys.exit()
    p_list[i], p_list[j] = p_list[j], p_list[i]
print('NO')

Submission status: Accepted

Solution code:

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

counta = 0
for i in range(N):
    if A[i] == i + 1:
        continue
    else:
        counta += 1

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

Submission status: Accepted

Solution code:
import sys


def main():
    n_list = [int(s) for s in sys.stdin.readline().strip().split()]
    n = n_list[0]
    p = [int(s) for s in sys.stdin.readline().strip().split()]
    sorted_p = sorted(p)
    diff = 0
    for i in range(n):
        if p[i] != sorted_p[i]:
            diff += 1
    if diff == 0 or diff == 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = input()
sample = list(map(int, input().split()))
sorted_sample = sorted(sample)
count = 0

for x, y in zip(sample, sorted_sample):
  if x != y:
    count += 1
  if count >2:
    print("NO")
    break

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

Submission status: Accepted

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

q = sorted(p)

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy
n = int(input())
p = input().split()
for i in range(len(p)):
    p[i] = int(p[i])
l = copy.deepcopy(p)
l.sort()
k = 0
for i in range(len(l)):
    if l[i]!=p[i]:
        k+=1
if k==2or k==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

    for i in range(n):
        for j in range(i + 1, n):
            b = [k for k in a]
            b[i], b[j] = b[j], b[i]
            if b == [k for k in range(1, n + 1)]:
                print("YES")
                return

    print("YES" if a == [k for k in range(1, n + 1)] else "NO")

main()

Submission status: Accepted

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

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

if sum([p!=q for p, q in zip(P, Q)]) <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

p_list_true = sorted(p_list)

flag = True
diff_indexs = []
for i in range(N):
    if p_list[i] != p_list_true[i]:
        diff_indexs.append(i)
if len(diff_indexs) == 0:
    print("YES")
elif len(diff_indexs) == 2:
    i, j = diff_indexs
    if p_list[i] == p_list_true[j] and p_list[j] == p_list_true[i]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

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

ans = [x + 1 for x in range(n)]
ans = sorted(ans)

count = 0

for i in range(n):
    if ans[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()))

counter = 0

buff = P.copy()

buff.sort()

for i in range(N):
    if P[i] != buff[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()))
s=[i+1 for i in range(n)]
count=0

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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

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

    c = 0
    for p1,p2 in zip(pp, sorted(pp)):
        if p1 != p2:
            c += 1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

p2 = sorted(p)

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

count = 0 
a = []

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

if len(a)==0:
    print("YES")
elif len(a)==2:
    if p[a[0]]==a[1]+1 and p[a[1]]==a[0]+1:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# いっこ入力
n =int(input())

# 空白区切りの数列をリストに代入
k = [int(i) for i in input().split()]

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

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

Submission status: Accepted

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

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

cnt = 0
for i in range(N):
    if Pn[i] != Qn[i]:
        cnt += 1
    else:
        continue

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

Submission status: Accepted

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

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

diff = 0
for i in range(n):
    if p_list[i] != default_list[i]:
        diff += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Q = sorted(P)

count = N
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())
ps = [int(pi) for pi in input().split(' ')]
trues = list(range(1, N+1))

bools = [pi != ti for pi, ti in zip(ps, trues)]
S = sum(bools)
if S == 0 or S == 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

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

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

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

Submission status: Accepted

Solution code:
def main():
    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")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
P = [int(i) for i in input().split()]
T = [ i for i in range(1,N+1)]

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

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

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
import re
from collections import deque
import sys
import math
input = sys.stdin.readline


def int_raw():
    return int(input())


def ss_raw():
    return input().split()


def ints_raw():
    return tuple(map(int, ss_raw()))


DIV = 10**9+7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def main():
    N = int_raw()
    As = ints_raw()
    Ass = sorted(As)
    nc = sum([As[i]!=Ass[i] for i in range(N)])
    if nc <=2:
        return "YES"
    else:
        return "NO"

print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = [i for i in 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 == 0) or (count ==2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

ap_list = sorted(p_list, reverse = False)

counter = 0

for i in range(N):
    if p_list[i] != ap_list[i]:
        counter += 1

if counter <= 2:
    print("YES")

else:
    print("NO")

Submission status: Accepted

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

sorted_p = sorted(p)

count = 0
for i in range(n):
    if p[i] != sorted_p[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()]
P_sort = sorted(P)
import numpy as np
P_np = np.array(P)
P_sort_np = np.array(P_sort)
if len(P) == (P_np == P_sort_np).sum() or len(P)-2 == (P_np == P_sort_np).sum():
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

if P == p_sorted:
    print('YES')
    exit()
for i in range(N):
    for j in range(i + 1, N):
        P[i], P[j] = P[j], P[i]
        if P == p_sorted:
            print('YES')
            exit()
        P[i], P[j] = P[j], P[i]
print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

right = list(range(1,n+1))
count = 0
for i in range(n):
  if p[i]!=right[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()))
x = 0
for i in range(n):
    if p[i] != i + 1:
        x += 1
if x == 0 or x == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
N = int(input())
P = list(map(int, input().split()))
gold = [i for i in range(1, N+1)]
err_cnt = 0
for i, j in zip(P, gold):
    if i != j:
        err_cnt += 1
if err_cnt <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i, item in enumerate(arr):
  if item != i+1:
    count += 1
if count == 2 or count == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
data = list(map(int, input().split()))
x = list(range(1, n + 1))
cnt = 0
for i in range(n):
    if data[i] != x[i]:
        cnt += 1

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

Submission status: Accepted

Solution code:
n = int(input())
a_s = [int(item) for item in input().split()]

a_sorted = sorted(a_s)

cnt = 0
for std, a in zip(a_sorted, a_s):
    if std != a:
        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()))
Q = sorted(P)
s = 0
for i in range(n):
    if P[i] != Q[i]:
        s += 1
if s <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))

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

B = sorted(A)

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

def main(): 
    N = II()
    P= LI()

    P_sort = sorted(P)

    diff = 0
    for i,j in zip(P, P_sort):
        if i != j:
            diff += 1
    if diff ==0 or diff ==2:
        print('YES')
    else:
        print('NO')

main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys, math
from collections import deque

readline = sys.stdin.readline

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

a = len([i for i, p in enumerate(P) if i+1 != p])
if a <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys

n= int(sys.stdin.readline())
p= [ int(x) for x in sys.stdin.readline().split() ]
q= sorted(p)

c= 0
for i in range(n):
    if p[i]!=q[i]: c+=1
    if c>2: break
#
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:
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)

def main():
	n = int(input())
	a = list(map(int,input().split()))
	b = deepcopy(a)
	b.sort()
	c = 0
	for i in range(n):
		if(b[i] != a[i]):
			c+=1
	if(c==2 or c==0):
		print('YES')
	else:
		print('NO')

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

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

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

Solution code:

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
flag=False
for i in range(1,n):
  if arr[i]<arr[i-1]:
    break
else:
  flag=True
for i in range(n):
  for j in range(i+1,n):
    tmp=arr[:]
    tmp[i],tmp[j]=tmp[j],tmp[i]
    for k in range(1,n):
      if tmp[k]<tmp[k-1]:
        break
    else:
      flag=True
if flag==True:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

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

cnt=0
for l,p in zip(n,P):
    if l != p:
        cnt+=1
if cnt <=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
cnt = 0
for i,j in zip(data,range(1,N+1)):
    if i != j:
        cnt += 1
if cnt == 2 or cnt == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

count = 0
for i, p in enumerate(ps):
    if i + 1 != p:
        count += 1

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

Submission status: Accepted

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

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

if upright(l):
    print("YES")
    exit(0)

for i in range(n):
    for j in range(i+1,n):
        tmp = l[i]
        l[i] = l[j]
        l[j] = tmp
        if upright(l):
            print("YES")
            exit(0)
        tmp = l[i]
        l[i] = l[j]
        l[j] = tmp
print("NO")

Submission status: Accepted

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

P_ = P.copy()
P.sort()
r = 0
for p, p_ in zip(P, P_):
  if p != p_:
    r += 1
  if r > 2:
    break
else:
  print("YES")
  exit()
print("NO")

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    *p, = map(int, input().split())

    res = [q for i, q in enumerate(p) if i + 1 != q]
    if len(res) in [0, 2]:
        print('YES')
    else:
        print('NO')


main()

Submission status: Accepted

Solution code:
N = int(input())

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


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

if(tmp_ans==N or tmp_ans==N-2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

z=0
for a,b in zip(p,ed):
    if a!=b:
        z+=1
print("YES" if z <= 2 else "NO")

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
Max = sys.maxsize
def l(): #intのlist
	return list(map(int,input().split()))
def m(): #複数文字
	return map(int,input().split())
def onem(): #Nとかの取得
	return int(input())
def s(x): #圧縮
	a = []
	aa = x[0]
	su = 1
	for i in range(len(x)-1):
		if aa == x[i+1]:
			a.append([aa,su])
			aa = x[i+1]
			su = 1
		else:
			su += 1
	a.append([aa,su])
	return a
def jo(x): #listをスペースごとに分ける
	return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
	return max(map(max,x))

n = onem()

p = l()

pp = p[:]
pp.sort()
if p == pp:
	print("YES")
	exit()

for i in range(n):
	for j in range(i+1,n):
		ppp = p[:]
		ppp[i],ppp[j] = ppp[j],ppp[i]
		if all(pp[k] == ppp[k] for k in range(n)):
			print("YES")
			exit()
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 not 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()))
q = sorted(p)
z = 0

for i,j in zip(p,q):
  if i!=j:
    z+=1

if z<=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 p[i] != i + 1 :
        c += 1
        
if c <= 2 :
    print('YES')
else :
    print('NO')