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

new_list = sorted(P)
cnt=0
for i,e in zip(P,new_list):
  if i != e:
    cnt+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
g = input()
a = list(map(str, g.split()))
b = list(map(int, g.split()))
if sorted(b) == b:
    print("YES")
else:
    m = 0
    for i in range(len(b)):
        if b[i] == sorted(b)[i]:
            m+=1
    if m==len(b)-2:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
import copy
import sys
N = int(input())
H = list(map(int,input().split()))
C = 0

if sorted(H) == H:
	print("YES")
	sys.exit()

for i in range(0,N-1):
	for j in range(1,N):
		L = copy.copy(H)
		L[i],L[j] = L[j],L[i] 
		if L == sorted(H):
			print("YES")
			sys.exit()
print("NO")

Submission status: Accepted

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

LPS = []
LPS = sorted(LP)

cnt = 0
for i in range(N):
    if LP[i] != LPS[i]:
        cnt += 1
        if cnt == 3:
            print("NO")
            exit()

print("YES")

Submission status: Accepted

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

ps = sorted(p)

cnt = 0
for i in range(len(p)):
  if p[i] != ps[i] :
    cnt += 1
    if cnt > 2 :
      print("NO")
      exit()

print("YES")

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    p = list(map(int, input().split()))
    l = range(1,n+1)
    count = 0
    for i, j in zip(p, l):
        if i != j:
            count += 1
    print('YNEOS'[count>2::2])

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = 0
N = int(input())
H = list(map(int,input().split()))
K = list(range(1,N+1))

for i in range(N):
    if H[i]!=K[i]:
        s+=1
if s==0 or s==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-1):
    if p[i] != i+1:
        count += 1
        if count > 2:
            print("NO")
            exit()
print("YES")

Submission status: Accepted

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

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

for j in range(N):
    for k in range(N):
        p[j],p[k]=p[k],p[j]
        if p==li:
            print("YES")
            exit()
        else:
            p[j],p[k]=p[k],p[j]

print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li1 = list(map(int,input().split()))
li2 = sorted(li1)

cnt = 0

for i in range(n):
    if li1[i] != li2[i]:
        cnt += 1

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

Submission status: Accepted

Solution code:
import numpy as np

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

sorted_p = np.sort(np.copy(p_array))
diff = (p_array != sorted_p).sum()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
isMatch = list(map(lambda x, y: x == y, p, sorted(p)))
result = 'YES' if isMatch.count(False) <= 2 else 'NO'
print(result)

Submission status: Accepted

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

count = 0
for i, j in zip(p, q):
    if i != j:
        count += 1
        if count > 2:
            print("NO")
            sys.exit()
print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

p_sorted = sorted(p)
swap_pre = []

for i,j in zip(p, p_sorted):
    if i != j and not swap_pre:
        swap_pre += [i,j]
    elif i != j and swap_pre == [j, i]:
        pass
    elif i != j:
        print('NO')
        break
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A=[int(i)-1 for i in input().split()]
res = 0
for i in range(n):
    if A[i]==i:
        continue
    else:
        res+=1
if(res < 3):print("YES")
else:print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

def swap(list, pos1, pos2) :
  list[pos1], list[pos2] = list[pos2], list[pos1]
  return list

ok = 0
for i in range(n):
  for j in range(i, n):
    b = a.copy()
    swap(b, i, j)
    flag = 1
    for k in range(1, n):
      if b[k - 1] > b[k]:
        flag = 0

    if flag == 1:
      ok = 1

if ok == 1:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

for i in range(N):
    if P[i] != A[i]:
        s += 1
        
print('NO' if s > 2 else 'YES')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for i in range(n):
    for j in range(i,n):
        q = []
        for x in p:
            q.append(x)
        q[i],q[j] = q[j],q[i]
        sw = 1
        for k in range(n-1):
            if q[k] > q[k+1]:
                sw = 0
                break
        if sw == 1:
            ans = 1
        q = []
print(["NO","YES"][ans])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
ps=sorted(p)
b=[]
for i in range(n):
  b.append(p[i]-ps[i])
