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:
import numpy as np
import sys

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

if all(p==p_s):
    print("YES")
else:
    diff = p_s - p
    diff_index = np.where(diff!=0)
    #print(diff_index[0][1])
    if len(diff_index[0]) != 2:
        print("NO")
    else:
        p0 = p[diff_index[0][0]]
        p[diff_index[0][0]] = p[diff_index[0][1]]
        p[diff_index[0][1]] = p0
        if all(p == p_s):
            print("YES")
        else:
            print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p_l = list(map(int,input().split()))
place = -1
cnt = 0
for i in range(n-1):
        if p_l[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()))

w = []
for i in range(0, len(p)):
  if i+1 != p[i]:
    w.append(i)

if len(w) == 0:
  print("YES")
  exit()

if (len(w) == 2):
  p[w[0]], p[w[1]] = p[w[1]], p[w[0]]
  if (list(range(1, N+1)) == p):
    print("YES")
    exit()

print("NO")

Submission status: Accepted

Solution code:
# coding: utf-8
a = int(input())
s = list(map(int, input().split(" ")))
t = sorted(s)
ans = 'NO'
all = 0
c = 0

sn = 0
sv = 0
snn = 0

for i in range(a):
    if i == 0:
        continue
    elif s[i] < s[i-1] and c == 0:
        sn = i-1
        sv = s[i-1]
        c = 1
    elif s[i] < s[i-1] and c == 1:
        snn = s[i]
        s[i] = sv
        s[sn] = snn 
        break

for i in range(a):
    if s[i] == t[i]:
        continue
    else:
        all = 1
        break

if all == 0 :
    ans = 'YES'

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
p0 = sorted(p)
key = 0
if p == p0:
    print("YES")
    key = 1
if key == 0:
    cnt = 0
    for i in range(len(p)):
        if p[i] != p0[i] and cnt == 0:
            idx1 = i
            cnt += 1
        elif p[i] != p0[i] and cnt == 1:
            idx2 = i
            cnt += 1
        if cnt >= 2:
            p[idx1], p[idx2] = p[idx2], p[idx1]
            if p == p0:
                print("YES")
                break
            else:
                print("NO")
                break
"""
    for i in range(len(p)):
        pt = p
        for j in range(len(p)):
            if i != j:
                pt[i], pt[j] = pt[j], pt[i]
                if pt == p0:
                    print("YES")
                    key = 1
                    break
"""

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:
import sys
n = int(input()) 
p = [int(r) for r in sys.stdin.read().split()]
for i, v in enumerate(p[:-1]):
    if p[i] > p[i + 1]:
        for j in range(i + 1, n - 1):
            if p[j] > p[j + 1]:
                if (i == 0 or p[i - 1] < p[j + 1]) and p[i + 1] > p[j + 1] and (j == n - 2 or p[j + 2] > p[i]) and p[j] < p[i]:
                    print('YES')
                    quit()
                else:
                    print('NO')
                    quit()
            if j == n - 2:
                print('NO')
                quit()
    if i == n - 2:
        print('YES')
        quit()

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
P = list(range(1,N+1))
if p == P:
    print('YES')
    exit()

tmp = [i for i in range(N) if P[i] - p[i] != 0]
#print(tmp)

if len(tmp) != 2:
    print('NO')
else:
    p[tmp[0]], p[tmp[1]] = p[tmp[1]], p[tmp[0]]
    if p == P:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

c = 0
for p, q in zip(P, sorted(P)):
  if p != q:
    c += 1
    if c > 2:
      break

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy
n=int(input())
numl=list(map(int,input().split()))	
chk=sorted(numl)

flag=True
if chk==numl :
  flag=True
else :
  flag=False

for i in range(n) :
  #print(i)
  for j in range(n):
    #print(j)
    temp=copy.deepcopy(numl)
    temp[i]=numl[j]
    temp[j]=numl[i]
    #print(temp)
    if temp==chk :
      flag=True
      break

if flag==True:
  print("YES")
