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

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    P = LIST()
    count = 0
    for i in range(N):
        if P[i] != i + 1:
            count += 1
    if count == 2 or count < 2:
        YES()
    else:
        NO()

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

p = [int(x) for x 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())
p = input().split()
for i in range(N):
  p[i] = int(p[i])
sorted_p = sorted(p)
count = 0
for i in range(N):
  if p[i] != sorted_p[i]:
    count += 1
  if count == 3:
    print("NO")
    break
if count != 3:
  print("YES")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
count = sum([index + 1 != num for index, num in enumerate(P)])
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

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

lst_sorted = sorted(lst)

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

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

Submission status: Accepted

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


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  p = list(map(int, input().split(' ')))
  c = 0
  if judge(p):
    print('YES')
  else:
    for i in range(n):
      for j in range(i+1, n):
        a = p.copy()
        a[i], a[j] = a[j], a[i]
        if judge(a):
          c += 1
          print('YES')
    if c == 0:
      print('NO')


def judge(l):
  return True if l == sorted(l) else False


if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

judge = 0

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

Submission status: Accepted

Solution code:
import numpy as np
n = input("")
n = int(n)
#exa = input("").split(" ")
exa = list(map(int, input("").split(" ")))

exa2 = np.arange(n)+1
if np.count_nonzero((exa != exa2)) in [0, 2]:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
P = [0] + [int(_) for _ in input().split()]
count = 0
for i, v in enumerate(P):
    if i != v:
        P[i], P[v] = P[v], P[i]
        break
if sorted(P) == P:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

diff = [n - i - 1 for i, n in enumerate(nums) if n - i - 1 != 0]
if len(diff) == 0:
  print('YES')
elif len(diff) == 2 and sum(diff) == 0:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

p_sorted = sorted(p)

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

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

Submission status: Accepted

Solution code:
n = int(input())
ps = [int(elem) for elem in input().split()]

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

Submission status: Accepted

Solution code:
import sys

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

sorted_l = sorted(l)

wrong = 0
for i in range(list_len):
    if l[i] != sorted_l[i]:
        wrong += 1
        if wrong > 2:
            print("NO")
            sys.exit(0)

print("YES")

Submission status: Accepted

Solution code:
mx = int(input())
tmp = input()
nl = tmp.split(" ")
error = 0
for i in range(1,mx+1):
  if int(nl[i-1]) != i:
    error+=1

if error <= 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):
	if a[i]!=i+1:
		cnt+=1
if cnt<=2:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

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

pp = np.arange(N)+1

if np.count_nonzero((p != pp)) in [0, 2]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

cnt = 0
for i in range(N):
    if P[i] != i + 1:
        if P[P[i] - 1] == i + 1:
            cnt += 1
        else:
            print("NO")
            sys.exit(0)

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

pp = sorted(p)
for i in range(n):
    for j in range(n):
        tmp = p.copy()
        tmpt = tmp[j]
        tmp[j] = tmp[i]
        tmp[i] = tmpt
        if tmp == pp:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

Solution code:
import sys

n = int(input())
i = list(map(int, input().split()))
count = 0
for j in range(n):
    if i[j] != j + 1:
        count = count + 1
        if count > 2:
            print("NO")
            sys.exit()

print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(n) for n in input().split()]
X = [n for n in range(1, N+1)]
ans = sum([0 if p==x else 1 for p,x in zip(p,X)])
print("YES" if ans <= 2 else "NO")

Submission status: Accepted

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

Submission status: Accepted

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

inds = []
for i, num in enumerate(a):
  if num != (i+1):
    inds.append(i)

if len(inds) > 2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

for i, num in enumerate(nsort):
  if nlst[i] != num:
    cnt += 1

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

Submission status: Accepted

Solution code:
length = int(input())
unsort_list = list(map(int,input().split()))
sort_list = [i+1 for i in range(length)]


values = [(i,j) for (i,j) in zip(unsort_list,sort_list) if i != j]

if len(values) < 3:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N=int(input())
p=list(int(i) for i in input().split())
p_sorted=sorted(p)
sum=0
for i in range(N):
  if p[i]==p_sorted[i]:
    sum+=1
if sum==N-2 or sum==N:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

p_sort = sorted(p)

count = 0

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

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

else:
    print('NO')

Submission status: Accepted

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

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

if list1 == list2:
    print("YES")
    exit()
for i in range(0,N):
    if list1[i] != i+1:
        if i+1 == list1[list1[i]-1]:
            list1[list1[i]-1] = list1[i]
            list1[i] = i+1
            

        if list1 == list2:
            print("YES")
            exit()
        else:
            print("NO")
            exit()

Submission status: Accepted

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