if b.count(0)<n-2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():

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

    count = 0
    l = []
    for i in range(N):
        if p[i] == i + 1:
            continue
        else:
            count += 1
            l.append((i + 1, p[i]))

    if count == 0:
        ans = 'YES'
    elif count == 2:
        if l[0][0] == l[1][1] and l[1][0] == l[0][1]:
            ans = 'YES'
        else:
            ans = 'NO'
    else:
        ans = 'NO'

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(int,input().split()))
Z=[]
tmp=[]
for i in range(N):
    Z.append(S[i])
    tmp.append(S[i])
    

Z.sort()


for i in range(N):
    for j in range(N):
        
        R=tmp[i]
        tmp[i]=tmp[j]
        tmp[j]=R
        if tmp==Z:
            print("YES")
            exit()
        R=tmp[i]
        tmp[i]=tmp[j]
        tmp[j]=R        
        

print("NO")

Submission status: Accepted

Solution code:
def actual(n, P):
    # 正しくない配置にある数字が2つ以下なら、1回以下のSwapで照準になる
    n_unmatched = [i == p for i, p in enumerate(P, start=1)].count(False)

    if n_unmatched <= 2:
        return 'YES'

    return 'NO'


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

print(actual(n, P))

Submission status: Accepted

Solution code:
import copy


def actual(n, P):
    nums_asc = sorted(P)

    if nums_asc == P:
        return 'YES'

    N = len(P)
    for i in range(N):
        for j in range(1, N):
            swapped_nums = copy.deepcopy(P)

            swapped_nums[i] = P[j]
            swapped_nums[j] = P[i]

            if swapped_nums == nums_asc:
                return 'YES'

    return 'NO'

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

print(actual(n, P))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = [i for i in range(1, n + 1)]
k = 0
for p, q in zip(a, b):
  if p != q:
    k += 1
if k >= 3:
  print('NO')
else:
  print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def linp():return list(map(int,input().split()))
n=int(input())
p=linp()
s=0
for i in range(n):
    if i==p[i]-1:continue
    else:
        p[p[i]-1]=p[i]
        s+=1
if s<=1:print("YES")
else:print("NO")

Submission status: Accepted

Solution code:
import numpy as np

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

a = np.arange(-1, -n-1, -1)
print('YES' if np.count_nonzero(p+a) < 3 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
for i ,j in zip(l,lis):
   
    if i!=j:
        ans+=1
if ans<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
ans=0
kai=0
ok=True
for i in range(n-1):
    if a[i]!=i+1:
        kai+=1
        if a[a[i]-1]!=i+1 or kai==3:
            ok=False
if ok:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
p = [int(x) for x in input().split()]
q = sorted(p)
d = [(pi, qi) for pi, qi in zip(p, q) if pi != qi]
print("YES" if len(d) == 0 or len(d) == 2 and d[0][0] == d[1][1] and d[1][0] == d[0][1] else "NO")

Submission status: Accepted

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

# print(n)
# print(p)

p_s = sorted(p)

# print(p_s)

count = 0
for i in range(len(p)):
    if p[i] != p_s[i]:
        count += 1
        
# print(count)

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

Submission status: Accepted

Solution code:
import copy

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

tlis = copy.deepcopy(lis)

sortlis = sorted(lis)


for i in range(n-1):
    for j in range(i,n):
        tmp = tlis[i]
        tlis[i] = tlis[j]
        tlis[j] = tmp
        if tlis == sortlis:
            print('YES')
            exit()
        else:
            tlis = copy.deepcopy(lis)
            
print('NO')

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    p = list(map(int, input().split()))
    count = 0
    for p, q in zip(p, sorted(p)):
        if p != q:
            count += 1

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


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()

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
            if count == 3:
                print("NO")
                return
    if count == 2 or count == 0:
        print("YES")
        return
    print("NO")
        
        
    
main()

Submission status: Accepted

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

p_sorted = sorted(p)

is_changed = [0 if x==y else 1 for x,y in zip(p, p_sorted)]

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
check = 0
for i in range(n):
    if i + 1 == p[i]:
        check += 1
    else:
        pass

print('YES' if check == n or check == n-2 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    A = list(map(int, input().split()))
    A_ = sorted(A)
    cnt = 0
    for i in range(len(A)):
        a = A[i]
        a_ = A_[i]
        if a != a_:
            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()) )

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_b

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