else :
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    min_n = ps[i]
    index = i
    for j in range(i+1, N):
        if ps[j] < min_n:
            min_n = ps[j]
            index = j

    if index != i:
        tmp = ps[i]
        ps[i] = ps[index]
        ps[index] = tmp
        break
    
sorted_ps = sorted(ps)
if ps == sorted_ps:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
A = int(input())
re = 0
B = list(map(int, input().split()))
C = [i for i in range(1, A+1)]
for B, C in zip(B, C):
  if B != C:
    re += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    P = list(map(int, input().split()))
    A = list(range(1, N + 1))
    x = 0

    if P == A:
        print("YES")
        return

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

    if x == 2:
        print("YES")
    else:
        print("NO")

    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(a)):
    if i+1 != a[i]:
        flag += 1

if flag <= 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())))
ps = sorted(p)
bools = p == ps

if sum(bools) >= len(p) - 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 >= 3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
N = int(input())

list_1 = input().split()
list_2 = []
x = 0

for i in range(N):
    list_2.append(i+1)

for i in range(N):
    if int(list_1[i]) == list_2[i]:
        x += 0
    else:
        x += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    n = int(input())
    ps = [int(i) for i in input().split()]
    sps = [i for i, j in zip(ps, sorted(ps)) if i != j]
    print("YES" if len(sps) <= 2 else "NO")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())
b=list(map(int,input().split()))
c=-1
for i in range(a-1):
  if b[i]>b[i+1]:
    if c==-1:
      c=i
    else:
      b[c],b[i+1]=b[i+1],b[c]
      break
      
for i in range(a-1):
  if b[i]>b[i+1]:
    print("NO")
    exit()
    
print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

q = sorted(p)

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

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

Submission status: Accepted

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

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

    target_i = None
    for i in range(1, N):
        if P[i-1] > P[i]:
            target = P[i-1]
            target_i = i-1
            if i-2 >= 0:
                low = P[i-2]
            else:
                low = None
            high = P[i]
            break
    
    # low超え high未満で、入れ替え可能な数字を探す
    if target_i is not None:
        for i in range(target_i+2, N):
            if P[i] < high:
                if low is None:
                    P[target_i] = P[i]
                    P[i] = target
                    break
                else:
                    if low < P[i]:
                        P[target_i] = P[i]
                        P[i] = target
                        break
                    else:
                        continue
    
    # 昇順か確認
    for i in range(N):
        if P[i] != i+1:
            print("NO")
            return
    print("YES")




if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
tigai = []
a = 1

for i in lis:
    if i == a:
        pass
    else:
        tigai.append([a,i])
    a = a+1
if len(tigai) == 0:
    print('YES')
elif len(tigai) != 2:
    print('NO')
else:
    if tigai[0][0] == tigai[1][1]:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

s = p.copy()
s.sort()
cnt = 0

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


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

Submission status: Accepted

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

p_sort = sorted(p)
diff = 0

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


#すでに昇順ソート済みだった場合
if p == p_sort:
    print("YES")
#一回入れ替えればOK
elif diff == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
AA=sorted(A)
li=[]
for i in range(N):
  if A[i]!=AA[i]:
    li.append(A[i])
if len(li)>2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

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

p = sorted(a)

if a == p:
    print "YES"
    exit()

flag = 0
for i in xrange(n):
    for j in xrange(i + 1, n):
        a[i], a[j] = a[j], a[i]
        if (a == p):
            flag = 1
            break
        a[i], a[j] = a[j], a[i]

    if flag: break

if flag:
    print "YES"
else:
    print "NO"

Submission status: Accepted

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

for i in range(n):
    for j in range(n):
        temp = p.copy()
        temp[i], temp[j] = temp[j], temp[i]
        sorted_temp = sorted(temp)

        if sorted_temp  == temp:
            print("YES")
            exit()

print("NO")

Submission status: Accepted

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

c = 0
var = 0
t = -1
for k in range(N):
  i = k+1
  if i == t:
    if p[i-1] == s:
      c = c+1
    else:
      var = var+1
  elif p[i-1] == i:
    c = c+1
  else:
    var = var+1
    s = i
    t = p[i-1]

