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:
words = lambda t : list(map(t, input().split()))
n = int(input())
xs = words(int)

s_xs = sorted(xs)
ans = False
if s_xs == xs:
    ans = True
else:
    for i in range(n-1):
        for j in range(i,n):
            f_xs = xs.copy()
            tmp = f_xs[i]
            f_xs[i] = f_xs[j]
            f_xs[j] = tmp
            if s_xs == f_xs:
                ans = True
                break
        if ans == True:
            break
if ans == True:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

count = 0

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

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

Submission status: Accepted

Solution code:
a = int(input())
b = input().split()
seisuu = [int(i) for i in b]
seisuu_sorted = sorted(seisuu)
count = 0

for i in range(a):
    if seisuu[i] != seisuu_sorted[i]:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  l2=copy.deepcopy(l)
  l2.sort()

  c=0
  l3=[]
  l4=[]
  for i in range(n):
    if l[i]!=l2[i]:
      c+=1
      l3.append(l[i])
      l4.append(l2[i])

  if c==0:
    return 'YES'
  l3.sort()
  l4.sort()
  if c==2 and l3==l4:
    return 'YES'
  return 'NO'

# main()
print(main())

Submission status: Accepted

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

Submission status: Accepted

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

def get_num():
    return int(input())


def get_nums(n):
    res = [0 for _ in range(n)]
    for i in range(n):
        res[i] = int(input())
    return res


def get_numbers():
    return map(int, input().split())


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

cnt = 0
for i, pi in enumerate(p):
    if pi != i + 1:
        cnt += 1

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

Submission status: Accepted

Solution code:
n = int(input())
q = list(map(int, input().split()))
ls = []
for i in range(n):
  ls.append(q[i])
#print(ls)

ls.sort()

ct = 0
for j in range(n):
  if ls[j] != q[j]:
    ct += 1
#print(ct)
if ct <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
p = input().split()
p = np.array(p).astype(type(N))

sorted_list = [i for i in range(1,N+1)]
sorted_list = np.array(sorted_list).astype(type(N))

OK_list =   np.uint8(sorted_list != p)

if(np.sum(OK_list) <= 2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(input())
Ps = [int(i) for i in input().split(" ")]
Qs = list(sorted(Ps))
cnt = 0
for i in range(N):
  if Ps[i] != Qs[i]:
    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()))

p2 = sorted(p)

if p2 == p:
    print('YES')
    sys.exit()


for i in range(n-1):
    for j in range(i+1, n):
        new = list(p)
        temp = p[i]
        new[i] = p[j]
        new[j] = temp
        if new == p2:
            print('YES')
            sys.exit()

print('NO')

Submission status: Accepted

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

if cnt == 2 or cnt == 0:
    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(1, n + 1):
    if a[i - 1] != i:
        c += 1
    if c > 2:
        break

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

Submission status: Accepted

Solution code:
n = int(input())

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

sortli = sorted(li)

count = 0

for i in range(n):
    if li[i] == sortli[i]:
        count += 1

if count == n:
    print('YES')


elif count + 2 == n:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import sys,collections,math,random;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
lis = Is()
slis = sorted(lis)
c = 0
for i in range(n):
    if lis[i] != slis[i]:
        c += 1
if c == 2 or c == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

s = p[:]
s.sort()

sum = 0

for i in range(len(p)):
    if s[i] != p[i]:
        sum += 1

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

Submission status: Accepted

Solution code:
N = int(input())
p_list = list(map(int, input().split()))
sorted_list = sorted(p_list)
count = 0
for p, s in zip(p_list, sorted_list):
    if p != s:
        count += 1
if count == 0 or count == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

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

Submission status: Accepted

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

result = [Ps[i] for i in range(N) if Ps[i] != Sorted[i]]

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

Submission status: Accepted

Solution code:
x = int(input()) # input:"7" intを忘れない
List = [int(i) for i in input().split()] # input:"1 2 3 4 5"
ans = 0
for i in range(x):
  if abs(i+1-List[i])>0:
    ans += 1
if ans==2 or ans==0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
s = input().split(' ', n)
count = 0

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

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

Submission status: Accepted

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
Mod = 1000000007



