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

Submission status: Accepted

Solution code:
# Swap 0 or 1 Swap

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
p=list(map(int,input().split()))
q=[i+1 for i in range(n)]
p=np.array(p)
q=np.array(q)
r=p-q
b=np.count_nonzero(r==0)
if b>=n-2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n =int(input())
p = tuple(map(int,input().split()))
q = sorted(p)
c = 0
for i in range(n):
  if p[i] != q[i]:
    c += 1
print('YNEOS'[c!=2 and c!=0::2])

Submission status: Accepted

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

result = li.copy()
result.sort()

cnt = 0

for x,y in zip(li,result):
    if x != y:
        cnt += 1

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

Submission status: Accepted

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

L = l.copy()

l.sort()

s = 0

for i, j in zip(L,l):
    if i != j : s+=1
def Y(a,t = 0):
    if a<t : print('YES')
    else : print('NO')
def y(a, t =0):
    if a==t : print('Yes')
    else : print('No')
Y(s,3)

Submission status: Accepted

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

a=[]

for i in range(len(li)-1):
  if li[i]>li[i+1]:
    if len(a)==0:
      a.append(i)
    else:
      a.append(i+1)
if len(a)>2:
  print('NO')
  exit()
else:
  a.append(len(li)-1)
  a.append(len(li)-1)
  temp=li[a[0]]
  li[a[0]]=li[a[1]]
  li[a[1]]=temp
for i in range(len(li)-1):
  if li[i]>li[i+1]:
    print('NO')
    exit()
print('YES')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

import numpy as np

p_sort = sorted(p)

p = np.array(p)
p_sort = np.array(p_sort)

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

Submission status: Accepted

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

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

print("YES" if cnt in {0, 2} else "NO")

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import deepcopy

def main():
    n = int(input())
    p = list(map(int,input().split()))
    org = deepcopy(p)
    p.sort()

    count = 0

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

a=int(input())
b=input().split() 
cnt=0

for i in range(a):
  if int(b[i]) != i+1:
    cnt =  cnt + 1
    if cnt == 3:
      print('NO')
      sys.exit()
print('YES')

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
c=0
for i in range(n-1):
  if a[i]!=i+1:
    c+=1
print('YES' if c<3 else 'NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

N, *P = map(int, sys.stdin.buffer.read().split())
Q = sorted(P)
count = 0
for x, y in zip(P, Q):
    if x != y:
        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()))

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

Submission status: Accepted

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

count=0
ans="YES"
for i in range(len(p)):
    if p[i]!=i+1:
        count+=1
    if count>=3:
        ans="NO"
        break

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 == 3:
      print('NO')
      break
else:
  print('YES')

Submission status: Accepted

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

cnt = 0
for i, p in enumerate(P):
    if i+1 != p:
        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:
		cnt += 1

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

Submission status: Accepted

Solution code:
n1 = int(input())

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

r1 = "YES"

n3 = sorted(n2)

c1 = 0

for i1 in range(n1):
    if n2[i1] != n3[i1]:
        c1 += 1
        if c1 == 3:
            r1 = "NO"

print(r1)

Submission status: Accepted

Solution code:
a  = int(input())
ls = [int(s) for s in input().split()]


total = 0

for i in range(a):
    if not ls[i] == i+1:
        total += 1

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

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

s_pnt = sorted(pnt)

cnt = 0
for p, s in zip(pnt, s_pnt):
    if p != s:
        cnt += 1


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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_b
# B - 0 or 1 Swap

N = int( input() )
plist = list( map( int, input().split()))
sorted_list = sorted( plist )

diff = 0
for n in range( N ):
    if plist[ n ] != sorted_list[ n ]:
        diff += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

k = 0
counter = 1
for e in p:
    if e == counter:
        counter += 1
    else:
        k += 1
        counter += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

s = tuple(sorted(P))

diff = 0
for a, b in zip(P, s):
    if a != b:
        diff += 1

