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:
input()
*p,=map(int,input().split())
print('NO' if 2<sum([1 for i,j in zip(p,sorted(p)) if i!=j]) else 'YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
li=list(map(int,input().split()))
lis=sorted(li)
dif=0
for (x,y)in zip(li,lis):
  if x!=y:
    dif+=1
if dif<3:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())
ps = [int(x) for x in sys.stdin.readline().split(" ")]


wrong_count = 0
for i in range(0, N):
    if ps[i] == i + 1:
        continue
    wrong_count += 1

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

Submission status: Accepted

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

for i in range(N):
    if order[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()))
Q=sorted(p)
count=0
for i in range(N):
    if p[i]!=Q[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()))
cnt = sum([(x - i) != 0 for i, x in enumerate(P, 1)])
if cnt == 0 or cnt == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
s = list(map(int,input().split()))
t = sorted(s)
if s==t:
    print('YES')
else:
    ans = sum(1 for i,j in zip(s,t) if i!=j)
    print('NO' if ans>2 else 'YES')

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    P = list(map(int, input().split()))
    diff = 0
    for p_org, p_sort in zip(P, sorted(P)):
        if p_org != p_sort:
            diff += 1
    if diff == 0 or diff == 2:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

Submission status: Accepted

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

p_sorted = sorted(p)
diff = 0
for i in range(n):
    if p[i] != p_sorted[i]:
        diff += 1