def main(): #startline-------------------------------------------
    N = int(input())
    P = list(map(int,input().split()))
    Psample = [i for i in range(1,N+1)]
    cnt = 0
    if P == Psample:
        print("YES")
        exit()
    for i in range(N):
        if P[i] != Psample[i]:
            cnt += 1
        else:
            pass
    if cnt != 2:
        print("NO")
        exit()
    else:
        P.sort()
        if set(P) == set(Psample):
            print("YES")
        else:
            print("NO")


if __name__ == "__main__":
    main() #endline===============================================




def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

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()))
q = sorted(p)

ans = N

for i in range(N):
    if p[i] == q[i]:
        ans -= 1

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

Submission status: Accepted

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

def checkSort(inp, n):
    for x in range(1, n):
        if inp[x] < inp[x-1]:
            return False
    return True

count = 0
if checkSort(p, n) == True:
    print("YES")
    sys.exit()

for i in range(n-1):
    for j in range(i+1, n):
        q = copy.copy(p)
        q[i] = p[j]
        q[j] = p[i]
        if checkSort(q, n) == True:
            print("YES")
            sys.exit()

print("NO")

Submission status: Accepted

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

for i in range(n-1):
    for j in range(i,n):
        tmp = p.copy()
        tmp[i],tmp[j] = p[j],p[i]
        if tmp == s:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
N = int(input())
p = list(map(int, input().split()))
sorted_p = copy.copy(p)
sorted_p.sort()
counter = 0

for i in range(N):
	if p[i] != sorted_p[i]:
		counter += 1
		
if counter <= 2:
	print('YES')
else:
	print('NO')

Submission status: Accepted

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


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

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

counter = 0
for i in range(N):
    if P[i] != Q[i]:
        counter += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

p_sort = sorted(p)

count = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i in range(N):
  if P[i] != psorted[i]:
    count += 1
  if count == 3:
    break
if count == 0:
  print("YES")
elif count == 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
P = np.array(list(map(int, input().split())))
Ps = np.sort(P)
s = ~(Ps == P)
print("YES" if s.sum() in (0,2) else "NO")

Submission status: Accepted

Solution code:
N = int(input())
ps = list(map(int,input().split()))
sortps = sorted(ps)
count = 0
for j in range(len(sortps)):
    if ps[j]!=sortps[j]:
        count+=1
if count==2 or count==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())

p_list=list(map(int, input().split()))
p_list_asc=sorted(p_list)

count=0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
p_array_tmp = [int(x) for x in input().split()]
for i in range(n):
    for j in range(i, n):
        if i == j:
            continue
        p_array = list(p_array_tmp)
        if p_array[i] > p_array[j]:
            tmp = p_array[i]
            p_array[i] = p_array[j]
            p_array[j] = tmp
        now = p_array[0]
        for p in p_array[1:]:
            if p > now:
                now = p
            else:
                break
        else:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

Solution code:
import sys

n = int(input())

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

ps = sorted(p)

count = 0

for a, b in zip(p, ps):
    if(a != b):
        count += 1
    if(count > 2):
        print("NO")
        sys.exit()
        break

print("YES")

Submission status: Accepted

Solution code:
N = int(input())

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

psort = sorted(p)

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

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

Submission status: Accepted

Solution code:
import itertools,copy
N = int(input())
a = list(map(int,input().split()))

def judge(array):
  for i in range(len(array)-1):
    if array[i] < array[i+1]:
      continue
    else:
      return False
  return True

def swap(i,j,array):
  arr = copy.copy(array)
  tmp = arr[i]
  arr[i] = arr[j]
  arr[j] = tmp
  return arr

ans = False
asc = judge(a)
if asc:
  ans = True
for (i,j) in itertools.combinations(range(N),2):
  arr = swap(i,j,a)
  asc = judge(arr)
  if asc: 
    ans = True
if ans:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
N = int(input())
nums = [int(i) for i in input().split()]
sorted_nums = sorted(nums)
N = 0
for i in range(len(nums)):
    if nums[i] != sorted_nums[i]:
        N += 1
if N <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

p_sorted = sorted(p)

ans = 'YES'

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

if cnt > 2:
    ans = 'NO'

print(ans)

Submission status: Accepted

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

YES = "YES"  # type: str
NO = "NO"  # type: str