try:
    E = sum([P[i-1] != i for i in range(1, N+1)])
    if E == 0 or E == 2:
        print("YES")
    else:
        print("NO")
except:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

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

x = sorted(m)

count = 0

for i in range(n):
  if m[i] != x[i]:
    if count == 2:
      print("NO")
      exit()
    count += 1
print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))    
a = [0]*n
b = 0  

for i in range(n):
    a[i] = p[i]-sorted(p)[i]
    if a[i] != 0:
        b += 1

if sum(a) == 0 and (b == 2 or b == 0):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
ps=list(map(int,input().split()))
if sum(1 for p,q in zip(ps, sorted(ps)) if p != q) in (0, 2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
nN = int(input())

Tmp = []
Tmp = input().rstrip().split(' ')

nData = [0 for i in range(nN)]

for i in range(nN):
  nData[i] = int(Tmp[i])

nFlag = 0
nCnt = 0

for i in range(nN):
  nTmp = i + 1
  if nData[i] != nTmp:
    nCnt += 1
    nTmp2 = nData[i] - 1
    nTmp3 = nData[nTmp2]
    if nTmp3 != nTmp:
      nCnt += 1
    else:
      nData[nTmp2]=nTmp2+1
  

  
if nCnt < 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

N = int(input())
L = [int(v) for v in input().split()]

for i in range(N):
    for j in range(i, N):
        p = [v for v in L]
        p[i], p[j] = p[j], p[i]
        
        f = True
        for k in range(N - 1):
            if p[k] > p[k + 1]:
                f = False
                break
        if f:
            print("YES")
            exit()
            
print("NO")

Submission status: Accepted

Solution code:
import itertools
import copy
n = int(input())
ps = list(map(int, input().split()))

c = list(itertools.combinations(range(n), 2))

ps_sort = sorted(ps)
if ps == ps_sort:
  print('YES')
  exit()
else:
  for i in range(len(c)):
    nps  = copy.copy(ps)
    nps[c[i][0]], nps[c[i][1]] = nps[c[i][1]], nps[c[i][0]]
    if nps == ps_sort:
      print('YES')
      exit()
  else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
pn = input().split()
asc = range(1,n + 1)
i = 0
for sample, px in zip(asc, pn):
	if sample != int(px): i += 1
      
print("YES" if i <= 2 else "NO")

Submission status: Accepted

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

ans = 0

for (i, j) in enumerate(p):
    if not i + 1 == j:
        ans += 1

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

Submission status: Accepted

Solution code:
n= int(input())

a=list(map(int, input().split()))
correct=sorted(a)

p=0
for i in range(n-1):
  for j in range(n-i-1):
    a[i], a[n-j-1] = a[n-j-1], a[i]
    if a == correct:
      p += 1
      
    else:
      pass
    a[i], a[n-j-1] = a[n-j-1], a[i]
    
if p == 1 or a == correct:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

sorted_array = sorted(array)
count = 0

for num1, num2 in zip(array, sorted_array):
    if num1 != num2:
        count += 1

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

Submission status: Accepted

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

sort_A = sorted (A)
flag = 0
for i in range(n):
    if ( A[i] != sort_A[i] ) :
        flag = flag + 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l_sort = sorted(l)

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

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

Submission status: Accepted

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

ans = [i+1 for i in range(N)]
cnt = len([a for a, b in zip(p, ans) if a != b])

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_dummy = p[:]
p.sort()
count = 0
for i in range(len(p)):
  if p[i] != p_dummy[i]:
    count += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def ok(p):
    n = len(p)
    for i in range(n-1):
        if p[i] > p[i+1]:
            return False
    return True

def solve(n, p):
    if ok(p):
        return "YES"
    for i in range(n):
        for j in range(i+1, n):
            q = p.copy()
            q[i], q[j] = q[j], q[i]
            if ok(q):
                return "YES"
    return "NO"

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

s_p=sorted(p)

cnt=0

for i in range(n):
  if p[i]!=s_p[i]:
    cnt +=1
  else:
    pass

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

Submission status: Accepted

Solution code:
# 2019/07/30
# AtCoder Beginner Contest 135 - B

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

# Sort
wp = sorted(p)

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

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

# Output
print(ans)

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

Submission status: Accepted

Solution code:
input_arr = [input() for i in range(2)]
N = input_arr[0]
p_arr = input_arr[1].split(' ')
 
k = 0
for i in range(len(p_arr)):
    if int(p_arr[i]) == i + 1:
        pass
    else:
        k += 1
if k <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
# print(n)
# print(p)
import numpy as np
a = list(range(n+1))
a.pop(0)
# print(a)
d = np.array(p) - np.array(a)
# print(d)
z = np.count_nonzero(d)
if z==2 or z==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

cnt = 0
flag = True
for i in range(N):
    if P[i] != Ps[i]: cnt += 1
    if cnt > 2:
        flag = False
        break
if flag: print('YES')
else: print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

q = sorted(pList)

count = 2
for a,b in zip(q,pList):
    if a != b:
        count -= 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
*l,=range(1,n+1)
c=0
for i in range(n):
    if a[i]!=l[i]:
        c+=1
if c<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
a = list(map(int, input().split()))
a_sort = sorted(a)
ctr = 0

for i in range(N):
    if a[i] != a_sort[i]:
        ctr += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
p = list(map(int, input().split()))
res = 0
for i, j in zip(range(1, N+1), p):
    if i != j:
        res = res + 1
    
print("YES" if res == 0 or res == 2 else "NO")

Submission status: Accepted

Solution code:
import numpy as np
a=[]


N = int(input())
a =[int(i) for i in input().split()]
a = np.array(a).astype(int)
a = np.where( (a-np.sort(a)) != 0)
if len(a[0]) > 2:
	print("NO")
else:
	print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
p = list(map(int, input().split()))

p2 = p.copy()
p2.sort()

p = np.asarray(p)
p2 = np.asarray(p2)
p3 = [p!=p2]
c = np.count_nonzero(p3)
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()))
cnt = 0
for i in range(1,n + 1):
    if i != p[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(" ")))
     
