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_sort = np.sort(p)

if (p==p_sort).all():
  print("YES")
  sys.exit()

if np.count_nonzero(p!=p_sort)==2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
lists = list(map(int,input().split()))
count = 0
num = 1
for sort in lists :
         if num != sort:
                  count += 1
                  num += 1
         else:
                  num += 1
if count >= 3:
         print("NO")
else:
         print("YES")

Submission status: Accepted

Solution code:
import sys

N = int(input())
for line in sys.stdin:
  p = list(map(int,line.split(' ')))


cnt = 0
for n in range(1,N+1):
  _ = p[n-1]
  if n == _:
    continue
  else:
    cnt += 1
    
if cnt > 2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(lambda x: int(x),input().split(" ")))
same_value = 0

for i in range(1, n+1):
  if i == data[i-1]:
    same_value += 1
  else:
    pass

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

Submission status: Accepted

Solution code:
#B - 0 or 1 Swap(ABC135)
N=int(input())
P=list(map(int, input().split()))
P1=sorted(P)
P2=[P[i]-P1[i] for i in range(N)]
if P2.count(0)<N-2:
    print("NO")
else:
    print("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>2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
import sys

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

for i in range(n - 1):
    if p[i] > p[i + 1]:
        break
    if i == n - 2:
        print("YES")
        sys.exit()

for i in range(n):
    for j in range(i + 1, n):
        q = list(p)
        tmp = q[i]
        q[i] = q[j]
        q[j] = tmp
        for k in range(n - 1):
            if q[k] > q[k + 1]:
                break
            if k == n - 2:
                print("YES")
                sys.exit()

print("NO")

Submission status: Accepted

Solution code:
N = int(input())
P = [int(p) for p in input().split()]
sorted_P = sorted(P)
counter = 0
for i in range(N):
    if P[i] != sorted_P[i]:
        counter += 1
        if counter > 2:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
sorted_arr = sorted(arr)

counter = 0
for i in range(n):
    if arr[i] != sorted_arr[i]:
        counter += 1
    if counter > 2:
        break

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()))
sP=sorted(P)
a=0
for i in range(N):
  a+=1 if P[i]!=sP[i] else 0
print("YES" if a<=2 else "NO")

Submission status: Accepted

Solution code:
N = int(input())
li = [int(_) for _ in input().rstrip().split()]

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

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

Submission status: Accepted

Solution code:
N=int(raw_input())
ary = map(int, raw_input().split())
c_num = 0

for i in range(N):
    if ary[i] != (i+1):
        c_num += 1

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

Submission status: Accepted

Solution code:
n=input()
p=list(map(int,input().split()))
cnt=0
for i in range(len(p)):
  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()))
ps = sorted(p)

c = 0
ans = 'YES'
for i in range(0, len(p)):
  if p[i] != ps[i]:
    c += 1
    if c > 2:
      ans = 'NO'
      break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

count = 0
for i, p in enumerate(map(int, input().split())):
  if (i + 1) != p:
    count += 1
    
if (count == 0) or (count == 2):
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
nums = [int(s) for s in input().split()]
for i in range(n -1):
  if nums[i] < nums[i + 1]:
    pass
  else:
    p = nums[i]
    nums[i] = min(nums[i + 1:])
    nums[np.argmin(nums[i + 1:]) + i + 1] = p
    break

if nums == [s + 1 for s in range(n)]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
num = int(input())
order = []
for i in range(1,num+1):
    order.append(i)
#print(order)

q_num = list(map(int,input().split()))
#print(q_num)
cnt = 0
for j in range(num):
    if order[j]!=q_num[j]:
        cnt = cnt+1
    else:
        pass
if cnt <=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int, input().split()))
sorted_nums = sorted(nums)

cnt = 0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

err_cnt = 0
cnt = 1

for i in a:
  if i != cnt:
    err_cnt += 1
  cnt += 1

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

Submission status: Accepted

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

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
L_sorted = sorted(L, reverse=False)
subtract = [x - y for (x, y) in zip(L, L_sorted)]
if subtract.count(0) == N-2 or subtract.count(0) == N:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = p.copy()
q.sort()
cnt = 0
for i in range(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()))
l=sorted(p)

ans=0

for i in range(0,n):
    if p[i] != l[i]:
        ans+=1
if ans >2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li_re = sorted(li)

point = 0

for i in range(len(li)):
    if li_re[i] != li[i]:
        point += 1

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