def solve(N: int, p: "List[int]"):
    for i in range(N):
        if p[i] != i + 1:
            j = p[i] - 1
            p[i], p[j] = p[j], p[i]
            break
    for i in range(N):
        if p[i] != i + 1:
            print(NO)
            return
    print(YES)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    p = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
p = list(map(int,input().split()))
sorted_p = sorted(p)
cnt = 0
ret = "YES"
for pi,spi in zip(p,sorted_p):
    if pi != spi:
        cnt+=1
    if cnt > 2:
        ret = "NO"
        break
print(ret)

Submission status: Accepted

Solution code:
import sys

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

if __name__ == "__main__":
    n = int(input())
    pl = list(map(int, input().split()))

    pl_sorted = sorted(pl)

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

    print("YES" if diff_count == 0 or diff_count == 2 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]:
    continue
  else:
    c += 1
    if c == 3:
      print("NO")
      exit()
print("YES")

Submission status: Accepted

Solution code:
N = int(input())
hoge = input().split()
p = []
q = []
for i in range(N):
  p.append(i)
  q.append(int(hoge[i]) - i)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import io
import sys


def IN_S():
  return input()


def IN_I():
  return int(input())


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


def IN_L_S():
  return list(map(str, input().split()))


def STR_SPLIT(s, n):
  for l in range(0, len(s), n):
    yield s[0 + l:n + l]


def T_IN():
  global test_str
  sys.stdin = io.StringIO(test_str[1:-1])


test_str = '''
5
5 2 3 4 1
'''


def MAIN():
  # T_IN()
  if A():
    print("YES")
  else:
    print("NO")


def A():
  n = IN_I()
  l = IN_L_I()
  cnt = 0
  for i, item in enumerate(l):
    if i + 1 != item:
      cnt += 1

  if cnt == 0 or cnt == 2:
    return True
  
  return False


def B():
  return None


def C():
  return None


MAIN()

Submission status: Accepted

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

P_sorted = sorted(P)

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
lst = list(range(1,n+1,1))
x = 0
for i in range(len(lst)):
  if lst[i]==p[i]:
    continue
  else:
    x+=1
if (x==0)|(x==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 p[i] == i + 1:
        counter += 1
if counter == n - 2 or counter == n:
    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] - 1 != i:
    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()))
ans = 0
for i in range(N):
    if p[i] != i+1:
        ans += 1
if ans > 2:
    print('NO')
else:
    print('YES')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    p = list(map(int, input().split()))
    sort_p = sorted(p)

    flag = []

    for i in range(N):
        if sort_p[i] == p[i]:
            flag.append(True)
        else:
            flag.append(False)
    if flag.count(False) <= 2:
        print('YES')
    else:
        print('NO')

main()

Submission status: Accepted

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

s = list(range(1,len(p)+1))


is_ok = 1
m = 0
l = 0
for i, j in zip(p, s):
    if i == j:
        pass
    else:
        if m == 0:
            m = i
            l = j
        else:
            if i == l and j == m:
                pass
            else:
                is_ok = 0

if is_ok == 1:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(input())
p = [0]*N
p = list(map(int, input().split()))
q = [0]*N
ans = int(0)

q = sorted(p)

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:
N=int(input())

p= list(map(int, input().split())) #ABCが数字の場合これで一発


a = sorted(p)

diff = 0

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



ans = 'NO'
if diff==2 or diff == 0:
    ans = 'YES'



print(ans)

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())
p_list = [int(x) for x in sys.stdin.readline().split()]
p_list_sort = sorted(p_list)
if p_list == p_list_sort:
    print("YES")
    sys.exit(0)
for i in range(len(p_list)):
    for j in range(len(p_list)):
        if i == j:
            break
        p_list[i], p_list[j] = p_list[j], p_list[i]
        if p_list == p_list_sort:
            print("YES")
            sys.exit(0)
        else:
            p_list[i], p_list[j] = p_list[j], p_list[i]
else:
    print("NO")

Submission status: Accepted

Solution code:


def check(lst, n):
    i = 0
    j = 1

    while j < n:
        if lst[i] > lst[j]:
            return False
        i += 1
        j += 1

    return True


