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

Submission status: Accepted

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

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

count = 0
big_index = [] # 昇順できないデータを入れておく
for i in range(N):
    if pn[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()))

p_collect = sorted(p)

no = 0

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

if no == 1:
    print("NO")
elif no >= 3:
    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 p[i] != i+1:
        count += 1
 
if count > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

permulated = len([1 for k, p in enumerate(P, 1) if p != k])
print('YES' if permulated <= 2 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

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

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

x = 0
for i in range(n):
    if l[i] != list_up[i]:
        x += 1

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(s) for s in input().split()]
cnt = [v != i + 1 for i, v in enumerate(p)].count(True)
print("YES" if cnt in (0, 2) else "NO")

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n = i()
p = wi()
q = sorted(p)

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

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

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x) for x in input().split()]
s_l = sorted(l)
print("YES" if sum([1 if l[i]!=s_l[i] else 0 for i in range(n)]) <= 2 else "NO")

Submission status: Accepted

Solution code:
N, *P = list(map(int, open(0).read().split()))
count = sum(p != i + 1 for i, p in enumerate(P))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_,p=open(0);print('YNEOS'[sum(i!=int(p)for i,p in enumerate(p.split(),1))>2::2])

Submission status: Accepted

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

q = sorted(P)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import math

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

r = []

for i in range(1, n + 1):
	if i != p[i - 1]:
		r.append(p[i - 1])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
r=list(range(1,n+1))
s=0
m=0
if l==r:
  m=1
  print("YES")
else:
  while s<n:
    for i in range(n):
      r=list(range(1,n+1))
      d=r[s]
      r[s]=r[i]
      r[i]=d
      if l==r:
        print("YES")
        s=n
        m=1
        break
    s=s+1
if not m==1:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
ideal_list = [i for i in range(1, N+1)]
delta_list = [abs(a-b) for a, b in zip(p, ideal_list)]
if len([x for x in delta_list if x != 0]) in [0, 2]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(input())
pn = [ int(item) for item in input().split()]
ANS = list(range(1,N+1))

import copy

if pn == ANS:
  print('YES')
else:
  flag=False
  for i in range(len(pn)):
    pi = pn[i]
    for j in range(len(pn)):
      pj = pn[j]
      pnc = copy.deepcopy(pn)
      pnc[j] = pi
      pnc[i] = pj
      if pnc == ANS:
        print('YES')
        flag=True
        break
    if flag==True:
      break
  if flag == False:
    print('NO')

Submission status: Accepted

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

out_num = {}
for i,pi in zip(range(1,N+1),p):
  if i != pi:
    out_num[i] = pi

if len(out_num) in [0,2]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import queue

"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