Submission status: Accepted

Solution code:
a=int(input())
b=input().split(" ")
sequence=[]
for i in range(0,a):
    sequence.append(int(b[i]))
error=[]
for q in range(0,a-1):
    if sequence[q]-sequence[q+1]>0:
        error.append(q)
        error.append(q+1)

if len(error)==0:
    print("YES")

elif len(error)==2:
    if a==2:
        print("YES")
    elif error[0]==0:
        if sequence[error[0]+2]-sequence[error[0]]>0:
            print("YES")
        else:
            print("NO")
    elif error[1]==a-1:
        if sequence[error[1]]-sequence[error[1]-2]>0:
            print("YES")
        else:
            print("NO")
    else:
        if  sequence[error[1]]-sequence[error[1]-2]>0 and sequence[error[0]+2]-sequence[error[0]]>0:
            print("YES")
        else:
            print("NO")

elif len(error)==4:
    if error[1]==error[2]:
        if sequence[error[0]]>sequence[error[3]]:
            if error[0]==0 and error[3]==a-1:
                if sequence[error[0]]-sequence[error[3]-1]>0 and sequence[error[0]+1]-sequence[error[3]]>0:
                    print("YES")
                else:
                    print("NO")
            elif error[0]==0:
                if sequence[error[0]+3]-sequence[error[0]]>0:
                    print("YES")
                else:
                    print("NO")
            elif error[1]==a-1:
                if sequence[error[3]]-sequence[error[3]-3]>0:
                    print("YES")
                else:
                    print("NO")
            else:
                if  sequence[error[3]]-sequence[error[3]-3]>0 and sequence[error[0]+3]-sequence[error[0]]>0:
                    print("YES")
                else:
                    print("NO")
    elif sequence[error[0]]-sequence[error[2]]>0 and sequence[error[1]]-sequence[error[3]]>0:
        if error[0]==0 and error[3]==a-1:
            if sequence[error[0]]-sequence[error[3]-1]>0 and sequence[error[0]+1]-sequence[error[3]]>0:
                print("YES")
            else:
                print("NO")
        elif error[0]==0:
            if sequence[error[3]+1]-sequence[error[0]]>0:
                print("YES")
            else:
                print("NO")
        elif error[3]==a-1:
            if sequence[error[3]]-sequence[error[0]-1]>0:
                print("YES")
            else:
                print("NO")
        elif sequence[error[3]]-sequence[error[0]-1]>0 and sequence[error[3]+1]-sequence[error[0]]>0:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

else:
    print("NO")

Submission status: Accepted

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

