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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy
N = int(input())
p = list(map(int, input().split()))
pcopy = copy.deepcopy(p)
ans = sorted(p)

is_ascend = False
if p == ans:
    is_ascend = True

for i in range(N):
    for j in range(i+1,N):
        p = copy.copy(pcopy)
        p[i], p[j] = p[j], p[i]
        if p == ans:
            is_ascend = True

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

Submission status: Accepted

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

s_p = sorted(p)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import collections
import itertools
import copy

def YesNo(Bool):
    if(Bool):
        print("YES")
    else:
        print("NO")
    return

def resolve():

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

    tmp=copy.deepcopy(p)
    isOK=True
    for k in range(N-1):
        if(tmp[k]>=tmp[k+1]):
            isOK=False
    if(isOK):
        ans=True

    for i in range(N):
        for j in range(i+1,N):
            tmp=[]
            tmp=copy.deepcopy(p)
            tmp[i],tmp[j]=tmp[j],tmp[i]
            isOK=True
            for k in range(N-1):
                if(tmp[k]>=tmp[k+1]):
                    isOK=False
            if(isOK):
                ans=True
    YesNo(ans)

resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,input().split()))
t = s.copy()
t.sort()
cnt = 0
for i in range(n):
    if s[i] != t[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()))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

q = p.copy()
q.sort()

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

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

Submission status: Accepted

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

not_equal = 0
for p, q in zip(nums, sorted_nums):
    if p != q:
        not_equal += 1

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

Submission status: Accepted

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

sor_N = sorted(p)

cn = 0

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


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

Submission status: Accepted

Solution code:
N = int(input())
P = [int(s) for s in input().split()]
index = []
for i in range(N):
    index.append(P.index(i+1))
count = 0
for i, j in enumerate(index):
    if i != j:
        count += 1
if count > 2:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
def inpl(): return list(map(int, input().split()))

N = int(input())
P = inpl()
Q = sorted(P)

for i in range(N):
    for j in range(i, N):
        R = [p for p in P]
        R[i], R[j] = R[j], R[i]
        for k in range(N):
            if R[k] != Q[k]:
                break
        else:
            print("YES")
            exit()
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
ans = False
for i in range(n):
    for j in range(i, n):
        p[i], p[j] = p[j], p[i]
        if all(p[k] < p[k + 1] for k in range(n - 1)):
            ans = True
        p[i], p[j] = p[j], p[i]
print(["NO", "YES"][ans])

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    P = [int(p) for p in input().split()]
    diff = 0
    for i in range(N):
        if P[i] != i + 1: diff += 1
    if diff <= 2: print("YES")
    else: print("NO")

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
#answer = [int(x) for x in range(1, N+1)]
count = 0
for i in range(1, N+1):
  if P[i-1] != 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 i+1!=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()))
cnt = 0
for i, a in enumerate(p):
    cnt += (i + 1) != a

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

Submission status: Accepted

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

def swap(a, b):
    return b, a

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = swap(a[i], a[j])
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = swap(a[i], a[j])
print('YES' if ok else 'NO')

Submission status: Accepted

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

ok = 0
f = 1
for i in range(n):
    if a[i] - 1 != i:
        f = 0
if f:
    ok = 1
for i in range(n):
    for j in range(i + 1, n):
        a[i], a[j] = a[j], a[i]
        f = 1
        for k in range(n):
            if a[k] - 1 != k:
                f = 0
        if f:
            ok = 1
        a[i], a[j] = a[j], a[i]
print('YES' if ok else 'NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
C = list(i for i in range(1,n+1))
L = list(map(int,input().split()))
cnt = 0
for c,l in zip(C,L):
  if c != l:
    cnt += 1
print('YES' if cnt == 0 or cnt == 2 else 'NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

count = 0
for x in range(n):
  if p[x] != ideal[x]:
    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()))

# p_i != i なる要素p_iの数をkとするとき、
# k<=2 であるときに限り1回以下のスワップでpを昇順にできる
# k=0 のとき、最初からpは昇順
# k=1 にはならない
# k=2 のとき p_i != i である2つの要素を1回スワップすることでpを昇順にできる

q = list(range(1, N+1))

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

Submission status: Accepted