"""

def isUp(A):
	p = 0
	for a in A:
		if a < p:
			return False
		p = a
	return True
N = int(input())
A = [int(i) for i in input().split()]
ans = False

if isUp(A):
	ans = True
if not ans:
	for i in range(N-1):
		for j in range(i+1,N):
			A[i] += A[j]
			A[j] = A[i]-A[j]
			A[i] = A[i]-A[j]
			if isUp(A):
				ans = True
				break
			A[i] += A[j]
			A[j] = A[i]-A[j]
			A[i] = A[i]-A[j]
if ans:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
from copy import deepcopy
from itertools import combinations


N = int(input())
ppp = list(map(int, input().split()))
ppp_sorted = sorted(ppp)

combis = list(combinations(ppp, 2))

if ppp == ppp_sorted:
    print('YES')
    exit()
if N == 2:
    print('YES')
    exit()

for combi in combis:
    i = ppp.index(combi[0])
    j = ppp.index(combi[1])
    ppp_copy = deepcopy(ppp)
    ppp_copy[i], ppp_copy[j] = ppp[j], ppp[i]
    if ppp_copy == ppp_sorted:
        print('YES')
        exit()

print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]

N = getInt()
p = getIntList()
n = 0
for i in range(N):
    if not (i+1==p[i]):
        n += 1
if(n<=2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
b = []
c = [] 

for i in range(len(a)):
    if a[i]!=i+1:
        count = count + 1
        b.append(i+1)
        c.append(a[i])
        
if count == 0:
    print('YES')
elif count == 2 and b[0] == c[1] and c[0] == b[1]:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

c=0
d=0
for i in range(n):
    if p[i]==i+1:
        continue
    else:
        c=c+1
        if c>=3:
            break
        if d==0:
            d=i+1
            continue
        if d!=0:
            if d!=p[i]:
                c=3
                break

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

Submission status: Accepted

Solution code:
L1 = input()
L2 = list(map(int,input().split()))
L3 = L2.copy()
L3.sort()

cnt = 0

for a ,b in zip(L2 , L3) :
  if a != b:
    cnt += 1

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

Submission status: Accepted

Solution code:
S = int(input())
slist = input().split()

for i,a in enumerate(slist):
  slist[i] = int(a)

new_list = sorted(slist)
d_count = 0
for i,j in zip(slist,new_list):
  if i != j:
    d_count += 1
    
if d_count <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = tuple(map(int,input().split()))
ans = sorted(p)
for i in range(n):
    for j in range(n):
        ori = list(p)
        temp = ori[i]
        ori[i] = ori[j]
        ori[j] = temp
        if ans == ori:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

Solution code:

def main():

    N = int(input())
    A= list(map(int,input().split()))
    against = list(range(1,N+1))
    count = 0

    for i in range(N-1):
        if A[i]!=against[i]:
            count +=1
    
    if count < 3:
        print("YES")
    else:
        print("NO")




if __name__== "__main__":
    main()

Submission status: Accepted

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

dif = []
dif_idx = []
for i in range(N):
    if p[i] != (i+1):
        dif.append(p[i])
        dif_idx.append(i)

flag_able = True
if len(dif) > 2:
    flag_able = False
elif len(dif) == 2:
    pass    

if flag_able is True:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=input().split()
#https://note.nkmk.me/python-list-str-num-conversion/
#せっかくだからnを使おう
a=0
for i in range(n):
  p[i]=int(p[i])
  if p[i]!=(i+1):
    a+=1
if a<=2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import copy

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

X.sort()
ans = "NO"


for i in range(N):
    for j in range(N):
        Y = copy.copy(P)
        a = copy.copy(P[i])
        b = copy.copy(P[j])
        Y[i] = b
        Y[j] = a
        #print(Y)
        if Y == X:
            ans =  "YES"


print(ans)

Submission status: Accepted

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

def main():
  n = int(input())
  input_line = list(map(int, input().split()))
  
  flag = 0
  i = 0
  buf = []
  for x in input_line:
    if len(buf) > 2:
      break
    elif (i + 1) != x:
      buf.append(i)
    i += 1
    
  if input_line == sorted(input_line):
    print("YES")
  elif len(buf) != 2:
    print("NO")
  else:
    first = buf[0]
    second = buf[1]
    s = input_line[first]
    input_line[first] = input_line[second]
    input_line[second] = s
    if input_line == sorted(input_line):
      print("YES")
    else:
      print("NO")


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n=int(input())
p=[int(_) for _ 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())
pList = list(map(int, input().split()))
sortedList = sorted(pList)
diffCount = 0

for i in range(N-1):
    if pList[i] != sortedList[i]:
        diffCount += 1

print(["NO","YES"][diffCount <= 2])

Submission status: Accepted

Solution code:
n = int(input())
t = input()
p = []
for k in range(0,n,1):
  p.append(int(t.split(' ')[k]))
c = 0
for k in range(0,n,1):
  if p[k] == k+1:
    c += 1
if c == n - 2 or c == n:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input());
list = list(map(int,input().split()))
diffcount = 0

for num in range(1,n+1):
    if list[num-1] != num:
        diffcount += 1
if diffcount == 2 or diffcount == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- encoding: utf-8 -*-

import sys

if __name__ == '__main__':
    inputN = int(input())
    inputList = list(map(int, input().split()))
    #
    flag = False
    before = 0
    for k in range(inputN):
        if before >= inputList[k]:
            flag = True
        before = inputList[k]
    
    #
    if not flag:
        sys.stdout.write("YES")
        sys.exit()
    
    #  
    for i in range(inputN):
        for j in range(inputN):
            if i != j:
                changeList = inputList[:]
                swap = changeList[i]
                changeList[i] = changeList[j]
                changeList[j] = swap
          
                #
                flag = False
                before = 0
                for k in range(inputN):
                    if before >= changeList[k]:
                        flag = True
                    before = changeList[k]
          
                #
                if not flag:
                    sys.stdout.write("YES")
                    sys.exit()


    sys.stdout.write("NO")

Submission status: Accepted

Solution code:
N = int(input())
lst = []
lst_2 = []
input_ = input().split()
for i in range(N):
	lst.append(int(input_[i]))
lst_s = sorted(lst)
for i in range(N):
  if lst[i] != lst_s[i]:
    lst_2.append(i)
if len(lst_2) <= 2:
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
num = int(input())
if num >= 2:
  input_list = [int(i) for i in input().split()] 
  sorted_list = sorted(input_list)

  count = 0
  for input, sort in zip(input_list, sorted_list):
    if input != sort:
      count += 1

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

Submission status: Accepted

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

import sys

sys.setrecursionlimit(10**7)

# Override `input` function because `stdin.readline()` is 10x faster than built-in `input()`
input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    n, = read_h()
    arr = read_h()

    bad = 0
    for i, p in enumerate(arr):
        if p != i + 1:
            bad += 1
        if bad > 2:
            print('NO')
            return

    print('YES')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np
import itertools
def main():
    input = sys.stdin.readline

    N = int(input())
    P = list(map(int, input().split()))
    #print(P)
    P = np.array(P)
    #print(P)
    ans = np.arange(start=1, step=1, stop=N+1)
    #print(ans)
    if np.all(P == ans):
        print('YES')
    else:
        dels_comb = list(itertools.combinations(P,2))
        #print(dels_comb)

        length = len(dels_comb)
        for i in range(length):
            dels = dels_comb [i]
            a, b = dels

            _P = P[(P != a) & (P != b)]
            _ans = ans[(ans != a) & (ans != b)]
            
            #print(_P)
            #print(_ans)
            if np.all(_P == _ans):
                if np.where(P == a)[0] == np.where(ans == b)[0]:
                    if np.where(P == b)[0] == np.where(ans == a)[0]:
                        print('YES')
                        break
            if i + 1 == length:
                print('NO')
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
num_list = input().split()
A = [int(s) for s in num_list]
A.sort()
B = []
for i in range(N):
    if A[i] != int(num_list[i]):
        B.append(1)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def swap(a, b):
    tmp = a
    a = b
    b = tmp;
    return a, b

n = int(input())
p = list(map(int, input().split()))
g = p.copy()
q = sorted(p)
flag = 0
for i in range(n):
    for j in range(n):
        p[i], p[j] = swap(p[i], p[j])
        if (p == q):
            flag = 1
        p = g.copy()
if flag == 1:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import copy

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

sortedps = copy.deepcopy(ps)
sortedps.sort()

count = 0
for i in range(0,n):
    if count > 2:
        print('NO')
        exit()
    if ps[i] != sortedps[i]:
        count = count +1
if count > 2:
    print('NO')
    exit()

print('YES')

Submission status: Accepted

Solution code:
import numpy as np
def acinput():
    return list(map(int,input().split(" ")))


N=int(input())
p=np.array(acinput())

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:
# coding: utf-8

n = int(input())

l = [int(i)+1 for i in range(n)]
check = list(map(int,input().split()))

#print(l)
index = 0
flag = 0
for i in range(n):
    #print("check{0},今{1}".format(check[i],i))
    if check[i] != i+1:
        flag +=1
        if flag == 2:
            check[index] = check[i]
            check[i] = tmp
            #print("index{}={}".format(check[index],i+1))
            #print("index{}={}".format(check[i],index+1))
            break
        #違う部分の値を保存
        index = i
        tmp = check[index]
        #print(tmp)
        
#print(l)
#print(check)

print("YES") if l == check else print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = map(int, list((input().split())))
count =0
for s,t in enumerate(p):
	if s !=(t-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()))
A=p.copy()
A.sort()
if p==A:
  print("YES")
else:
  flag=0
  for i in range(N):
    for j in range(i):
      p[i],p[j]=p[j],p[i]
      if p==A:
        flag=1
        break
      else:
        p[i],p[j]=p[j],p[i]
    if flag==1:
      break
  if flag==0:
    print("NO")
  else:
    print("YES")

Submission status: Accepted

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

p_sorted = sorted(p)

cnt = 0
for i, j in zip(p,p_sorted):
    if i != j:
        cnt += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split())) #i_1 i_2を取得し、iに値を入れる

pure_arr = []
for i in range(n):
    pure_arr.append(i+1)
count=0
for (num, value) in zip(pure_arr, arr):
    if num!=value:
        count += 1

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

Submission status: Accepted

Solution code:
x = int(input())
a_list = list(map(int,input().split()))
a = sorted(a_list)
count = 0
for i in range(x):
    if a[i] == a_list[i]:
      count += 1
if count >= x-2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
N = int(input())
plist = [int(i) for i in list(input().split())]
c=1
check=0
for i in plist:
    if i!=c:
        check+=1
    c+=1

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

Submission status: Accepted

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


down_i = []
down_count = 0
for i in range(n-1):
    if p[i] > p[i+1]:
        down_i.append([i,p[i],p[i+1]])
        down_count += 1


if down_count == 0:
    print('YES')
else:
    if down_count == 1:
        p[down_i[0][0]] = p[down_i[0][0]+1]
        p[down_i[0][0]+1] = down_i[0][1]

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

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

    elif down_count == 2:
        p[down_i[0][0]] = down_i[1][2]
        p[down_i[1][0]+1] = down_i[0][1]

        #print(p)

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import itertools

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

    compArr = list(range(1, N+1))
    arr = list(range(N))
    combs = list(itertools.combinations(arr, 2))

    ans = 'NO' 
    if p == compArr:
        ans = 'YES'
    for c in combs:
        tmpArr = compArr.copy()
        tmpArr[c[0]], tmpArr[c[1]] = tmpArr[c[1]], tmpArr[c[0]]
        if p == tmpArr:
            ans = 'YES'

    return ans

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n = int(input())
p_lis = list(map(int, input().split()))
org = list(range(1, n+1, 1))

c = 0
for i in range(n):
      if p_lis[i]== org[i]:
            pass
      else:
            c += 1
if c >=3:
      print("NO")
else:
      print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p = [0] + list(map(int, input().split())) + [0]
ans = 'YES'
d = {}
for i in range(n):
    if p[i] != i:
        if p[p[i]] == i:
            d[tuple(sorted([i, p[i]]))] = 0
            continue
        else:
            ans = 'NO'
            break
if len(d) > 1:
    ans = 'NO'
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline())
p = [int(i) for i in sys.stdin.readline().split()]

if p == sorted(p):
    print('YES')
elif len(p) == 2:
    print('YES')
else:
    swap_index = []
    for i, v in enumerate(p[:-1]):
        if v > p[i + 1]:
            if len(swap_index) == 0:
                swap_index.append(i)
            else:
                swap_index.append(i + 1)
                break
    if len(swap_index) == 1:
        print('NO')
    else:
        p[swap_index[0]], p[swap_index[1]] = p[swap_index[1]], p[swap_index[0]]
        if p == sorted(p):
            print('YES')
        else:
            print('NO')

Submission status: Accepted

Solution code:
import copy
N = int(input())
li = list(map(int,input().split()))
ans = copy.copy(li)
ans.sort()

count = 0
judge = True

for i in range(N-1):
    if (li[i] != ans[i]):
        count = count + 1
        if(count > 2):
            judge = False
            break

if(judge):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

flag = 0
for i,c in enumerate(l):
  if i+1 != c:
    flag += 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
p_n_str = input().split()
p_n_int = [int(p) for p  in p_n_str]
flag = 0

for k in range(0, n):

    if p_n_int[k] != k+1:
        
        for i in range(1, n):
            if k+1 == p_n_int[i]:
                flag += 1
                smp = p_n_int[k]
                p_n_int[k] = p_n_int[i]
                p_n_int[i] = smp
                break
        
if flag <= 1:        
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
a=int(input())
b = list(map(int, input().split()))
c=sorted(b)
d=0

for i in range(a):
    if b[i] != c[i]:
        d += 1

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

Submission status: Accepted

Solution code:
#a,b = map(int,input().sprit())
n = int(input())
p = list(map(int,input().split()))
q = sorted(p)
j = 0 # index
count = 0 # swap count
ans = 0
for i in range(len(p)):
    if p[i] != q[i]:
        if count:
            ans = 1
            break
        elif j:
            if p[i] == q[j]:
                count += 1
            else:
                ans = 1
                break
        else:
            j = i
if ans == 1:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

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

if count == 0:
    print('YES')
elif count == 2:
    if (p[index[0]] == index[1] + 1) & (p[index[1]] == index[0] + 1):
        print('YES')
    else:
        print('NO')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
bara=list(map(int,input().split()))
number=list(range(1,n+1))

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

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

Submission status: Accepted

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

ans = 'YES'
judge = False
for i in range(n):
    if (p[i] != i + 1 and judge == False):
        if(p[p[i] - 1] == i + 1):
            judge = True
            a = p[i] - 1
            p[i] = i + 1
            p[a] = a + 1
        else:
            ans = 'NO'
            break
    elif (p[i] != i + 1 and judge == True):
        ans = 'NO'
        break
print(ans)

Submission status: Accepted

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

N = input()

n = int(N)

list1 = list(range(n))

for i in range(n) :
	list1[i] = list1[i] + 1

#print(list1)

list2 = []

list2 = input().split()  

list2 = [int(n) for n in list2]

#print(list2)

judge = 0

for i in range(n) : 
	if list1[i] != list2[i] :
		judge = judge + 1

if judge <= 2 :
	print('YES')

else :
	print('NO')

Submission status: Accepted

Solution code:
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    A = list(map(int, lines.pop(0).split(' ')))
    c = 0
    for i, x in enumerate(A):
        if ( i + 1 != x ):
            c += 1

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

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

N = int(input())
a = list(map(int,input().split()))
ans = "NO"
b = copy.deepcopy(a)
if b == sorted(a):
    ans = "YES"
else:
    for i in range(0, N-1):
        if ans == "YES":
            break
        for j in range(i, N):
            b = copy.deepcopy(a)
            temp = b[i]
            b[i] = b[j]
            b[j] = temp
            if b == sorted(a):
                ans = "YES"
                break
print(ans)

Submission status: Accepted

Solution code:
import copy
n=int(input())
p=[]
ps=[]
cnt=0
p = list(map(int, input().split()))
pn=copy.deepcopy(p)
ps=p
ps.sort()
for i in range(n):
    if ps[i]!=pn[i]:
        cnt=cnt+1
        if cnt==3:
            break
if cnt==0 or cnt==1 or cnt==2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import collections
n = int(input())
p = [int(i) for i in input().split()]
ps = sorted(p)
x = [p[i]-ps[i] for i in range(len(p))]
c = collections.Counter(x)
if len(c) == 1:
  print('YES')
elif len(c) == 3 and x.count(0) == n-2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = []

if a != b:
    for i in range(1, n+1):
        if a[i-1] != i:
            c.append(i-1)

        if len(c) == 2:
            break

    if len(c) == 2:
        ch = a[c[0]]
        a[c[0]] = a[c[1]]
        a[c[1]] = ch

    if a == b:
        print('YES')
    else:
        print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
nl = [i for i in range(1, n+1)]
pi = -1
pii = 999
pj = -1
pji = 999
for i in range(n):
    if pi == -1:
        if p[i] != nl[i]:
            pi = p[i]
            pii = i
    else:
        if p[i] != nl[i]:
            pj = p[i]
            pji = i
            break

if pii != 999 and pji != 999:
    p[pji] = pi
    p[pii] = pj

ans = True
for i in range(n):
    if nl[i] != p[i]:
        ans = False
        break

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