count = 0
for i in range(n):
    if ps[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(1, n + 1):
    if p[i - 1] != i:
        cnt += 1

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

Submission status: Accepted

Solution code:
n = int(input())
xn = [int(x) for x in input().split()]
k = 0
for i in range(1,n+1):
	if xn[i-1] != i:k += 1
        
print("NO" if k > 2 else "YES")

Submission status: Accepted

Solution code:
n = int(input())
P = list(map(int, input().split()))
for i in range(n):
    for j in range(n):
        P_copy = P.copy()
        P_copy[j], P_copy[i] = P_copy[i], P_copy[j]
        ascending = True
        for k in range(n-1):
            if P_copy[k] > P_copy[k+1]:
                ascending = False
        if ascending:
            print('YES')
            exit()
print('NO')

Submission status: Accepted

Solution code:

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

index_not_equal_value_counter = 0
yes_or_no_flag = True
for i, v in enumerate(p):
    i = i + 1
    if i != v:
        index_not_equal_value_counter += 1
    if index_not_equal_value_counter > 2:
        yes_or_no_flag = False

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
cnt = 0
for ii in range(len(P)):
    if P[ii] != ii + 1:
        cnt += 1

f = cnt > 2
print ("YNEOS"[f::2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import glob


# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = 'ABC\\135\\B'

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'


class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class B(Common):

    def resolve(self):

        N = int(self.input_data())
        seq = [int(i) for i in self.input_data().split()]

        diff = []
        result = "NO"

        for i in range(N):
            if seq[i] != i+1:
                if len(diff) < 2:
                    diff.append([i+1, seq[i]])
                else:
                    break
        else:
            if len(diff) == 0:
                result = "YES"
            if len(diff) == 2:
                if diff[0][0] == diff[1][1] and diff[0][1] == diff[0][1]:
                    result = "YES"

        print(result)


solver = B(REL_PATH)
solver.exec_resolve()

Submission status: Accepted

Solution code:
N = int(input())
ary = list(map(int, input().split()))
  
sorted_ary = sorted(ary)
d = [a - s for a, s in zip(ary, sorted_ary)]
n = sum([1 for x in d if x != 0])
if n <= 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( 1 ,N+1 ):
    if p[i-1] != i:
        ans += 1

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

Submission status: Accepted

Solution code:
import sys

n = input()
data = list(map(int,sys.stdin.readline().split()))
#data.pop(0)

datacp = sorted(data)

a = list(map(lambda x: x[0]-x[1] ,zip(data,datacp)))

a = [i for i in a if i != 0 ]
if len(a) <= 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(1, n+1):
    if p[i-1] != i:
        ans += 1

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a_sort = sorted(a)
#print(N, a, a_sort)
x = 0
p = 'YES'
for i in range(N):
  if a[i] != a_sort[i]:
    x += 1
    if x > 2:
      p = 'NO'
      break

print(p)

Submission status: Accepted

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

count=0
i=0
while True:
  if p[i]!=i+1:
    count+=1
  if i>=N-1:
    break
  i+=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()))
k=0
count=0
while True:
    if k==n:
        break
    elif p[k]==k+1:
        k+=1
    else:
        count+=1
        k+=1
if count==0 or count==2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import math

# inputList=[]
# for i in range(6):
#     inputNum = input()
#     inputList.append(inputNum)
inputa = input().split()
inputb = input().split()

a = int(inputa[0])
# b = int(inputa[1])
# c = int(inputa[2])

# x = int(inputb[0])
# y = int(inputb[1])

# inputa = list(inputa[0])
inputbList = []
inputbList = list(inputb)

# inputaint = [int(n) for n in inputa]
inputbint = []
inputbint = [int(n) for n in inputbList]

# inputa.sort()
# inputb.sort()

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

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

Submission status: Accepted

Solution code:
def B_0or1Swap():
    N = int(input())
    p = list(map(int, input().split()))
    p_sort = sorted(p)
    num = 0
    ans = "NO"
    for x in range(N):
        if p[x] != p_sort[x]:
            num += 1
    if (0 == num) or (2 == num):
        ans = "YES"
    print(ans)
    
if __name__ == "__main__":
    B_0or1Swap()

Submission status: Accepted

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

q_list = list(range(1, n + 1))
unmatch_p = []
for p, q in zip(p_list, q_list):
    if p == q:
        pass
    else:
        unmatch_p.append(p)
        
if len(unmatch_p) == 2:
    print("YES")
elif len(unmatch_p) == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

P=sorted(p)
flag=0

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

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

Submission status: Accepted

Solution code:
n,*x=map(int,open(0).read().split())
soreted_x=sorted(x)
cnt=0

for a,b in zip(x,soreted_x):
  if a != b:
    cnt+=1

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

Submission status: Accepted

Solution code:
n=int(input())
line= list(map(int,input().split()))
cnt=0
for x in range(n):
    if line[x]!=x+1:
        cnt+=1
if cnt==0 or cnt==2: #cnt==0 or 2だと　２４３５１でYES（ｃnｔ＝４）になる
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

p_hikaku = []
for j in range(1,N+1):
    p_hikaku.append(j)

count = 0
for j in range(N):
    if p[j] !=  p_hikaku[j]:
        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()))
t = [i for i in range(1,n+1)]
count=0
for i in range(n):
    if p[i]!=t[i]:
        count+=1
    if count>=3:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter
import math
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n = ni()
ppp = na()
ln = len(ppp)
ct = 0
for i in range(ln):
    if ppp[i] != i+1:
        ct += 1
if ct == 0 or ct == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = input()
lst1 = list(map(int, input().split()))
lst2 = sorted(lst1)
 
n = 0
for x, y in zip(lst1, lst2):
    if x != y:
        n += 1
        if n > 2:
            break
 
if n > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
n = input()
a = list(map(int, input().split()))
print("YES") if sum(x != i for i, x in enumerate(a, 1)) <= 2 else print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
input()
p = [int(x) for x in input().split()]
    
p_sorted = sorted(p)
    
diff = 0
for p_elem, p_sorted_elem in zip(p, p_sorted):
    if p_elem != p_sorted_elem:
        diff += 1
    
