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()))
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())
a = list(map(int, input().split()))
same = 0
for i,v in enumerate(a):
    if v==i+1:
        same +=1
if same>=n-2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import numpy as np


N = int(input())
P = np.array(input().split(), dtype=np.int32)
P_sort = np.sort(P)
diff_P = P != P_sort
diff_num = np.count_nonzero(diff_P)

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

Submission status: Accepted

Solution code:
import sys

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


sorted_p = sorted(p)

limit = 2

index_L,value_L = [],[]
for val in range(0,n):
    if p[val] != sorted_p[val]:
        index_L.append(val)
        value_L.append(p[val])
        limit -= 1

        if limit <= 0:
            p[index_L[0]] = value_L[1]
            p[index_L[1]] = value_L[0]

            if p == sorted_p:
                print("YES")
            else:
                print("NO")
            
            sys.exit(0)

print("YES")

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
pp=sorted(p,reverse=False)
ans=0
for i,j in enumerate(pp):
    if j!=p[i]:
        ans+=1
if ans==0 or ans==2:
    print("YES")
else :
    print("NO")

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
shadow=p[:]
shadow.sort()
cnt=0
if p==shadow:
  print("YES")
  exit()
for i in range(N):
  if p[i]!=shadow[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()))
ps = sorted(p)
x = 0
for i, j in zip(p, ps):
    if i != j:
        x += 1
if x > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
P = list(map(int, input().split()))
P2 = copy.deepcopy(P)
P2.sort()

cnt = 0
for p, p2 in zip(P, P2):
    if p != p2:
        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(' ')))
correct_p = [i + 1 for i in range(n)]
ctr = 0

for i in range(n):
    if p[i] != correct_p[i]:
        ctr += 1
        if ctr > 2:
            break

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

Submission status: Accepted

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

P_sorted = sorted(P)

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

    if count > 2:
        print("NO")
        exit()

print("YES")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
p = [int(i) for i in input().split(" ")]
c = [0 if p[i]==i+1 else 1 for i in range(n)]
if np.sum(c) >= 3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

n_error = 0

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

if n_error==0 or n_error==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 == 3:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

Solution code:
N=int(input())

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

Submission status: Accepted

Solution code:
def main(): 

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

    cnt = 0

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

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


if __name__ == "__main__":
    main()

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]"):
    
    q = sorted(p)
    x = [ p[i] == q[i] for i in range(N)]
    if(x.count(False) in (0,2)):
        print(YES)
    else:
        print(NO)
    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:
from sys import stdin

n = int(stdin.readline().rstrip())
p_lst = [int(x) for x in stdin.readline().rstrip().split()]

if sorted(p_lst) == p_lst:
  print("YES")
else:
  for i in range(n):
    for j in range(n):
      if i < j:
        p_lst_tmp = p_lst.copy()
        tmp = p_lst_tmp[i]
        p_lst_tmp[i] = p_lst_tmp[j]
        p_lst_tmp[j] = tmp
        if sorted(p_lst_tmp) == p_lst_tmp:
          print("YES")
          exit()
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

N_array = np.arange(1, N+1, dtype=np.int)

count = 0

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

if count >= 3:
	print('NO')
else:
	print('YES')

Submission status: Accepted

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

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

Submission status: Accepted

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

diff = 0
for i,j in zip(range(1, n+1), p):
    if i != j:
        diff += 1
if diff <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

args = [int(e) for e in input().split()]

count = 0

for i in range(0, n) :
    if args[i] != i + 1 :
        count += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split()))
c = 0

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

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

Submission status: Accepted

Solution code:
import math

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

p_dash = p.copy()
p_dash.sort()

count = 0

for i in range(n):
	if p[i] - p_dash[i] != 0:
		count += 1
	if count == 3:
		flag = False
		break

if count <= 2:
	flag = True

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

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

has_changed = False
for i in range(N):
    expect = i + 1
    #print(p)
    #print(p[i], expect)
    if p[i] != expect:
        if has_changed:
            print('NO')
            exit()
        index = p[i] - 1
        if p[index] == expect:
            has_changed = True
            tmp = copy.deepcopy(p[i])
            p[i] = expect
            p[index] = tmp
            # print('change', p[i], p[index])
        else:
            print('NO')
            exit()

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]"):
    s = 0
    for i, j in zip(p, sorted(p)):
        if i != j:
            s += 1
    if s == 0 or s == 2:
        print('YES')
    else:
        print('NO')
    return


