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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("YES") if fl else print("NO")
import collections
import math
import itertools
import heapq as hq
n = ni()
P = lma()
cnt = 0
for i in range(n):
    if P[i] != i+1:
        cnt+=1
f = False
if cnt==2 or cnt==0:
    f=True
yn(f)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
n_list = np.array(list(map(int,input().split())))
syoujun_list =np.array(range(1,n+1))
different = n_list - syoujun_list
if np.count_nonzero(different) ==2 or np.count_nonzero(different) ==0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

k=0
for i in range(N):
    if i+1!=p[i]:
        if k>2:
            break
        k+=1

print('YNEOS'[k>2::2])

Submission status: Accepted

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

k=0
for x,y in zip(p,sorted(p)):
    if x!=y:
        if k>2:
            break
        k+=1

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

Submission status: Accepted

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

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

ok = cnt == 2 or cnt == 0

print("YES" if ok else "NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

input()
ary = list(map(int, input().split()))
copy = copy.copy(ary)
ary.sort()
ans = 0

for a, c in zip(ary, copy):
  if a != c:
    ans += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Q = [i+1 for i in range(N)]
for i in range(N):
    for j in range(i,N):
        P[i], P[j] = P[j], P[i]

        if P == Q:
            print("YES")
            exit()

        P[i], P[j] = P[j], P[i]

print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(s) for s in input().split()]
sortedP = sorted(p)
sortedP.sort(key=int)
diffcount = 0
for i in range(n):
    if p[i] != sortedP[i]:
        diffcount += 1
if diffcount == 0 or diffcount == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

c = 0

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

if c <= 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

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

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

Submission status: Accepted

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

    for i in range(n):

        if p[i] == i+1:
            count += 1

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p_ls = list(map(int, input().split(' ')))
cnt = 0

for i in range(len(p_ls) - 1):
    min_idx = i
    for j in range(i + 1, len(p_ls)):
        if p_ls[min_idx] > p_ls[j]:
            min_idx = j
    if min_idx != i:
        p_ls[min_idx], p_ls[i] = p_ls[i], p_ls[min_idx]
        cnt += 1
if cnt > 1:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
p0 = 0
p1 = P[0]
temp = []
for i, p in enumerate(P[1:]):
    if p >= p1:
        p0 = p1
        p1 = p
        continue
    else:
        temp.append(i)
        p1 = p

if len(temp) == 2:
    P[temp[0]], P[temp[1]+1] = P[temp[1]+1], P[temp[0]]

    p0 = 0
    p1 = P[0]
    temp = []
    for i, p in enumerate(P[1:]):
        if p >= p1:
            p0 = p1
            p1 = p
            continue
        else:
            temp.append(i)
            p1 = p
    if len(temp) == 0:
        print("YES")
    else:
        print("NO")
elif len(temp)==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

print("YES") if cnt in [0, 2] else print("NO")

Submission status: Accepted

Solution code:
N, *P = map(int, open(0).read().split())

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

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

Submission status: Accepted

Solution code:
n = int(input())
P = list(map(int, input().split()))
sorted_P = sorted(P)

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

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

Submission status: Accepted

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

ps = sorted(p)

count = 0
for q, qs in zip(p, ps):
  if q != qs:
    count += 1
    
print('YES' if count <= 2 else 'NO')

Submission status: Accepted

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

ps = sorted(p)

d_p = []
d_ps = []
for i,q in enumerate(p):
  if q != ps[i]:
    d_p.append(q)
    d_ps.append(ps[i])
if len(d_p) > 2:
  print('NO')
elif len(d_p) == 2:
  d_p.sort()
  if d_p == d_ps:
    print('YES')
  else:
    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]!=sorted(p)[i]:
        count += 1