Solution code:
def check(n, list1): 
    new = [] 
    for i in range(n): 
        new.append(list1[i]) 
          
    new.sort()
    swaps = 0
    for i in range(n): 
        if list1[i] != new[i]: 
            swaps += 1
              
    if swaps == 0 or swaps == 2: 
        return True
    else: 
        return False
  
n = int(input())

numbers =list(map(int, input().split()[:n]))



if check(n, numbers): 
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
x = int(input())
p = list(map(int, input().strip().split()))
k = sorted(p)
count = 0
for i in range(0,x):
    if p[i] == k[i]:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
List=list(input().split())
List=list(map(int,List))
List_sorted=sorted(List)

i=0
while i<len(List)-1:
    j=i+1
    ListTest=list(List)
    count=0
    while j<len(List):
        if ListTest==List_sorted:
            print('YES')
            quit()
        ListTest[i],ListTest[j]=ListTest[j],ListTest[i]
        if ListTest==List_sorted:
            print('YES')
            quit()
        else:
            ListTest=list(List)
        j+=1
    i+=1
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("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
import numpy as np
 
N = int(input())
p = list(map(int, input().split()))
 
q = sorted(p)
p, q = np.array(p), np.array(q)

if np.sum(p != q) <= 2: print('YES')
else: print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

print('YES')

Submission status: Accepted

Solution code:
# import math
# import statistics
a=int(input())
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
#e1,e2 = map(int,input().split())
f = list(map(int,input().split()))
#g = [input() for _ in range(a)]

count=0
for i in range(a-1):
    if f[i]!=f[i+1]-1:
        f[i+1]=i+2
        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()))
new_p=sorted(p)
cnt=0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
list_p = [int(i) for i in input().split()]
count = 0
for i in range(0, n):
    if i + 1 != list_p[i]: count += 1
    if count > 2:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

Solution code:
n = int(input())
plist = list(map(int, input().split()))
nlist = sorted(plist)

ng_cnt = 0
for p, n in zip(plist, nlist):
    if p != n:
        ng_cnt += 1
if ng_cnt >2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
R = sorted(P)
import copy
answer = 'NO'
for i in range(0,N-1):
    for j in range(i+1,N):
        Q = copy.deepcopy(P)
        A = Q[i]
        B = Q[j]
        Q[i] = B
        Q[j] = A
        if Q == R:
            answer = 'YES'
            break
if P == R:
    answer = 'YES'
print(answer)

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

Submission status: Accepted

Solution code:
# ABC135

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
print("YES" if [i == j for i, j in zip(a, sorted(a))].count(False) <= 2 else "NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,*P=map(int,open(0).read().split())
S='YES' if sum([P[i]==i+1 for i in range(N)])>=N-2 else 'NO'
print(S)

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==0 or cnt==2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    p = LI()

    cnt = len([j for i, j in zip(p, sorted(p)) if i != j])
    if cnt <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N=int(input())
ans=0
d=[int(s) for s in input().split()]
for i in range(N):
    if d[i]!=i+1:
        ans+=1
if ans>=3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())
    p = [0] + list(map(int, readline().split()))

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

np_list=sorted(p_list)

count=0
for i in range(n):
    if p_list[i]!=np_list[i]:
        count+=1

if count==2 or count==0:
    ans="YES"
else:
    ans="NO"

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

num = int(input())
str = input().split()
table = list(str)
table5 = [int(i) for i in table]
count = 0
table2 = sorted(table5)
table3 = [int(i) for i in table2]
for i in range(num):
    if table5[i] == table3[i]:
        continue
    else:
        count += 1

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

Submission status: Accepted

Solution code:
import sys
import itertools
tokens = itertools.chain.from_iterable(map(str.split, sys.stdin))

yesno = ['NO', 'YES']
n = int(next(tokens))
p = [int(next(tokens)) for _ in range(n)]


s = sum(1 if a != b else 0 for a, b in zip(p, sorted(p)))
print(yesno[s == 2 or s == 0])

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

# input = stdin.readline