if diff == 0 or diff == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

sort_p=sorted(p)
cnt=0

#i,jにpとソートしたpを代入して比較
for i,j in zip(p,sort_p):
  if i != j:
    cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
p = list(map(int, input().split()))
pp = sorted(p)
ct = 0
for i in range(N):
    if p[i] != pp[i]:
        ct += 1
    if ct > 2:
        print('NO')
        sys.exit()

print('YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

ok = list(range(1,n+1,1)) 

# numpy配列に変換
npp = np.array(p)
npok = np.array(ok)

# 配列の差分を比較
if np.count_nonzero(npp!=npok) <=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
s = input().split()
b =[str(i+1) for i in range(len(s))]
count=0
for i in range(len(s)):
  if s[i]!=b[i]:
    count+=1
if count==0: print("YES")
elif count==2: print("YES")
else: print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def p_b():
    N = int(input())
    p_n = list(map(int, input().split()))

    p_sort = sorted(p_n)
    cnt = 0

    for a, b in zip(p_n, p_sort):
        if a != b:
            cnt += 1

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


if __name__ == '__main__':
    p_b()

Submission status: Accepted

Solution code:
N = int(input())
suuretsu_str = input().split()
suuretsu = [int(i) for i in suuretsu_str]
suuretsu_order = [i for i in range(1,N+1)]
count = 0
for i in range(N):
    if suuretsu[i] != suuretsu_order[i]:
        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()))
pp = sorted(p)
count = 0
for i in range(len(p)):
    if p[i] != pp[i]:
        count = count + 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
num = [int(i) for i in input().split()]
flag = 0
if num == sorted(num):
  flag = 1
else:  
  sorted_num = sorted(num)
  for i in range(n):
    if flag == 1:
      break
    for j in range(n-i-1):
      temp = num[i]
      num[i] = num[i+j+1]
      num[i+j+1] = temp
      if num == sorted_num:
        flag = 1
        break
      else:
        temp = num[i]
        num[i] = num[i+j+1]
        num[i+j+1] = temp
if flag == 1:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

s=sorted(p)
cnt=0

def jud(p,s,n,cnt):
    # print(p,s)
    for i in range(n):
        if s[i]!=p[i]:
            cnt+=1
        if cnt>2:
            return "NO"
    return "YES"

print(jud(p,s,n,cnt))

Submission status: Accepted

Solution code:
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n=getint()
a=getints()
b=list(sorted(a))

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


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())
Pp = input()

P = []
H = []
n=1

for i in range(N):
    P = int(Pp.split()[i])
    H.append(P-n)
    n+=1


R = H.count(0)

if N-R < 3:
    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
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()))
Q = sorted(P)
if sum(1 if P[i] != Q[i] else 0 for i in range(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 p[i]-1 != i:
        count+=1
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
from sys import stdin 

n = int(input())
p = [int(x) for x in stdin.readline().rstrip().split()]
c= 0
q = sorted(p)

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

if c>2:
	print("NO")

else:
	print("YES")

Submission status: Accepted

Solution code:
num = int(input())
num_list = input().split(' ')
num_list[0] = int(num_list[0])
for i in range(num):
    num_list[i] = int(num_list[i])
count = 0
for i in range(num):
    if num_list[i] != i+1:
        count = count +1
if count > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p_ls = [int(i) for i in input().split()]
gold_p_ls = sorted(p_ls)
_start = None
_end = None
for i, p in enumerate(p_ls):
    if i + 1 != p:
        _start = i
        break

for j, p in enumerate(p_ls[::-1]):
    if N - j != p:
        _end = N - 1 - j
        break

if _start is None:
    print("YES")
else:
    tmp = p_ls[_start]
    p_ls[_start] = p_ls[_end]
    p_ls[_end] = tmp
    if p_ls == gold_p_ls:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
arr=[int(x) for x in input().split(' ')]
arr2=sorted(arr)
for x in range(n):
	if x+1==arr[x]:
		continue
	else:
		t=arr[x]
		arr[x]=arr[t-1]
		arr[t-1]=t
		break
# print(arr,arr2)
if arr==arr2:
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int,input().split()))
p_sort = sorted(p)
p_len = len(p)
ans = [0]*p_len
for i in range(p_len):
  ans[i] = p[i]-p_sort[i]

if ans.count(0) >= p_len-2:
  print('YES')
else:
  print('NO')