print("YES" if count/2<=1 else"NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
A = []
y = 0

for n in range(1,N+1):
  A.append(n)

for p,a in zip(P,A):
  if p!=a:
    y+=1
    

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    p = [int(i) for i in input().split()]
    for i in range(n):
        for j in range(i, n):
            p[i], p[j] = p[j], p[i]
            if is_sorted(p):
                print("YES")
                return
            p[i], p[j] = p[j], p[i]
    print("NO")


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
xlist = list(map(int, input().split()))
ans = "NO"
#降順になっていない部分の数をカウント
minus_gaps = np.count_nonzero(np.diff(xlist)<0)
#全て降順になっていれば、それでOK
if minus_gaps == 0:
    ans = "YES"
#降順でない部分が2つの場合は、その2つを入れ替えて、全て降順になるか確認
elif minus_gaps == 2:
    tmpList = np.where(np.diff(xlist)<0)
    xlist[tmpList[0][0]],xlist[tmpList[0][1]+1] = xlist[tmpList[0][1]+1], xlist[tmpList[0][0]]
    if np.count_nonzero(np.diff(xlist)<0) == 0:
        ans = "YES"
#あとは全部NO
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("NO")
elif cnt <= 2:
    print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

S = sorted(s)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    P = LI()

    cnt = 0
    for i, p in enumerate(P):
        if (i+1) != p:
            cnt += 1
    if cnt <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
MM = input().split()
count = 0
list1 =[]
for i in MM:
  a = int(i)
  list1.append(a)
list2 = sorted(list1)
for i,j in zip(list1,list2):
  
  if i != j:
    
    count +=1
if count >2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))


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

q = sorted(p)

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

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 135
# B - 0 or 1 Swap

'''
解説見て書き直し、
数列の要素と順番(i+1)が一致していない個数を数える。
これが3以上であれば"NO"
3未満(0か2)であれば"YES"
'''


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

count = 0

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

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

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 135
# B - 0 or 1 Swap

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

S={}
s=N+1

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


if len (S)==0:
    print('YES')
    exit()
elif len(S)>2:
    print('NO')

else:
    n=S[s]
    if S[n]==s:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque, defaultdict
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations,product#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
from functools import reduce,lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("## ",*x,file=sys.stderr)
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): #xは64bit整数
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    #w.sort(key=itemgetter(1),reverse=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = list(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列

    def hantei(A):
        for i in range(1,len(A)):
            if not A[i-1]<A[i]:
                return False
        return True
    
    if hantei(A):
        print('YES')
        return
    x=list(range(N))
    for i,j in combinations(x,2):
        cA=A.copy()
        cA[i],cA[j]=cA[j],cA[i]
        if hantei(cA):
            print('YES')
            return
    print('NO')



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
q = 0
for i in range(n):
 for j in range(i, n):
  t = a[i]
  a[i] = a[j]
  a[j] = t
  f = 1  
  lst = - 1100
  for k in a:
   f &= k > lst
   lst = k
  q |= f
  t = a[i]
  a[i] = a[j]
  a[j] = t
print("YES" if q else "NO")

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n=int(input())
p=list(map(int,input().split()))
q=sorted(p)
for i in range(n-1):
  for j in range(i,n):
    s=p[i]
    p[i]=p[j]
    p[j]=s
    if p==q:
      print("YES")
      sys.exit()
    s=p[i]
    p[i]=p[j]
    p[j]=s
      
print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
P=list(map(int,input().split()))
Q=sorted(P)
count=0
for i in range(N):
    if P[i]!=Q[i]:
        count=count+1
    else:
        pass
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()))
P = sorted(p)

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

Submission status: Accepted

Solution code:
from copy import copy
n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
ret = 'NO'
for i in range(n-1):
    for j in range(i, n):
        r = copy(p)
        r[i] = p[j]
        r[j] = p[i]
        if q == r:
            ret = 'YES'
            break
print(ret)

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

miss_cnt = 0

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

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

Submission status: Accepted

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

swap = 0

for i in range(n):
    min_num = p[i]
    min_j = i
    for j in range(i, n):
        if min_num > p[j]:
            min_num = p[j]
            min_j = j
    if min_j != i:
        p[i] ,p[min_j] = p[min_j], p[i] 
        swap += 1
    # print(swap)
    # print(p)
if swap <= 1:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

wrong = 0
for i in range(1, n + 1):
    if i == p[i - 1]:
        pass
    else:
        wrong += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input());a=list(map(int,input().split()))
print('NYOE S'[sum([i==j for i,j in zip(a,sorted(a))])>=n-2::2])

Submission status: Accepted

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

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