print('YES'if diff <= 2 else '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 p[i-1]!=i:
    cnt += 1
print('YES' if cnt<=2 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()

    cnt = 0
    for n in range(N):
        if P[n] != n + 1:
            cnt += 1
        if cnt > 2:
            print('NO')
            exit()
    else:
        print('YES')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N = ri()
P = rl()

import itertools

def check(P):
	#print(P)
	prev = P[0]
	for i in range(1, len(P)):
		if prev > P[i]:
			return False
		prev = P[i]
	return True

P0 = list(P)
if check(P):
	print('YES')
	exit()
for c in itertools.combinations(list(range(N)), 2):
	i, j = c
	#print(i, j)
	P = list(P0)
	P[i], P[j] = P[j], P[i]
	if check(P):
		print('YES')
		exit()
print('NO')

Submission status: Accepted

Solution code:
N = input()
number_list = [int(i) for i in input().split()]
sorted_list = sorted(number_list)
count = 0
for i,j in zip(number_list, sorted_list):
    if i != j:
        count += 1

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
n_l = list(range(1,n + 1))
count = 0
for n_num , p_num in zip(n_l,p):
  if n_num != p_num:
    count += 1
if count == 2 or count == 0:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    P = list(map(int, input().split()))
    expected = []
    real = []
    for i in range(1, N+1):
        if i == P[i-1]:
            continue
        expected.append(i)
        real.append(P[i-1])
    print("YES" if len(real) == 0 or (len(real)==2 and len(expected)==2 and expected[0]==real[1] and expected[1]==real[0]) else "NO")

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

a = np.arange(1, n+1)
tmp = p - a
cnt = np.count_nonzero(tmp != 0)

flg = False
if cnt == 0 or (cnt == 2 and tmp.sum() == 0):
    flg = True

print('YES') if flg else print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
r = list(range(1,n+1))
t = 0

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

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
li = rl()
li1 = sorted(li)
cnt = 0
for a, b in zip(li, li1):
  if a != b:
    cnt += 1
print('NO' if cnt > 2 else 'YES')

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
if a == [i for i in range(1, n+1)] or sum(np.array(a)==np.array([i for i in range(1, n+1)]))==n-2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
#create date: 2020-07-05 10:48

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    p = na()
    for i in range(n-1):
        for j in range(i, n):
            q = p.copy()
            q[i] = p[j]; q[j] = p[i]
            now = q[0]
            f = True
            for qi in q:
                if now > qi:
                    f = False
                    break
                now = qi
            if f:
                print("YES")
                quit()
    print("NO")



if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 0
for i in range(N):
  for j in range(i+1,N):
    temp = 0
    good = True
    for k in range(N):
      if k == i:
        if temp > p[j]:
          good = False
          break
        else:
          temp = p[j]
      elif k == j:
        if temp > p[i]:
          good = False
          break
        else:
          temp = p[i]
      else:
        if temp > p[k]:
          good = False
          break
        else:
          temp = p[k]
    if good:
      break
  if good:
    break

temp = 0
good2 = True
for k in range(N):
    if temp > p[k]:
      good2 = False
      break
    else:
      temp = p[k]
    
if good or good2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

k = 0
for i in range(0, len(pn)):
    if (i+1) != pn[i]:
      k += 1

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

for n in range(N):
  if P[n]!=1+n:
    d+=1

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

Submission status: Accepted

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

for p,q in zip(P,Q):
  if p!=q:
    d+=1

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

Submission status: Accepted

Solution code:
n,*p=map(int,open(0).read().split());l=len(p)
np=sorted(p);cou=0
for i in range(l):
  if p[i]!=np[i]:cou+=1
print("YES" if (cou==2 or cou == 0) else "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 or cnt==0:print("YES")
else: print("NO")

Submission status: Accepted

Solution code:
N=input()
N=int(N)
ls=[int(s) for s in input().split()]
count=0
for i in range(N):
  if ls[i]==i+1:
    count=count+1
if count>=N-2:    
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n=int(input())
p=[int(j) for j in input().split()]
s=range(n+1)
x=0
  
c=0

for a in range(n):
  if p[a]!=s[a+1]:
    c=c+1
  

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

Submission status: Accepted

Solution code:
n=[int(s) for s in input().split()]
p=[int(s) for s in input().split()]
count=0
expected=0
a=0
f=0
for i in p:
  if i==a+1:
    a+=1
  else:
    if count>0:
      f=1
    if expected==0:
      expected=i
      a+=1
    else:
      if expected==a+1:
        count+=1
        a+=1
      else:
        f=1

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    p = list(map(int, input().split()))
    check = [i for i in range(1, n+1)]
    cnt = 0
    for a, b in zip(p, check):
        if a != b:
            cnt += 1
    if cnt == 0 or 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()))
answer = 0
for i in range(n):
  if p[i]  != (i + 1):
  	answer += 1
  
if answer <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
ok=True
for i in range(1,n):
  if p[i]-p[i-1]>0:
    continue
  ok=False
if ok:
  print("YES")
  exit(0)

for i in range(n):
  for j in range(n):
    if i==j:continue
    t=p[:]
    taihi=t[i]
    t[i]=t[j]
    t[j]=taihi
    ok=True
    for k in range(1,n):
      if t[k]-t[k-1]>0:
        continue
      ok=False
    if ok:
      print("YES")
      exit(0)
print("NO")

Submission status: Accepted

Solution code:
import sys

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

targets = {}
for i in range(N):
    if P[i] != i+1:
        targets[i] = P[i]

# print(targets)
if not targets:
    print("YES")
elif len(targets) == 2:
    for i, v in targets.items():
        if v-1 in targets and targets[v-1] == i+1:
            print("YES")
        else:
            print("NO")
        break
else:
    print("NO")

Submission status: Accepted

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n = I()
    p = list(IL())
    q = sorted(p)
    flag = 3
    for i in range(n):
        if p[i] != q[i]:
            flag -= 1
        if not flag:
            print("NO")
            exit()
    print("YES")
    return

if __name__=='__main__':
    Main()

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
c=0
for i in range(n):
  c+=(a[i]==i+1)*1
print(['YES','NO'][c<n-2])

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Ps = list(mapint())

S = Ps[:]
S.sort()

cnt = 0
for p, s in zip(Ps, S):
    if not p==s:
        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 = list(range(1,(n+1)))

if p == l:
    ans = "YES"
cnt = 0
for i in range(n):
    if p[i] != l[i]:
        if p[p[i]-1] == l[i]: cnt += 1
        else:
            ans = "NO"
            break
if cnt > 0:
    print("YES" if cnt//2 == 1 else "NO")
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if all(a<b for a,b in zip(P,P[1:])):
    print('YES')
    exit()

for i in range(N-1):
    for j in range(i+1, N):
        P[i],P[j] = P[j],P[i]
        if all(a<b for a,b in zip(P,P[1:])):
            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()))

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

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = sorted(A)
dif = 0
for i in range(n):
  if A[i]!=B[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()))

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()
    cnt = 0
    for n in range(N):
        if P[n] != n + 1:
            cnt += 1
        if P[P[n]-1] != n+1:
            print('NO')
            exit()
    print('NO' if cnt > 3 else 'YES')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *p = map(int, open(0).read().split())