# Generated by 1.1.5 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:
main_num = raw_input()
num_list = map(int, raw_input().split())

fake_counter = 0
counter = 0

while True:
  if num_list[counter] != counter + 1:
    fake_counter += 1
  counter += 1
  if counter >= int(main_num):
    break

if fake_counter > 2:
  print "NO"
else:
  print "YES"

Submission status: Accepted

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

Submission status: Accepted

Solution code:

    
import sys

if sys.platform =='ios':
	sys.stdin=open('input_file.txt')
	
n=int(input())
l=list(map(int,input().split()))
lsort=sorted(l)
cnt=0

for i in range(n):
	if l[i]==lsort[i]:
		cnt+=1
		
if cnt==n or cnt==n-2:
	print("YES")
	
else:
	print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
sort_p = sorted(p)
count = 0
for origin, sort in zip(p, sort_p):
    if origin != sort:
        count +=1
if count == 0 or count == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

# p[i] != iの個数をカウントして 0 or 2ならpossible
count = 0
for i in range(n):
    if p[i] != i+1:
        count += 1
if count > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
import copy

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

for i in range(N):
    if count > 2:
        print("NO")
        break
    if P[i] != i+1:
        count += 1
if count <=2:
    print("YES")

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int,input().split()))
tmp = 0

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

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

Submission status: Accepted

Solution code:
while True:
    try:
        n = int(input())
        N = list(map(int, input().split()))
        swap = 0
        for i in range(len(N)):
            if N[i] != i+1:
                N[N.index(i+1)] = N[i]
                N[i] = i+1
                swap += 1
                if swap > 1:
                    print("NO")
                    break
        else:
            print("YES")
            
    except:
        break

Submission status: Accepted

Solution code:
num = int(input())

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

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

Submission status: Accepted

Solution code:

N = int(input())

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

collect = sorted(p)

if p == collect:
    print('YES')
    exit()

for i in range(N):
    for j in range(N):
        p[i],p[j] = p[j],p[i]
        if p == collect:
            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()))
ps = sorted(p)

dif = 0
for i in range(N):
    if p[i] != ps[i]:
        dif += 1
if dif == 0 or dif == 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 or count==0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy
import numpy as np
N=input()
N=int(N)
a=list(map(int,input().split()))
b=copy.copy(a)
a.sort(reverse=False)
c=np.array(a)-np.array(b)
NN=np.count_nonzero(c==0)
if N-NN<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#むりくぼ
n = int(input())
p = list(map(int,input().split()))
count = 0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for i in range(N):
    if A[i] != i+1:
        count = count +1
    
if count > 2:
    print("NO")

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

Submission status: Accepted

Solution code:
#Coding:Utf-8

N=int(input())
p=list(map(int,input().split()))
Asc=sorted(p)
check=0
for i in range(N):
    if Asc[i]==p[i]:
        pass
    else:
        check+=1
    if check>2:
        print('NO')
        exit()
print('YES')

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    num = LI()
    for i in range(n):
        for j in range(n):
            tmp = num[:]
            tmp[i], tmp[j] = tmp[j], tmp[i]
            if tmp == sorted(num):
                return 'YES'
    return 'NO'


print(main())

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate
import sys


def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


def main():
    n = I()
    num = LI()
    for i in range(n):
        for j in range(n):
            tmp = num.copy()
            tmp[i], tmp[j] = tmp[j], tmp[i]
            if tmp == sorted(num):
                return 'YES'
    return 'NO'


print(main())

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
flag = False
for i in range(n):
    for j in range(n):
        q = p.copy()
        q[i], q[j] = q[j], q[i]
        if q == sorted(q):
            flag = True
            break
if flag:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

n = int(input())
l = list(map(int, input().split()))
c = 0
for i,n in enumerate(l,1):
    if n != i:
        c += 1
ans = 'YES' if c == 0 or c == 2 else 'NO'
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N =  int(input())
    p = list(map(int, input().split()))
    
    k_num = [x for i, x in enumerate(p, 1) if x != i]
    if len(k_num) <= 2:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N = int(input())
    p = [int(n) for n in input().split()]
    sp = sorted(p)
    cnt = 0
    for i in range(N):
        if p[i] != sp[i]:
            cnt += 1

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





if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

for i in range(n):
  moto.append(i+1)

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

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
p = list(map(int, input().split()))
sort_p = sorted(p)
if p == sort_p:
    print('YES')