if diff in (0, 2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

for i in range(n):
    if numlist==sortedlist:
        print("YES")
        break
    minnum=i
    for j in range(i,n):
        if numlist[minnum]>numlist[j]:
            minnum=j
    if minnum!=i:
        a=numlist[minnum]
        numlist[minnum]=numlist[i]
        numlist[i]=a
        if numlist==sortedlist:
            print("YES")
        else:
            print("NO")
        break

Submission status: Accepted

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

cnt = 0
for i in range(N):
    if cnt > 2:
        break

    if Nlist[i] == (i + 1):
        continue
    else:
        cnt += 1

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

Submission status: Accepted

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

sorted_pp = pp[:]
sorted_pp.sort()

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

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

Submission status: Accepted

Solution code:
N = raw_input()
a = map(int, raw_input().split())
ind = 0
counter = 0

for num in a:
	ind = ind + 1
	if num != ind:
		counter = counter + 1
	
if counter < 3:
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
count = 0
n = int(input())
L = list(map(int, input().split()))
for i in range(n):
    if L[i] == i+1:
        continue
    else:
        count = 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()))
Q = sorted(P)
num = 0
for i in range(N):
    if P[i]!=Q[i] : num+= 1 
print('YES') if num == 0 or num == 2 else print('NO')

Submission status: Accepted

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

r = sum([1 for i, j in zip(range(1, n+1), p) if i != j])
print("YES" if r == 0 or r == 2 else "NO")

Submission status: Accepted

Solution code:
N = input()
nList = input().split()

i = 1
checkSum = 0
for n in nList:
    if int(n) != i:
        checkSum += 1
    i += 1
if checkSum > 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):
    i = _i + 1
    if p[_i] != i:
        c += 1

if c <= 2:
    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(1, n+1):
  if a[i-1]!=i:
    count += 1
if count >= 3:
  print("NO")
else:
  print("YES")

Submission status: Accepted

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

sorted_list_ = sorted(list_)

count = sum([1 if l != r else 0 for l, r in zip(list_, sorted_list_)])

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

Submission status: Accepted

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

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

flag = True
for i in range(0, N):
  if i + 1 != li[i]:
    if flag == False:
      print("NO")
      break
    for j in range(i + 1, N):
      if li[j] == i + 1:
        li[i] = i + 1
        li[j] = j + 1
        flag = False
else:
  print("YES")

Submission status: Accepted

Solution code:
num = int(input())
a = input().split()
for i in range(len(a)):
  a[i] = int(a[i])

flag = True  
new = -1
m = -1
for i in range(num):
  if i+1 == a[i]:
    pass
  else:
    if new == -1:
      new = i
    else:
      a[i],a[new] = a[new], a[i]
      break

for i in range(num):
  if i+1 == a[i]:
    pass
  else:
    flag = False
    print('NO')
    break
if flag:
  print('YES')

Submission status: Accepted

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

for n, p in enumerate(P):
    if n+1 != p:
        count += 1
    if count > 2:
        print('NO')
        break
else:
    print('YES')

Submission status: Accepted

Solution code:
a=int(input())
list1=(input().split())
count=0
for i in range(a):#0,1,2,...len(list1)-1まで
    if int(list1[i])!=i+1:
        count+=1
if count==0 or count==2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
# 0 or 1 Swap
n = int(input())
a_list = list(map(int, input().split()))
b_list = range(1, n+1)
cnt = 0
for a, b in zip(a_list, b_list):
    if a != b:
        cnt+= 1
if cnt <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int,input().split()))
ori = list(range(1,N+1))
check = [array[index] == ori[index] for index in range(N)]
flag = N - sum(check)
if flag <=2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
#135
N=int(input())
p=list(map(int,input().split()))
a=p[:]
p_u=[x for x in range(1,N+1)]

if p==p_u:
    print("YES")
else:
    for i in range(N):
        if p[i]-p_u[i]>0:
            a[p[i]-1]=p[i]
            a[i]=p[p[i]-1]
            if a==p_u:
                print("YES")
            else:
                print("NO")
                break

Submission status: Accepted

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

P = p.copy()
P.sort()
final = []

if P == p:
    final.append("YES")
else:
    for i in range(N):
        for o in range(i):
            pCopy = p.copy()
            I = pCopy[i]
            O = pCopy[o]
            pCopy[i] = O
            pCopy[o] = I
            if pCopy == P:
                final.append("YES")
            pCopy = p
if "YES" in final:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
"""
 // Author : snape_here - Susanta Mukherjee
 // Date : 27/07/2019

"""
from __future__ import division, print_function
import os,sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()

    
#from collections import deque, Counter, OrderedDict,defaultdict
#from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
from math import ceil,floor,log,sqrt,factorial,pow,pi
#from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
#from decimal import *,threading