def main():
  N = int(input())
  p = list(map(int,input().split()))
  def solve(n,p):
    assert n >= 2
    target = sum(0 if p[i]==i+1 else 1 for i in range(n)) 
    return target == 0 or target == 2
  print("YES" if solve(N,p) else "NO")
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

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

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

    if cnt == 0 or cnt == 2:
        ans = "YES"
    else:
        ans = "NO"

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
P_sort = sorted(P)
counter = 0
for p,p_sort in zip(P,P_sort):
    if p != p_sort:
        counter += 1
if counter == 2 or counter == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

memo = 0
mode = 1
for k in range(n):
	if memo > p[k]:
		mode = 0
		break
	memo = p[k]
if mode:
	print("YES")
	exit()

for i,j in itertools.combinations(range(n), 2):
	memo = 0
	mode = 1
	for k in range(n):
		if k == i:
			k = j
		elif k == j:
			k = i
		if memo > p[k]:
			mode = 0
			break
		memo = p[k]
	if mode:
		print("YES")
		exit()
print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def main():
    n = int(input())
    x = list(range(1, n+1))
    p = Input()
    data = [i != j for i, j in zip(x, p)]
    if sum(data) > 2:
        print("NO")
    else:
        print("YES")

main()

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

Submission status: Accepted

Solution code:
N = int(input())

p_ = list(range(1, N+1))
p = [int(n) for n in input().split()]

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=[int(x) for x in input().split()]
P=sorted(p)
cnt=0
for i in range(n):
  if p[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()))

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:
from itertools import combinations

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

combi = combinations([i for i in range(n)], 2)
sort = sorted(p)

if sort == p:
        print('YES')
        exit()

for i, j in combi:
    p[i], p[j] = p[j], p[i]
    if sort == p:
        print('YES')
        exit()
    p[j], p[i] = p[i], p[j]
print('NO')

Submission status: Accepted

Solution code:
import numpy as np

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

before = np.array(P)
after = np.sort(before)
sort_cnt = np.count_nonzero(before - after)

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0

for i in range(N):
    if lis[i] == i+1:
        cnt += 1


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

Submission status: Accepted

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

isPossible = True
for k in range(N-1):
    if P[k] > P[k+1]:
        isPossible = False
if isPossible:
    print("YES")
    exit()


for i in range(N):
    for j in range(i+1, N):
        newList = P.copy()
        newList[i], newList[j] = newList[j], newList[i]
        isPossible = True
        for k in range(N-1):
            if newList[k] > newList[k+1]:
                isPossible = False

        if isPossible:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

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

ans = 0

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

print('NO' if ans > 2 else 'YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import combinations

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

ans = 'NO'
p_sorted = list(sorted(p))
if p == p_sorted:
    ans = 'YES'
else:
    l = [i for i in range(N)]
    c = combinations(l, 2)
    for v in c:
        tmp = p.copy()
        tmp[v[0]], tmp[v[1]] = tmp[v[1]], tmp[v[0]]
        if tmp == p_sorted:
            ans = 'YES'
            break
print(ans)

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

Submission status: Accepted

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

def bubble(input_list):
    cnt = 0
    flg = 0
    for i in range(len(input_list)):
        if i+1 != input_list[i]:
            cnt +=1
            flg = 1
    if flg == 1:
        return False, cnt
    else:
        return True, 0
a, b = bubble(p)

if b <= 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:
import sys
N = int(input())
P = list(map(int,input().split()))

ans = sorted(P)

if P == ans:
  print("YES")
else:
  for i in range(N-1):
    for j in range(i+1,N):
      check = []
      check += P
      check[i],check[j] = check[j],check[i]
      if check == ans:
        print("YES")
        sys.exit()
  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()))
l = [i for i in range(1,n+1)]
c = 0
for j in range(n):
  if p[j] != l[j]:
    c += 1
print("YES" if c == 0 or c == 2 else "NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

pp = [i for i in range(1, n+1)]
cnt = 0

for i in range(n):
  if p[i] != pp[i]:
    cnt += 1
  if cnt > 2:
    print('NO')
    sys.exit()

print('YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 問題：https://atcoder.jp/contests/abc135/tasks/abc135_b

n = int(input())
p = list(map(int, input().strip().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 = list(map(int,input().split()))
s = sorted(p)
cnt = 0
for i in range(N):
    if p[i] != s[i]:
        cnt += 1
        if cnt > 2:
            print('NO')
            exit(0)
print('YES')