def swap(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp


def submit():
    n = int(input())
    plist = list(map(int, input().split()))

    if check(plist, n):
        print('YES')
        return

    for i in range(0, n - 1):
        for j in range(i + 1, n):
            swap(plist, i, j)
            if check(plist, n):
                print('YES')
                return
            swap(plist, j, i)

    print('NO')


if __name__ == '__main__':
    submit()

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()))
p2 = sorted(p)

count = 0

for a, b in zip(p, p2):
    if a != b:
        count += 1

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

Submission status: Accepted

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

count = 0
for i in range(n):
    if data[i] != i+1:
        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()))
# ans = False
ans = 0
for i in range(N):
    if P[i] != i+1:
        ans += 1
# for i in range(N):
#     for j in range(i+1, N):
#         P[i], P[j] = P[j], P[i]
#         for k in range(1, N):
#             if P[k] < P[k-1]:
#                 break
#             if k == N-1:
#                 ans = True
#         if ans:
#             break
#         P[i], P[j] = P[j], P[i]
#     if ans:
#         break

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())

P = [int(s) for s in input().split()]

K = [int(i+1) for i in range(n)]

P = np.array(P)
K = np.array(K)
P_K = P - K

c = 0
for i in range(n):
    if P_K[i] != 0:
        c += 1
    else:
        pass
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,j in enumerate(p):
    if i+1==j:
        continue
    else:
        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()]

array = [i for i in range(1, N + 1)]
cnt = 0

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

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(x) for x in input().split()]
x = sorted(p)
cnt = 0
for ind,i in enumerate(p):
    if(x[ind]!=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

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

Submission status: Accepted

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

def readInts():
  return [int(s) for s in input().split(" ")]

def is_ok(N, p):
  for k in range(N-1):
    if p[k] > p[k+1]:
      return False
  return True

def solve(N, p):
  if is_ok(N, p):
    return "YES"

  for i in range(N-1):
    for j in range(i, N):
      ps = p[:]
      ps[i], ps[j] = p[j], p[i]
      if is_ok(N, ps):
        return "YES"
  return "NO"

def main():
  N = readInts()[0]
  p = readInts()
  print(solve(N, p))

if __name__ == "__main__":
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
p =[int(i) for i in 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:
a = int(input())

b = input().split()

counter = 0
for i,one in enumerate(b):
    if i+1 != int(one):
        counter += 1
        
if counter == 0 or counter == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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


def solv(n, p):
    sp = sorted(p)
    count = 0
    for i in range(n):
        if p[i] != sp[i]:
            count += 1
    if count == 0 or count == 2:
        return 'YES'
    return 'NO'


if __name__ == '__main__':

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

    ans = solv(n, p)

    print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, d=DVSR): return pow(x, d - 2, d)
def DIV(x, y, d=DVSR): return (x * INV(y, d)) % d
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

N=II()
ps = LI()
sps = list(sorted(ps))

diff = 0
for i, j in zip(ps,sps):
    diff += i != j

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

Submission status: Accepted

Solution code:
N = int(input())
ps = [int(i) for i in input().split()]
qs = range(1, N+1)

times = 0

for i in range(N):
    if ps[i] != qs[i]:
        times +=1

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

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    p=[int(i) for i in input().split(' ')]
    p_sorted = sorted(p)
    cnt = 0
    for i,j in zip(p,p_sorted):
        if i != j:
            cnt += 1
    if cnt ==2 or cnt == 0:
        print('YES')
    else:
        print('NO')
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

yes = False

count = 0
for i, v in enumerate(p):
    if i+1 != v:
        count+=1

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

Submission status: Accepted

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

count = 0
for i, p in enumerate(ps, 1):
    if i != p:
        count += 1

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
p_s = list(map(int,input().split()))
cnt = 0
for i,p in enumerate(p_s):
    if i+1 != p:
  	    cnt += 1
    if cnt > 2:
        break
else:
    print("YES")
    sys.exit(0)
print("NO")

Submission status: Accepted

Solution code:
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

def check(P):
    t = 0
    for p in P:
        if t > p:
            return False
        t = p
    return True

def main():
    N = i2n()
    P = i2nn()
    if check(P):
        print('YES')
        return
    for i in range(N):
        for j in range(i+1, N):
            P[i], P[j] = P[j], P[i]
            if check(P):
                print('YES')
                return
            P[i], P[j] = P[j], P[i]
    print('NO')

main()