# For getting input from input.txt file 
#sys.stdin = open('input.txt', 'r')  
  
# Printing the Output to output.txt file 
#sys.stdout = open('output.txt', 'w') 

def ii(): return int(input())
def fi(): return float(input())
def si(): return str(input())
def mi(): return map(int,input().split())
def li(): return list(mi())

def main():
    
    n=ii()
    p=li()
    l=sorted(p)
    c=0
    for i in range(n):
        if p[i]==l[i]:
            c+=1
    if c==n or c==n-2:
        print("YES")
    else:
        print("NO")
    
    
# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        if self.buffer.tell():
            return self.buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding:utf-8
n = int(input())
num_list = [int(x) for x in input().split(' ')]

diff_list = []
for i, num in enumerate(num_list):
    if not i+1 == num:
        diff_list.append([i+1, num])
    if len(diff_list) > 2:
        break
if len(diff_list) == 2 and diff_list[0][0] == diff_list[1][1] and diff_list[0][1] == diff_list[1][0]:
    print('YES')
elif len(diff_list) == 0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
count = 0
for i, j in enumerate(a):
    if i+1 != j:
        count += 1
print('YES' if count in [0,2] else 'NO')

Submission status: Accepted

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

def is_asc(l, o):
    for o1, o2 in zip(o, o[1:]):
        if l[o1] > l[o2]:
            return False
    return True

if is_asc(pl, range(n)):
    print('YES')
else:
    can_asc = False
    for i in range(n):
        for j in range(i+1, n):
            o = list(range(n))
            o[i] = j
            o[j] = i
            if is_asc(pl, o):
                can_asc = True
                break
    if can_asc:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
import sys
import copy
input = sys.stdin.readline
n = int(input())
p = list(map(int, input().split()))
for i in range(n):
    for j in range(i, n):
        x = copy.copy(p)
        x[i], x[j] = x[j], x[i]
        y = sorted(x)
        if x == y:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

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

cnt = 0
for i in range(len(Ps)):
    if Ps[i] != i + 1:
        cnt+=1
if cnt>2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

N = int(input())
p = list(map(int,input().split()))
#print(p[0])
flag = 0

for i in range(N):	
	if p[i] != i + 1 : #iは０からはじまるから＋１する
		flag = flag + 1

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

Submission status: Accepted

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

count=0
for i in range(int(n)):
  if N[i] !=i+1:
    count+=1
if count > 2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

Solution code:
n = input()
s = input().split()

s = [int(i) for i in s]

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

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

Submission status: Accepted

Solution code:
def check_sort(alist):
    check = None
    for i in range(n-1):
        if alist[i] > alist[i+1]:
            check = [i]
            break
    for i in range(1,n):
        if alist[i] < alist[i-1]:
            temp = i
            break
    for i in range(1,n):
        if alist[i] < alist[i-1]:
            if alist[i] < alist[temp]:
                temp = i
    if check is not None:
        check.append(temp)
    return check
n = int(input())
raw_in = input().split()
alist = [int(x) for x in raw_in]
check_max = alist[0]
check_id = 0
status = check_sort(alist)
if status is None:
    print("YES")
else:
    alist[status[0]], alist[status[1]] = alist[status[1]], alist[status[0]]
    if check_sort(alist) is None:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  lst = list(map(int, input().split()))
  
  a = lst.copy()
  a.sort()
  t = 0
  for i in range(n):
    if (lst[i] != a[i]) == True:
      t += 1
  
  if t ==2 or t == 0:
    print("YES")
  else:
    print("NO")
main()

Submission status: Accepted

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

Submission status: Accepted

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

cnt=0
for i in range(len(itr)):
    if (i + 1) != itr[i]:
        cnt += 1
    if cnt > 2:
        print("NO")
        break
else:
    print("YES")

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    ps = [int(x) for x in input().split()]

    dis = []

    for n, p in enumerate(ps):
        if n + 1 != p:
            dis.append(p)

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

Submission status: Accepted

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

c = 0
i = 1
for x in xs:
    if x != i:
        c += 1
    i += 1

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

Submission status: Accepted

Solution code:
n = int(input())
p_n = list(map(int, input().split()))
c = 0
for i, p in enumerate(p_n):
    if i + 1 != p:
        c += 1

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

Submission status: Accepted

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

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

    c=p[i]
    p[i], p[c-1] = p[c-1],p[i]

    if(count == 2):
      print("NO")
      exit()

