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())
pn = list(map(int, input().split()))
 
pnSort = sorted(pn)
 
if pn == pnSort:
    print("YES")
else:
    change = []
    index = 0
    for i,j in zip(pn, pnSort):
        if i != j:
            change.append(index)
            if len(change) == 2:
                break
        index += 1
 
    tmp = pn[change[0]]
    pn[change[0]] = pn[change[1]]
    pn[change[1]] = tmp
    
    if pn == pnSort:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
sorted_l = sorted(l)
for i in range(n):
  for j in range(i,n):
    tmp = l.copy()
    tmp[i], tmp[j] = tmp[j], tmp[i]
    if tmp==sorted_l:
      print("YES")
      exit()
print("NO")

Submission status: Accepted

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

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 unittest


class TestB(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([5, 2, 3, 4, 1]), 'YES')

    def test_2(self):
        self.assertEqual(think([2, 4, 3, 5, 1]), 'NO')

    def test_3(self):
        self.assertEqual(think([1, 2, 3, 4, 5, 6, 7]), 'YES')


def solve():
    p = read()
    result = think(p)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_float(n):
    return list(map(lambda x: float(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(data):
    reverse = []
    for i in range(1, len(data)):
        if data[i] < data[i - 1]:
            if reverse:
                reverse.append(i)
            else:
                reverse.append(i - 1)
    if not reverse:
        return 'YES'

    if len(reverse) == 2:
        i, j = reverse
        data[i], data[j] = data[j], data[i]
        for i in range(1, len(data)):
            if data[i] < data[i - 1]:
                return 'NO'
        return 'YES'
    else:
        return 'NO'


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

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

swap = False
for i in range(n):
  if p[i] != i+1:
    if swap == False and p[p[i]-1] == i+1:
      swap = True
      p[p[i]-1] = p[i]
    else:
      print('NO')
      sys.exit()

print('YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
import numpy as np
a = np.arange(1, n+1)
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:
n=int(input())
p=list(map(int,input().split()))
cnt=0
for i in range(0,n):
  if p[i]==i+1:
    cnt+=0
  else:
    cnt+=1
    
if cnt<=2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
P = list(map(int, 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')

Submission status: Accepted

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

count = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC135B - 0 or 1 Swap
def main():
    N, *P = map(int, open(0).read().split())
    flg = sum(i != j for i, j in enumerate(P, 1)) <= 2
    print("YES" if flg 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:
		cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

P.insert(0,0)

count=0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p =map(int, input().split())
l = list(range(1, (n + 1)))
P = list(p)

if P == l:
  print("YES")
else:
  N = 0
  i = []
  for x in range(n):
    if P[N] == l[N]:
      N += 1
    else:
      N += 1
      i.append(N)
  if len(i) == 2:
    P[i[0] - 1], P[i[1] - 1] = P[i[1] - 1], P[i[0] - 1]
    print("YES" if P == l else "NO")
  else:
    print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(i) for i in input().split()]
check = 0
pp = sorted(p)
for i in range(N):
    if p[i] != pp[i]:
        check += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
new_d=sorted(d)
count=0
x=0
y=0

for i in range(n-1):
  if d[i]>d[i+1] and count==1:
    d[y]=0
    d[y]+=d[i+1]
    d[i+1]=x
    count+=1
  if d[i]>d[i+1] and count==0:
    x+=d[i]
    y+=i
    count+=1
if d==new_d:
  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 <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
li2 = sorted(li)
cnt = 0
for i in range(n):
    if li[i] != li2[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)
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())) 
q = p[:]
q.sort()
m = 0

for i in range(n):
    if not p[i] == q[i]:
        m += 1
        
if m <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

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

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

else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

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


if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
a = [input().split() for l in range(2)]
N=int(a[0][0])
p=a[1]
count=0
for i in range(N):
    if int(p[i])!=i+1:
        count=count+1

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

Submission status: Accepted

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

cnt = 0

Q = sorted(P)

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

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

Submission status: Accepted

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

sort_p = sorted(p)
dict_p = {}
isOk = True
for i in range(N):
    if p[i] == sort_p[i]:
        continue
    elif len(dict_p) == 0:
        dict_p[sort_p[i]] = p[i]
    elif p[i] in dict_p:
        continue
    else:
        isOk = False
        break

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

Submission status: Accepted

Solution code:

def main():
    A = int(input())
    P = [int(x) for x in input().split()]

    count = 0
    for idx in range(len(P)):
        if P[idx] != idx+1:
            if P[P[idx]-1] == idx+1:
                count += 1
                if count == 3:
                    print('NO')
                    return
            else:
                print('NO')
                return

    print('YES')


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = input()
P = list(map(int, input().split()))
flag = 0
for idx, p in enumerate(P):
    if p != idx + 1:
        flag += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

for i in range(n-1,0,-1):
    if arr[i] == n:
        n -= 1
    elif flg == True:
        j = arr[i]
        arr[i] = n
        arr[j-1] = j
        n -= 1
        flg = False
    else:
        print('NO')
        exit()
print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

counter = 0

for i in range(N):		#rangeは範囲（長さ）を表すからNでよい
	if P[i] != Q[i]:
		counter = counter + 1

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

diff = sum(x != y for x,y in enumerate(P,1))

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

Submission status: Accepted

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

q = [int(x) for x in range(1,N+1)]

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

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

Submission status: Accepted

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

miscount = 0
for idx in range(len(p)):
	if(p[idx] != sortp[idx]):
		miscount += 1
if(miscount <= 2):
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
raw_input()
g=raw_input().split()
a=[int(x) for x in g]
n=len(a)
acc=[]
for i in range(n-1):
	if a[i]>a[i+1]:
		acc+=[i]
		if len(acc)>1:
			break
if len(acc)>1:
	a[acc[0]],a[acc[1]+1]=a[acc[1]+1],a[acc[0]]
for i in range(n-1):
	if a[i]>a[i+1]:
		print("NO")
		exit()
print("YES")

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
a=int(input())
b=list(map(int,input().split()))
count=0
for i,j in enumerate(b,1):
  if i != j:
    count +=1

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
L2 = [L[i]-i-1 for i in range(n)]
L3 = [i for i in L2 if i != 0]
if len(L3) == 0 or len(L3) == 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
c = list(map(int, input().split()))  # 複数数値入力(リスト格納)
ans = False
for i in range(0,n-1):
    for j in range(i, n):
        flg = False
        # ここでiとjを入れ替えたものをつくり、昇順ならbreak
        li = copy.copy(c)
        li[j], li[i] = li[i], li[j]

        for k in range(1, n+1):
            if li[k-1] != k:
                flg = True
                break
        
        if not flg:
            ans = True 

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

Submission status: Accepted

Solution code:
n=int(input())
p=input()
P=p.split()
count=0
for i in range(len(P)):
    P[i]=int(P[i])
for j in range(len(P)):
    if P[j]!=j+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()))
g=sum((1 for i in range(n) if i+1 !=p[i]))
print("YNEOS"[::2] if g==2 or g==0 else "YNEOS"[1::2])

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

Submission status: Accepted

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

cnt = 0
for idx, val in enumerate(p):
    if idx + 1 != val:
        cnt += 1
        if cnt == 3:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

Solution code:
n = int(input())
p = list(int(x) for x in input().split())
ans = 0

for i in range(n):
    if i+1 != 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()))