ps = sorted(p)
     
c = len([1 for x,y in zip(p, ps) if x != y])
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()))
li = list(p)
li.sort()
count = 0
for i in range(N):
  if li[i]!=p[i]:
    count += 1
if count<=2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = input()
list = [int(x) for x in input().split()]
end = 0
for i, num in enumerate(list):
    if num != i + 1:
        end += 1
    if end > 2:
        break

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

Submission status: Accepted

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

diff = 0
for p, sp in zip(P, sort_P):
    if p != sp:
        diff += 1
        
if diff == 2 or diff == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

swapped = False
result = 'YES'
for i in range(N):
  if p[i] == i + 1:
    continue
  
  temp = p[i]
  if swapped or p[temp - 1] != i + 1:
    result = 'NO'
    break
  
  p[i] = p[temp - 1]
  p[temp - 1] = temp
  swapped = True
      
print(result)

Submission status: Accepted

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

for i, j in enumerate(p):
    if j != 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()))

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

for j in range(0, n):
    if p[j] != asce[j]:
        dif.append(j)

if len(dif) == 2:
    if p[dif[1]] == asce[dif[0]] and p[dif[0]] == asce[dif[1]]:
        print("YES")
    else:
        print("NO")
else:
    if len(dif) == 0:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()] 
p_ = sorted(p)
count = 0
for i in range(n):
  if p[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()))
s=sorted(p)
flag=0
from copy import deepcopy as dc
if p==s:
    flag=1
for i in range(n):
    for j in range(i+1,n):
        d=dc(p)
        d[i],d[j]=d[j],d[i]
        if d==s:
            flag=1
if flag==1:
    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 or c==2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

P = list(map(int, input().split()))
P_sort = sorted(P)
cnt = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
import copy

sys.setrecursionlimit(1000000)

#input = sys.stdin.readline
from collections import defaultdict

# ~~~~~~~~~~~~~~~~~~~~~_(＾～＾ ｣ ∠)_~~~~~~~~~~~~~~~~~~~~~

N = int(input())

p = list(map(int, input().split()))
q = copy.deepcopy(p)

p.sort()

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

print(("NO", "YES")[diff<=2])

Submission status: Accepted

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

f = False
for i in range(n-1):
    for j in range(i+1, n):
        q = p[:]
        b = q.pop(j)
        a = q.pop(i)
        q.insert(i, b)
        q.insert(j, a)
        r = sorted(q[:])
        if q == r:
            f = True
            break
    if f:
        break

if p == sorted(p):
    f =True

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
b=sorted(a)
count=0
for i,j in zip(a,b):
    if i!=j:count+=1
if count<=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
P = [int(i) for i in input().split()]
ok = [i for i in range(1,n+1)]
cnt = 0
for p,o in zip(P,ok):
    if p != o: cnt += 1
if cnt == 2 or cnt == 0: print('YES')
else: print('NO')

Submission status: Accepted

Solution code:
import sys
N = int(input())
As = list(map(int, input().split()))
cnt = 0
for i, a in enumerate(As):
    if i+1 != a:
        if As[a-1] == i+1:
            cnt +=1
        else:
            print("NO")
            sys.exit()

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

Submission status: Accepted

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

count = 0

for p,n in zip(ps,range(1,N+1)):
    if p != n:
        count += 1
if count == 2 or count == 0:
    print('YES')
else:
    print('NO')