if c <= 2:
    print('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 1+i!=p[i]:
    cnt+=1
if cnt==0 or cnt==2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
#１３５．
n=int(input())
p=list( map(int, input().split()))
ans=0
for i in range(n):
    if p[i]!=i+1:
        ans=ans+1
if ans<=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

temp = 0

for i in range(n):
    if pl[i] != i+1:
        temp += 1

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

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

Solution code:
# abc135_b.py
# https://atcoder.jp/contests/abc135/tasks/abc135_b

# B - 0 or 1 Swap /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 200点

# 問題文
# {1, 2, ..., N} を並び替えた数列 p = {p1, p2, ..., pN} があります。
# あなたは一度だけ、整数  i, j (1≤i<j≤N) を選んで  pi  と  pj を入れ替える操作を行うことができます。
# 操作を行わないことも可能です。
# pを昇順にすることができるなら YES を、できないならば NO を出力してください。

# 制約
#     入力は全て整数である。
#     2≤N≤50
#     pは {1, 2, ..., N} を並び替えた数列である。

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N
# p1 p2 ... pN

# 出力
# pを昇順にすることができるなら YES を、できないならば NO を出力せよ。

# 入力例 1
# 5
# 5 2 3 4 1

# 出力例 1
# YES

# p1と p5 を入れ替えることで pを昇順にできます。

# 入力例 2
# 5
# 2 4 3 5 1

# 出力例 2
# NO

# この場合、どのような操作を行っても pを昇順にすることはできません。

# 入力例 3
# 7
# 1 2 3 4 5 6 7

# 出力例 3
# YES

# pが最初から昇順なので、操作を行う必要はありません。


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    N = int(lines[0])
    # X, A = list(map(int, lines[0].split()))
    values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(6):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(M):
    #     valueses.append(list(map(int, lines[i+1].split())))

    values_c = values.copy()
    values_c.sort()

    cnt = 0

    for n in range(N):
        if values[n] != values_c[n]:
            cnt += 1
    
    if cnt <= 2:
        result = 'YES'
    else:
        result = 'NO'

    return [result]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['5', '5 2 3 4 1']
        lines_export = ['YES']
    if pattern == 2:
        lines_input = ['5', '2 4 3 5 1']
        lines_export = ['NO']
    if pattern == 3:
        lines_input = ['7', '1 2 3 4 5 6 7']
        lines_export = ['YES']
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
def main():
  n=int(input())
  p = list(map(int,input().split()))
  first=-1
  last=-1
  
  for i in range(0,n-1):
    if p[i]>p[i+1]:
      first=i
      break
  for i in reversed(range(1,n)):
    if p[i]<p[i-1]:
      last=i
      break
  
  if first!=-1 and last!=-1:
    tmp=p[first]
    p[first]=p[last]
    p[last]=tmp

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 2
for i in range(1,N+1):
    cnt -= 1 if p_list[i-1] != i else 0

if cnt < 0:
    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:
        pass
    else:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
p1=list(map(int,input().split()))
p2=sorted(p1)
count=0
for i in range(N):
  if p1[i]!=p2[i]:count+=1

if count in [0,2]:print('YES')
else:print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
sp = sorted(p)
print('YES' if sum(p[i] == sp[i] for i in range(n)) >= n - 2 else 'NO')

Submission status: Accepted

Solution code:
N = int(input())
ppp = list(map(int, input().split()))
cnt = 0
for i, p in enumerate(ppp, 1):
    if i != p:
        cnt += 1
print('YES' if cnt < 3 else '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

sort_p = sorted(p)

for i in range(n):
  if p[i] != sort_p[i]:
    count += 1
    
if count  == 2:
  print("YES")
  exit()
elif count == 0:
  print("YES")
  exit()
else:
  print("NO")
  exit()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    p = list(map(int, input().split()))
    c = {}
    f = []
    for i in range(N):
        if p[i] != i+1:
            c[p[i]] = i+1
            f.append(p[i])
    if len(c) > 2:
        print('NO')
    elif len(f) == 0 or (c[f[0]] == f[1] and c[f[1]] == f[0]):
        print('YES')
    else:
        print('NO')

main()

Submission status: Accepted

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

temp = 0

for i in range(n):
    if pl[i] != i+1:
        temp += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math

def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    P = list(map(int,input().split()))
    PS = sorted(P)
    count =0
    for i in range(N):
        if P[i]!=PS[i]:
            count +=1

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



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
N = int(input())
array = list(map(int,input().split()))
array_base = [ I for I in range(1,N+1) ]

dic = {}
for I in range(N):
    if array[I] != I + 1:
        dic[I] = array[I]

if not (len(dic) == 2 or len(dic) == 0):
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
input()
*p,=map(int,input().split())
print('YNEOS'[2<sum(i!=j for i,j in zip(p,sorted(p)))::2])

Submission status: Accepted

Solution code:
input()
*p,=map(int,input().split())
print('YNEOS'[2<sum(1 for i,j in zip(p,sorted(p)) if i!=j)::2])

Submission status: Accepted

Solution code:
input()
*p,=map(int,input().split())
print('YNEOS'[2<sum([1 for i,j in zip(p,sorted(p)) if i!=j])::2])