print("YES")
exit()

Submission status: Accepted

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

Submission status: Accepted

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

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

             

if flag >= n-2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
p_raw = np.array(list(map(int, input().split())))
p_sort = np.sort(p_raw)

compare = (p_raw==p_sort)

if np.count_nonzero(compare == False) > 2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

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

n = int(input())

numbers = list(map(int, input().split(" ")))
a = int(numbers[0])
b = int(numbers[1])

cnt = 0

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

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

Submission status: Accepted

Solution code:
import sys

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

def isAsc(lst):
  for i in range(len(lst)-1):
    if lst[i]>lst[i+1]:
      return False
  else:
    return True

if isAsc(plist):
  print("YES")
  sys.exit()

for i in range(N-1):
  for j in range(i+1,N):
    p2list=plist[:]
    p2list[i],p2list[j] = plist[j],plist[i]
    #print(p2list)
    if isAsc(p2list):
      print("YES")
      sys.exit()
      
print("NO")

Submission status: Accepted

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

B = sorted(P)
   
tmp = 0
for i in range(N):
    if P[i] != B[i]:
        tmp += 1
        
if tmp == 2 or tmp == 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 i+1 != p[i]:
        cnt += 1

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

Submission status: Accepted

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

sorted_p_list = sorted(p_list)

count = 0
flag = True

if sorted_p_list == p_list:
    print("YES")
else:
    for i in range(n):
        if sorted_p_list[i] != p_list[i]:
            count += 1
            if count > 2:
                print("NO")
                flag = False
                break

    if flag: print("YES")

Submission status: Accepted

Solution code:
n = int(input())

data = [int(s) for s in input().split()]
count = 0
for i in range(n):
  if (i+1)!= data[i]:
    count += 1
if count<= 2:
  print("YES")
else:
  print("NO")
    
  
"""
mx = max(data)
mn = min(data)

if (mx == n-1 and mn == 0) or 
  for i in range(1,n-1):
    if data[i] < data[i+1]:
      continue
    else:
      print("No")
      exit()
"""

Submission status: Accepted

Solution code:
N = int(input())
l = [list(map(int, input().split())) for _ in range(1)][0]
t = sorted(l)
count = 0
for i in range(len(l)):
    if l[i] == t[i]:
        count += 1

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

Submission status: Accepted

Solution code:
import numpy as np
print('YES') if np.count_nonzero(np.array(np.arange(1, int(input())+1) != list(map(int, input().split())))) < 3 else print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split(' ')))
q = p[:]
q.sort()

diff = 0
for _p, _q in zip(p, q):
    if _p != _q:
        diff += 1

    if diff > 2:
        break

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split(' ')))
q = p[:]
q.sort()

diff = 0
for _p, _q in zip(p, q):
    if _p != _q:
        diff += 1

print('YES' if diff <= 2 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())
a = [int(i) for i in input().split()]
count = 0
for i in range(N):
    if a[i] != i+1:
        count += 1
if count == 2 or count == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

cnt = 0
for i in range(n):
    if p_l[i] != min(p_l) + i:
        cnt += 1
if cnt == 2 or p_l == sorted(p_l):
    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):
    if A[i]!=i+1:
        count+=1
if count==2 or count==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
pn = np.array(list(map(lambda x: int(x), input().split(' '))))
diff = np.count_nonzero(pn != np.arange(1, n + 1, dtype='int'))
print('YES') if diff in [0, 2] else print('NO')

Submission status: Accepted

Solution code:
size = int(input())
list_number = list(map(int, input().split()))

k = 0

for i, number in enumerate(list_number):
    if i + 1 != number:
        k += 1

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

Submission status: Accepted

Solution code:
input()

xs = [int(x) for x in input().split(" ")]
c  = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

is_switched = False
for i in range(len(seq)-1):
    if seq[i] <= min(seq[i+1:]):
        continue
    elif seq[i] > min(seq[i+1:]):
        if is_switched:
            print("NO")
            break
        j = seq.index(min(seq[i+1:]))
        seq[i], seq[j] = seq[j], seq[i]
        is_switched = True
else:
    print("YES")

Submission status: Accepted

Solution code:
s = int(input())

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

count = 0

for val in range(s):
    if val+1 != nums[val]:
        count += 1

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

Submission status: Accepted

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