if var <= 1:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import sys
import copy
N = int(input())
p = list(map(int, input().split()))
A = sorted(p)
if p == A:
  print("YES")
  sys.exit()
  
for i in range(N):
  for j in range(i+1,N):
    newp = copy.copy(p)
    newp[i], newp[j] = newp[j], newp[i]
    if newp == A:
      print("YES")
      sys.exit()
print("NO")

Submission status: Accepted

Solution code:
N = int(input())
ps = list(map(int,input().split()))
sort_ps = sorted(ps)
tmp = ps.copy()
c = False
if ps == sort_ps:
  print("YES")
else:
  for x in range(len(ps)):
    for y in range(x+1,len(ps)):
      tmp[x], tmp[y] = tmp[y], tmp[x]
      if tmp == sort_ps:
        c = True
      tmp = ps.copy()
  print('YES' if c == True else 'NO')

Submission status: Accepted

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

a = 0
b = []
c = []
for i in range(N):
    if i+1 != p[i]:
        a += 1
        b.append(p[i])
        c.append(i+1)

if a == 0:
    print("YES")
elif a == 2:
    if b[0] == c[1] and b[1] == c[0]:
        print("YES")
else:
    print("NO")

Submission status: Accepted

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

flag=0
save=0
for i in range(N-1):
  if flag==0 and P[i]>P[i+1]:
    save=i
    flag=1
    continue
  if flag==1 and P[i]>P[i+1]:
    tmp=P[i+1]
    P[i+1]=P[save]
    P[save]=tmp
    break

flag=0
for i in range(N-1):
  if P[i]>P[i+1]:
    print('NO')
    flag=1
    break

if flag!=1:
  print('YES')

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = list(map(int, input().split()))
a_sorted = sorted(a)
cant = True
if a == a_sorted:
	print("YES")
	quit()
for i in range(n):
	for j in range(n):
		a_c = copy.copy(a)
		if i >= j:
			continue
		a_c[i],a_c[j] = a_c[j],a_c[i]
		if a_c == a_sorted:
			cant = False
if cant:
	print("NO")
else:
	print("YES")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from copy import copy

input = sys.stdin.readline

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

    cnt = 0
    for i in range(N):
        if P[i] != P2[i]:
            cnt += 1
            if cnt == 3:
                print("NO")
                return
    print("YES")

main()

Submission status: Accepted

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

err=[]
err_idx=[]

for i in range(N):
  if (i+1)!=p[i]:
    err.append(p[i])
    err_idx.append(i)
if len(err)>=3:
  print("NO")
  exit()
elif len(err)==0:
  print("YES")
  exit()
#print(err)
p[err_idx[0]] = err[1]
p[err_idx[1]] = err[0]

for i in range(N):
  if (i+1)!=p[i]:
    print("NO")
    exit()
print("YES")

Submission status: Accepted

Solution code:
import sys

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

def sort_hantei(p):
    for i in range(len(p)-1):
        if p[i] > p[i+1]:
            return False
    return True

if sort_hantei(p):
    print('YES')
else:
    for i in range(n):
        for j in range(i+1, n):
            p_copy[i] = p[j]
            p_copy[j] = p[i]
            if sort_hantei(p_copy):
                print('YES')
                sys.exit()
            p_copy = p.copy()
    print('NO')

Submission status: Accepted

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

count = 0

for i in range(n):
    if a[i] != b[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()))	

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

Submission status: Accepted

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

cnt = 0
for i in range(1, n):
    if i != p_li[i - 1] :
        cnt += 1
if cnt <= 2 :
    print("YES")
else :
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = sorted(a)
if a == b:
    print("YES")
else:
    c = 0
    for i in range(n):
        if a[i] == b[i]:
            pass
        else:
            c+=1
    if c == 2:
        x = []
        y = []
        for i in range(n):
            if a[i] == b[i]:
                pass
            else:
                x.append(a[i])
                y.append(i)
        a[y[1]] = x[0]
        a[y[0]] = x[1]
        
        if a == b:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