count = 0

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

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

Submission status: Accepted

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

def main():
    n = int(input())
    p_list = list(map(int, input().split()))
    count = 0
    for i in range(n):
        if i+1 != p_list[i]:
            count += 1
    
    if count >= 3:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
p_l = list(map(int,input().split()))
p_sort = sorted(p_l)

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

Submission status: Accepted

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

check = []

for x in range(N):
    check.append(x + 1)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    p = LI()
    cnt = 0
    for i in range(N):
        if p[i] != i + 1:
            cnt += 1
    if cnt > 2:
        print("NO")
    else:
        print("YES")


main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
p = list(map(int, input().split()))
q = [0]*n
for i in range(n):
    q[i] = p[i] - i - 1
if q.count(0) >= n - 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

a = sorted(p)
ans = 0

for i in range(n):
    if p[i] != a[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()))
temp=sorted(p)
ans=0
for i in range(n):
  if p[i]!=temp[i]:
    ans=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()))
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 = list(map(int,(input().split())))

hikaku = list(range(1,n+1))
count = 0
for i in range(n):
    if hikaku[i] != p[i]:
        count += 1
#print(count)
if count >=3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
N = int(input())
p = [0] + list(map(int,input().split()))
count = 0 #あるべきでないところにある数の個数
for i in range(1,N+1,1):
    if 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()))

p2 = sorted(p)

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

if cnt == 3:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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


def main():
    N = int(input())
    ps = list(map(int, input().split()))
    ps = [0] + ps

    cnt = 0
    for i in range(1, N + 1):
        if ps[i] == i:
            continue
        if ps[ps[i]] == i:
            cnt += 1
        else:
            print('NO')
            return
    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()))

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()))
count =0

for i in range(1,n+1):
    if i == p[i-1]:
        pass
    else:
        count += 1
        if count ==3:
            print('NO')
            exit(0)
print('YES')

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
q=sorted(p)
count=0
for i in range(len(p)):
  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())
A = list(map(int, input().split()))
cnt = 0
for i, a in enumerate(A):
    if i+1 != a:
        cnt += 1

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

Submission status: Accepted

Solution code:
n=int(input())
l=[int(x) for x in input().split()]
cnt=0
x=0
y=0
b=0
for i in range(n):
    if(i+1!=l[i]):
        cnt+=1
if(cnt>2):
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n): return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
p=n1()
f=False
q=sorted(p)

if p==q:
    f=True
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 a==q:
                f=True
                break
p0(f,"YES","NO")

Submission status: Accepted

Solution code:
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf


n = iinput()
p = iarr()
a = [i for i in range(1,n+1)]

ans = 'NO'
for i in range(n):
    for j in range(n):
        pp = cp.deepcopy(p)
        pp[i],pp[j] = pp[j],pp[i]
        if pp == a: ans = 'YES'; break
    
print(ans)

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('NO')
        exit()

print('YES')

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

for i in range(N):
    if P[i] != i+1:
        j = P.index(i+1)
        P[i],P[j] = P[j],P[i]
        break

print("YES" if P == list(range(1,N+1)) else "NO")

Submission status: Accepted

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

for i in range(n):
    if i+1 != p[i]:
        if switch == 2:
            print("NO")
            exit()
        
        if switch == 1:
            if memory == i+1:
                switch = 2
            else:
                print("NO")
                exit()
            
        else:
            switch = 1
            memory = p[i]

print("YES")

Submission status: Accepted

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

list2=[i for i in list1]

    
list1.sort()

count=0

for i in range(n):
    if(list1[i]!=list2[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()))
q = sorted(p)
s = sum(pi != qi for pi, qi in zip(p, q))
if s == 0 or s == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())

p=[int(_) for _ 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()))
pi = sorted(p)
c = 0
for i in range(n):
	if p[i] != pi[i]:
		c += 1

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