print("YNEOS"[sum(p[i] != i+1 for i in range(n)) not in (0, 2)::2])

Submission status: Accepted

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

sort_P = list(range(1, N+1))
wrong = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Count = 0
for T in range(0,N):
    if Sort[T] != P[T]:
        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()))
res = 0
for i in range(n):
    if p[i]!=i+1:res +=1
print("YES" if res <=2 else "NO")

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(z) for z in input().split()]
k = sorted(p)
poss = False

for i in range(n):
      for j in range(i+1,n):
            new = p[:i]+[p[j]]+p[i+1:j]+[p[i]]+p[j+1:]
            #print(new)
            if (new)==k:
                  poss = True
                  break
if (p==k):
      poss=True

if (poss):
      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 or cnt==0:
  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 i,j in zip(p,sort_p):
    if(i!=j):
        count+=1
if(count==2 or count==0):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

    tmp = 0
    for i in range(len(inlis)):
        if inlis[i] != sortlis[i]:
            tmp += 1
    if tmp == 0 or tmp == 2:
        print("YES")
    else:
        print("NO")

    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

newlist=[x-y for (x,y) in zip (p,alis)]


if sum(1 for x in newlist if x!=0)==2 or sum(1 for x in newlist if x!=0)==0 :
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

judge = 0
for i in range(n):
    if p[i] != i + 1:
        judge += 1
if judge >= 3:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import exit
n=int(input())
p=list(map(int,input().split()))
s=sorted(p)
if p==s:
	print("YES")
	exit(0)
for i in range(n-1):
	for j in range(i+1,n):
		p[i],p[j] = p[j],p[i]
		if p == s:
			print("YES")
			exit(0)
		p[i],p[j] = p[j],p[i]
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(map(int,input().split()))
q = p
k = sorted(p)
count = 0
for i in range(n):
    if q[i] != k[i]:
        count += 1
if count == 2 or count ==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    P = il()
    Q = [n+1 for n in range(N)]

    cnt = 0
    for p, q in zip(P, Q):
        if p != q and P[p-1] != q:
            print('NO')
            exit()
        elif p != q:
            cnt += 1
    else:
        if cnt/2 < 2: print('YES')
        else: print('NO')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
cnt = 0
for i in range(n):
  if i + 1 != lst[i]:
    cnt += 1
  if cnt >= 3:
    print('NO')
    exit()
print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

flg = 0
for num, val in enumerate(p):
    if (num + 1) != val:
        flg += 1
if flg == 2 or flg == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

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

arr2 = sorted(arr)

count = 0 

for i in range(0,n):
  if arr[i] != arr2[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()))

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:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *P = map(int, read().split())

    k = sum(1 for i, p in enumerate(P, 1) if i != p)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *P = map(int, read().split())
    
    if P == sorted(P):
        print('YES')
        return
    
    for i in range(N-1):
        for j in range(i+1, N):
            B = P[:]
            B[i] = P[j]
            B[j] = P[i]
            if B == sorted(P):
                print('YES')
                return
                
    print('NO')
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 135 B

def swap(k, a, b):
    k[a], k[b] = k[b], k[a]
    return k

n = int(input())
l = list(map(int, input().split()))
num = 0
k = []
ans = []

for i in range(len(l)):
    k.append(l[i])

k.sort()

for i in range(n):
    if k[i] != l[i]:
        num += 1
        ans.append(i)

if num == 2 :
    swap(l, ans[0], ans[1])
    if k == l:
        print("YES")
    else:
        print("NO")
elif num == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
flg = False
for i in range(N - 1):
    for j in range(i, N):
        temp_p = p.copy()
        temp = temp_p[i]
        temp_p[i] = temp_p[j]
        temp_p[j] = temp
        if temp_p == sorted_p:
            print('YES')
            flg = True
            break
    if flg:
        break
else:
    print('NO')

Submission status: Accepted

Solution code:
def main():
    N = input()
    A = list(map(int, input().split()))

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
l = LI()
x = 0
for i in range(1, N+1):
    if i != l[i-1]:
        x += 1
if x > 2:
    print('NO')
else:
    print('YES')