l = stdin.readline().rstrip().split()

n = 0
for i in range (len(l)):
  if int(l[i]) == i+1:
    n += 1
  else:
    n += 0
if n == N or n == N-2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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


for i in range(N-1):
    if P[i] > P[i+1]:
        tmp = P[P[i]-1]
        P[P[i]-1] = P[i]
        P[i] = tmp
        break

if P == sorted(P):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
# print(N, a)
ge_cnt = 0
diffs = []
for i in range(N-1):
    if a[i] > a[i+1]:
        ge_cnt += 1
        diffs.append(a[i+1] - a[i])

if ge_cnt == 0:
    print('YES')
elif ge_cnt == 2:
    if diffs[0] == diffs[1]:
        print('YES')
    else:
        print('NO')
else:
    print('NO')

Submission status: Accepted

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

m = 0

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


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

Submission status: Accepted

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

onceflag = 0

for i in range(n):
    if numbers[i] == i + 1:
        continue
    else:
        onceflag += 1

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

Submission status: Accepted

Solution code:
n = (int(input()))
a = list(map(int,input().split()))
b = sorted(a)
j=0
u = [x-y for (x,y) in zip(a,b)]
for a in u:
    if(a!=0):
        j = j+1
if(j>2):
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
N=int(input())
P=[int(x) for x in input().split()]
 
N_J=list(range(1,N+1))
ans='NO'
for i in range(N):
    for j in range(N):
        P_temp=P.copy()
        p_i=P[i]
        p_j=P[j]
 
        P_temp[i]=p_j
        P_temp[j]=p_i
        
        if P_temp==N_J:
            ans='YES'
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst)
if lst==lst2:
  print("YES")
else:
  tmp = 0

  for i in range(n):
    if lst[i]!=lst2[i]:
      tmp+=1
      
  if tmp != 2:
    print("NO")
  else:
    print("YES")

Submission status: Accepted

Solution code:
N=int(input())
p = list(map(int, input().split()))
#p=int(input()).split()
q=[]
q=sorted(p)
#print(p,q)
count=0
for i in range(N):
    if p[i]!=q[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()))

flag = 0
ans = "NO"

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

if flag == 0 or flag == 2:
  ans = "YES"
  
print(ans)

Submission status: Accepted

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

count = 0

p_sort = sorted(p)

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

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

Submission status: Accepted

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

i=0
i_index=0
j=0
j_index=0
for a in range(1, n+1):
  if a != l[a-1] and i == 0:
    i = l[a-1]
    i_index = a - 1
  elif a != l[a-1] and j == 0:
    j = l[a-1]
    j_index = a - 1

if i==0 and j==0:
  print("YES")
else:
  l[i_index]=j
  l[j_index]=i
  if sorted(l) == l:
    print("YES")
  else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
for k in range(n-1):
    if p[k+1]-p[k]!=1:
        break
    if k==n-2:
        print("YES")
        exit()
for i in range(n-1):
    for j in range(i+1,n):
        pn=p.copy()
        x=pn[i]
        pn[i]=pn[j]
        pn[j]=x
        for k in range(n-1):
            if pn[k+1]-pn[k]!=1:
                break
            if k==n-2:
                print("YES")
                exit()
print("NO")

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
p = LI()

p_sorted = sorted(p)

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N=int(input())
p=list(map(int,input().split()))
count=0
origin=[j+1 for j in range(N)]
for i in range(N):
    if p[i]!=origin[i]:
        count=count+1
if count>=3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
tmp = p[:]
new_p = sorted(p)
ans = 'NO'
for i in range(n):
    for j in range(n):
        tmp = p[:]
        tmp[i], tmp[j] = tmp[j], tmp[i]
        if tmp == new_p:
            ans = 'YES'
print(ans)

Submission status: Accepted

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