count = 0

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

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

Submission status: Accepted

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

s2 = sorted(s1)
for i in range(n):
  if s1[i] != s2[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()))
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(map(int, input().split()))
cnt = 0
for i in range(len(p)):
  if p[i] != i + 1:
    cnt += 1
if cnt == 0 or cnt == 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
def swap():
  N = int(input())
  P = [int(x) for x in input().split()]
  Q = sorted([int(x) for x in P])
  ans = 0
  for i in range(N):
    if ans!=3:
      if P[i]!=Q[i]:
        ans += 1
    else:
      break
  if ans==0 or ans==2:
    print("YES")
  else:
  	print("NO")
if __name__=="__main__":
  swap()

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:
n = int(input())
p = list(map(int,input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flg = 0
for idx in range(N):
    if p_l[idx] != idx+1:
        flg += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

counter = 0
answer = 'YES'
for i in range(N):
    if p[i] != i + 1:
        counter += 1
    if counter > 2:
        answer = 'NO'
        break

print(answer)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

o=1
e=0
for i in p:
  if i != o:
    e +=1
  o +=1
if e==0 or e==2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n= int(input())
d=list(map(int,input().split()))
d2=sorted(d)
k=0
count=0
for i in range(n-1):
  if d[i]>d[i+1] and count==0:
    k=d[i]
    count+=1
    for j in range(i+2,n):
      if d[i+1]>d[j]:
        d[i]=d[j]
        d[j]=k
        break
if d2==d:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

lN=[]
for i in range(1,N+1):
  lN.append(i)
  
cnt = 0  
for i in range(N):
  if lN[i] != P[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()))
t = list(range(1,n+1))
a = sum([p[i]==t[i]for i in range(n)])
print('YES'if n-a==2or n==a else'NO')

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
#p copy q
import copy
q=copy.copy(p)
a=0
for i in range(n):
    if p[i]!=i+1 and a==0:
        q[i]=int(p[p[i]-1])
        q[p[i]-1]=p[i]
        a+=1
p.sort()
if p==q:
    print("YES")
else:
    print("NO")
#135b

Submission status: Accepted

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

j=1
numofswap=0
for _p in p:
    if _p != j:
        numofswap+=0.5
    j+=1

print("YES" if numofswap == 0 or numofswap == 1 else "NO")

Submission status: Accepted

Solution code:
import numpy as np

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

for i in range(N - 1):
  for j in range(N - i):
    p[i], p[i + j] = p[i + j], p[i]
    if sorted(p) == p:
      Jud = 1
      break
      
    p[i], p[i + j] = p[i + j], p[i]
    
  else:
    continue  
  break
  
if Jud == 1:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

isFail = 0

for i in range(N):
  if(l[i] == i + 1): continue
  if(isFail):
    isFail = 2
    break
  
  isFail = 1
  dest = l[i] - 1
  l[i], l[dest] = l[dest], l[i]

print('YES' if isFail < 2 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

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


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

    cnt = 0
    for i in range(N-1):
        if P[i] != i+1:
            cnt += 1
    
    if cnt <= 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

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


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
def canSort(num, mylist):
    num1, num2, count = 0, 0, 0
    
    for i in range(1,num):
        if mylist[i] < mylist[i - 1]:
            count += 1
            
            if num1 == 0:
                num1 = i
            else:
                num2 = i
                
    if count > 2:
        return False
    
    if count == 0:
        return True
    
    if count == 2:  
        (mylist[num1 - 1], mylist[num2]) = (mylist[num2], mylist[num1 - 1])   
    elif count == 1:  
        (mylist[num1 - 1], mylist[num1]) = (mylist[num1], mylist[num1 - 1]) 
    
    for i in range(1,num):
        if mylist[i] < mylist[i - 1]:
            return False
    
    return True

N = int(input())
mylist = list(map(int, input().split(None,N)[:N]))
num = len(mylist)

if canSort(num, mylist):
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
dif = 0
for i in range(n):
    if p[i] != i + 1:  # 並び替えはせず、インデックスとの比較でOK
        dif += 1
if dif == 0 or dif == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
q = sorted(p)
dif = 0
for i in range(n):
    if p[i] != q[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()))
cnt = 0
for i in range(n):
  if p[i] != i + 1:
    cnt += 1
if cnt <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

miss_count = 0

for i, p in enumerate(P, start=1):
    if not i == p:
        miss_count += 1

    if miss_count > 2:
        print('NO')
        sys.exit()
print('YES')