else:
    p = np.array(p)
    sort_p = np.array(sort_p)
    index = np.where(p != sort_p)
    if len(p[index]) == 2:
        p[index[0][0]],p[index[0][1]] = p[index[0][1]],p[index[0][0]]
        p = list(p)
        sort_p = list(sort_p)
        if p == sort_p:
            print('YES')
        else:
            print('NO')
    else:
        print('NO')

Submission status: Accepted

Solution code:
import copy
N = int(input())
p = list(map(int,input().split()))
a = sorted(p)
if a==p:
  print("YES")
  exit()
for i in range(N):
  for j in range(i,N):
    b = copy.deepcopy(p)
    l = b[i]
    b[i] = b[j]
    b[j] = l
    if a==b:
      print("YES")
      exit()
print("NO")

Submission status: Accepted

Solution code:
N = int(input())
P = [int(_) for _ in input().split()]
P2 = sorted(P)
cnt = sum([1 for p in zip(P, P2) if p[0] != p[1]])
print('YES' if cnt <= 2 else 'NO')

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
p = np.array(list(map(int, input().split())))

t = np.array([i for i in range(1, N+1)])

c = np.count_nonzero(p != t)

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

Submission status: Accepted

Solution code:
n = int(input())

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

if len([x for x, y in zip(li_p, sorted(li_p)) if x != y]) <= 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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(int(i) for i in input().split())
q = list(range(1, n+1))
diff = [1 if i==j else 0 for i, j in zip(p,q)]
if diff.count(0) in [0, 2]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

res = 0
a_sort = sorted(a)
 
for i in range(n):
    if a_sort[i] == a[i]:
        res += 1
 
if res >= n - 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
a = int(input())
b = input().split()
for i in range(a):
  b[i] = int(b[i])
  
for i in range(a-1):
  if (b[i] > b[i+1]):
    break
if i == a-2:
  print('YES')
else:
  for j in range(a-1,i,-1):
    if (b[j] < b[j-1]):
      break
  temp = b[i]
  b[i] = b[j]
  b[j] = temp
  for k in range(a-1):
    if (b[k] > b[k+1]):
      break
  if k == a-2:
    print('YES')
  else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = [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')

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    if p[i] != p_sorted[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()]

def judge(p, n):
	count = sum([1 for i in range(n) if p[i] != i + 1])
	return count <= 2

print('YES' if judge(p, n) else 'NO')

Submission status: Accepted

Solution code:
n = int(input())
p = [int(_) for _ in input().split()]
diff = sum(1 for i, j in zip(range(1, n + 1), p) if i != j)

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

Submission status: Accepted

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

import numpy as	np

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

n_sorted = np.arange(1, n+1)
correct = n_sorted == p
if correct.sum() >= n-2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    p = list(map(int, input().rstrip('\n').split()))
    q = sorted(p)
    cnt = 0
    for i in range(n):
        if q[i] != p[i]:
            cnt += 1
    print("YES" if cnt <= 2 else "NO")


if __name__ == '__main__':
    slove()

Submission status: Accepted

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


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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
cnt = 0
for x,y in zip(a, b):
    if x != y:
        cnt += 1
if cnt == 2 or a == b:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

psort = sorted(p)
a = 0

for i in range(N):
    dist = psort[i] - p[i]
    if dist != 0:
        a += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 0
for i in range(n):
    if l[i] != i + 1:
        ans += 1
if ans <= 2:
    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 in range(N):
  if A[i] == B[i]:
    count += 1
    
if count == N or count == N -2:
  print('YES')
  exit()
print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

L=[]
for i in range(n):
    if i+1 !=p[i]:
        L.append([i, p[i]])
    if len(L)==2:
        break


if len(L)<=1:
    print('YES')
else:
    p[L[0][0]] = L[1][1]
    p[L[1][0]] = L[0][1]
    if p == sorted(p):
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

c = 0
for i in range(n):
    if l[i] != ll[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()))


pp = sorted(p)
c=0
for i in range(n):
    if str(pp[i]) == str(p[i]):
        c=c
    else:
        c=c+1

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(n)):
  if n[i] == p[i]:
    continue
  else:
    count+=1
if count == 0 or count == 2:
  ans = 'YES'
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
res = 'YES'
count = 0
for i in range( n):
	if l[i] >  i + 1:
		p = l[i] - 1
		if l[p] == i + 1:
			l[i] = i
			l[p] = p + 1
			count += 1
			if count > 1:
				res = 'NO'
		else:
			res = 'NO'

print(res)