for i in range(N):
    if p[i] != i+1:
        tmp = p[i]
        p[i], p[tmp-1] = p[tmp-1], p[i]
        break

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def check(i,j,number_list):
    number=[]
    for k in number_list:
        number.append(k)
    k=number[i]
    number[i]=number[j]
    number[j]=k
    flag=1
    for l in range(len(number_list)):
        if (int(number[l])==(l+1)):
            continue
        else:
            flag=0
            break
    return flag
N=int(input())
in_number_list=input().split(" ")
number_list=[]

for i in in_number_list:
    number_list.append(int(i))

flag=0

for i in range(N):
    for j in range(i,N):
        a=check(i,j,number_list)
        if(a==1):
            print("YES")
            flag=1
            break
    if(flag==1):
        break
if(flag==0):
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

N = int(input())
p = [int(i) for i in input().split()]
q = [i+1 for i in range(N)]

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

count = []
tmp_num = 0

for p_n in p:
    dif = p_n - tmp_num
    if dif < 0:
        count.append(abs(dif))
    tmp_num = p_n

if len(count) == 0:
    print("YES")
elif len(count) == 2:
    if count[0] == count[1]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

a=0
for i in range(n):
    if p[i]==q[i]:
        a+=1
    else:
        a+=0

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

Submission status: Accepted

Solution code:

def main():
	N = int(input()) 
	p = [int(f) for f in input().split()]
	
	A = [f for f, i in zip(p, range(1, N+1)) if not f-i == 0]

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


if __name__ == "__main__":
	main()

Submission status: Accepted

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

psort = [0] * N
for i in range(N):
    psort[i] = p[i]
psort.sort()

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

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

list_sorted = list.copy()
list_sorted.sort()

stock = 0
for i in range(n):
    if list[i] != list_sorted[i]:
        stock += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

d = []

for i in range(N):
  d.append(p[i]-P[i])
  
D = d[:]
while 0 in D:
  D.remove(0)
  
if max(d) == 0:
  print('YES')
elif len(D) == 2:
  if D[0] == (-D[1]):
    print('YES')
  else:
    print('NO')
else:
  print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
d = input().split()
 
# print(n)
# print(d)

cnt = 1
a=[]
b=[]
for i in d:
    if(int(i) != cnt):
        a.append(d.index(i)+1)
        b.append(int(i))
        # print("i,cnt=",i,cnt)
    cnt += 1


if(len(a)==2 or len(a)==0):
    b.sort()
    if(a==b):
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

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

sortedP = sorted(P)
idx = []
for i in range(N):
    if P[i] != sortedP[i]:
        idx.append(i)
if len(idx) == 0:
    print("YES")
elif len(idx) == 2:
    P[idx[0]], P[idx[1]] = P[idx[1]], P[idx[0]]
    if P == sortedP:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
P=list(map(int,input().split()))
if P==list(range(1,n+1)):
    print('YES')
else:
    for i in range(n-1):
        for j in range(i,n):
            Q=P[:]
            Q[i]=P[j]
            Q[j]=P[i]
            if Q==list(range(1,n+1)):
                print('YES')
                exit()
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())

seq = map(int, input().split())

diff = 0

for i, v in enumerate(seq, start=1):
    if i != v:
        diff += 1

answer = "YES" if diff in (0, 2) else "NO"
print(answer)

Submission status: Accepted

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

cnt = 0
for i in range(n-1):
	if numbers[i] == i +1 :
		continue
	else:
		cnt += 1

if cnt > 2:
	result = "NO"
else:
	result = "YES"

print(result)

Submission status: Accepted

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

from copy import deepcopy

y=sorted(x)

res='NO'

if x==y:
	res='YES'
else:
	for i in range(n-1):
		for j in range(i+1,n):
			z=deepcopy(x)
			a=z[i]
			b=z[j]
			z[i]=b
			z[j]=a
			if z==y:
				res='YES'
				break
		if res=='YES':
			break

print(res)

Submission status: Accepted

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

count = 0
for i in range(n):
  if not p[i] == p2[i]:
    count += 1
  if count >= 3:
    print('NO')
    exit()
print('YES')