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()))
o = [i for i in range(1,n+1)]
k = 0
for i in range(n):
    if p[i] != o[i]:
        k += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = [i for i in map(int,input().split())]
check = 0
Model = [i for i in range(1,N+1)]
for i in range(N):
  if P[i] != Model[i]:
    check += 1
if check > 2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np


n = int(input())
p = [i for i in list(map(int, input().split()))]
p = np.array(p)
p_org = p.copy()
p.sort()
if (p != p_org).sum() <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split(" ")))
num_count = 0

for i in range(n):
    if x[i] != i+1:
        num_count += 1
        
if num_count == 0 or num_count == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys

# 入力を整数に変換して受け取る
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
# 入力全てを整数に変換したものの配列を受け取る
def LI(): return list(map(int, sys.stdin.readline().split()))
# 入力全てを整数に変換して1引いたものの配列を受け取る
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SR(): return sys.stdin.readline().rstrip()

ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_uppercase2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ'

# リストを改行区切りで出力する
p2D = lambda x: print(*x, sep="\n")
p2E = lambda x: print(''.join(x))
p2S = lambda x: print(*x, sep=" ")



n=II()
l=LI()

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

Submission status: Accepted

Solution code:
import sys
N=input()
P=map(int, sys.stdin.readline().split())
cnt=0
for x,y in zip(P,sorted(P)):
    if x!=y: cnt+=1

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

Submission status: Accepted

Solution code:
import io

nim = int(input())

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

kite = list(range(1,nim+1))

counter = 0

for i in range(nim):
    if kite[i]!=mike[i]:
        counter+=1
if counter<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

if p == p_:
  print("YES")
  sys.exit()

for i in range(N):
  for j in range(i+1, N):
    a = p[i]
    p[i] = p[j]
    p[j] = a
    if p == p_:
      print("YES")
      break
    else:
      b = p[i]
      p[i] = p[j]
      p[j] = b
      continue
  else:
    continue
  break
else:
  print("NO")

Submission status: Accepted

Solution code:
N=int(input())
p=[int(i) for i in input().split()]
q=sorted(p)
count=0
for t in range(N):
    if p[t]!=q[t]:
        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()))

count = 0
for i in range(n):
	if p[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=list(map(int, input().split()))
p_sort = []
for i in range(N):
  p_sort.append(p_list[i])
p_sort.sort()
ans = 0



for i in range(N):
  if p_list[i] != p_sort[i]:
    ans = ans + 1
if ans <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
N=int(input())
p=[int(i) for i in input().rstrip().split()]
p_c=copy.deepcopy(p)
p_c.sort()

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


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()))


count=0
for i in range(N):
    if p[i]==i+1:
        continue
    else:
        dammy = p[i]
        num = p.index(i+1)
        p[i]=p[num]
        p[num] = dammy
        count+=1

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

Submission status: Accepted

Solution code:
N = int(input())
p = [int(x) for x in input().split()]
n = [int(y) for y in range(1, N+1)]
ans = 'YES'
count = 0
for i in range(N):
    if p[i] == n[i]:
        pass
    else:
        if count <= 1:
            count +=1
        else:
            ans = 'NO'
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=[int(_) for _ in input().split()]
p_sort=sorted(p)
count=0
for i in range(n):
    if p[i]!=p_sort[i]:
        count+=1
if count==0 or count==2:print("YES")
else:print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
P=nl()
c=0
for i in range(1,N+1):
    if(P[i-1]!=i):
            c+=1

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import math
import collections
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n=I()
ps=LI()
count=0
for i in range(n):
    if ps[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()))
nlst = [x for x in range(1,n+1)]
lst = [0]*n
#print(nlst,lst)
for i in range(n):
    if nlst[i] != p[i]:
        lst[i] = 1
    #print(lst)
print('YES' if sum(lst) in [0,2] else 'NO')

Submission status: Accepted

Solution code:
#ABC135B
num = 0
tmp = [0,0,0,0]
N = int(input())
p = list(map(int,input().split()))
m = list(int(i) for i in range(1,N+1))
l = 0
if p == m:
  print("YES")
else:
  for k in range(N):
    if num >= 2:
      break
    if p[k] == m[k]:
      continue
    else:
      tmp[l] = p[k]
      tmp[l+1] = int(k+1)
      num+=1
      l+=2
  if tmp[1] == tmp[2]:
    print("YES")
  else:
    print("NO")

Submission status: Accepted

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

P = sorted(p)
cnt = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

c = sorted(p)
count = 0

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

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

Submission status: Accepted

Solution code:
n, *p = map(int, open(0).read().split())

p_sort = sorted(p)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0

for i in range(N-1):
    if not i + 1 == P[i]:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if p == p_l:
    print("YES")

else:
    for i in range(n):
        if not p[i] == p_l[i]:
            count +=1

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

Submission status: Accepted

Solution code:
N,*P,=map(int,open(0).read().split())
A=[a!=b for a,b in zip(range(1,N+1),P)]
print('YNEOS'[sum(A)!=0 and sum(A)!=2::2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def q2():
    N = int(input())
    P = [int(i) for i in input().split()]
    P_sorted = sorted(P)
    cnt = 0
    for i, p in enumerate(P):
        if P[i] - P_sorted[i] != 0:
            cnt += 1

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


if __name__ == '__main__':
    q2()

Submission status: Accepted

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

p_ = sorted(p)

number_of_different = 0
answer = 'YES'
for pi, pi_ in zip(p, p_):
    if pi is not pi_:
        number_of_different += 1

    if number_of_different >= 3:
        answer = 'NO'
        break

print(answer)

Submission status: Accepted

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

count = 0
f = True
for i in range(n):
    if p[i]!=q[i]:
        count+=1
    if count > 2:
        print('NO')
        f = False
        break
if f:
    print('YES')

Submission status: Accepted

Solution code:
N = int(input())
pi = list(map(int, input().split()))
pi_sort = sorted(pi)
count = 0
for p, p_s in zip(pi, pi_sort):
    if p != p_s:
        count += 1

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

n = INT()
p = LIST()
if p == list(range(1, n + 1)):
    print('YES')
    exit()
for i in range(n):
    for j in range(i + 1, n):
        arr = deepcopy(p)
        arr[i], arr[j] = arr[j], arr[i]
        if arr == list(range(1, n + 1)):
            print('YES')
            exit()
print('NO')

Submission status: Accepted

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


cnt = 0
ps = sorted(p)
for i in range(n):
  if ps[i] != p[i]:
    cnt += 1
if cnt < 3:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
a = int(input())
b = input().split()
count=0
for i in range(a):
    if int(b[i]) is not 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()))
count = 0
for i in range(len(p)):
    if i + 1 != p[i]:
        count += 1
if count > 2:
    print('NO')
else:
    print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
p_sort = sorted(p)
count = 0
for a, b in zip(p, p_sort):
  if a == b:
    count += 1
if len(p) - count <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""
  
def main():
  n = int(input())
  P = list(map(int,input().split()))
  
  ANS = sorted(P)
  
  for i in range(n-1):
    for j in range(i,n):
      DUMMY = P.copy()
      tmp = DUMMY[i]
      DUMMY[i] = DUMMY[j]
      DUMMY[j] = tmp
      #print(DUMMY)
      if DUMMY == ANS:
        print('YES')
        exit()
  print('NO')
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))

def main():
    N = int(input())
    P = input_nums()
    Q = sorted(P)
    diff = [1 for p, q in zip(P, Q) if p != q]
    if sum(diff) <= 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
#a,b=map(int,input().split())
pl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
#sortedl=sorted(pl)
flag=False
if pl==sorted(pl):
    flag=True
else:
    for i in range(n):
        for j in range(n):
            templ=pl[:]
            templ[i]=pl[j]
            templ[j]=pl[i]
            if templ==(sorted(pl)):
                flag=True
                break
if flag:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
P = [int(x) for x in input().split()]
res = 0
for i, p in enumerate(P, 1):
  if not i == p:
    res += 1
print('YES' if res <= 2 else 'NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
P = iil()
P_sorted = sorted(P)

indexes = []
count = 0

for i in range(n):
    if P[i] != P_sorted[i]:
        indexes.append(i)
        count += 1

if count == 0:
    print('YES')
elif count != 2:
    print('NO')
else:
    tmp = P[indexes[0]]
    P[indexes[0]] = P[indexes[1]]
    P[indexes[1]] = tmp
#    for j in range(n):
        #if P[j] != P_sorted[j]:
    if P != P_sorted:
        print('NO')
        exit()
    print('YES')

Submission status: Accepted

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

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

for i in range(n):
    for j in range(n):
        tmp = p.copy()
        tmp_i = tmp[i]
        tmp[i] = tmp[j]
        tmp[j] = tmp_i
        for k in range(n):
            cnt = 0
            if(ex[k] != tmp[k]):
                cnt += 1
                break
        if(cnt == 0):
            print('YES')
            exit()

print('NO')

Submission status: Accepted

Solution code:
import copy
N = int(input())
p = list(map(int,input().split()))
ph = copy.copy(p)
sp = copy.copy(p)
sp.sort()
for i in range(N-1):
  for j in range(i,N):
    tmp = ph[i]
    ph[i] = ph[j]
    ph[j] = tmp
    if sp == ph:
      print('YES')
      exit()
    ph = copy.copy(p)
print('NO')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

    s_p = sorted(p)

    c = 0
    for i in range(N):
        if p[i] != s_p[i]:
            c += 1
            if c > 2:
                print('NO')
                exit()

    print('YES')

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    s=LIST()
    sort_s=sorted(s)
    flg=0
    if s==sort_s:
        flg=1
    for i in range(n-1):
        for k in range(i+1,n):
            temp=copy.deepcopy(s)
            temp[i],temp[k]=temp[k],temp[i]
            #print(temp)
            if temp==sort_s:
                flg=1
    if flg==1:
        print('YES')
    else:
        print('NO')
        
    
if __name__ == '__main__':
    do()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
ok=0
for i in range(1,n+1):
  if i!=p[i-1]:
    ok+=1
if ok<=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)) #正しい昇順リスト
c = 0

if p == q:
  print("YES")

else:
  for i in range(n): #違う箇所の数を探す
    if p[i] != q[i]: #違ったら
      c = c + 1 #cを+1
    if c > 2: #違う箇所が2を超えたら見込みがないので抜ける
      break
  if c == 2: #2なら入れ替え1回で直る
    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 = [ int(x) for x in input().split() ]
q = sorted(p)

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

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

Submission status: Accepted

Solution code:
import sys
import copy
n = int(input())
p = list(map(int,input().split()))
flag = True
for i in range(1,n):
     if p[i] < p[i-1]:
          flag = False
          continue
if flag:
     print('YES')
     sys.exit()

for i in range(n-1):
     for j in range(i,n):
          pp = copy.deepcopy(p)
          pp[i] = copy.deepcopy(p[j])
          pp[j] = copy.deepcopy(p[i])
          flag = True

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

Submission status: Accepted

Solution code:
N=int(input())
P=list(map(int,input().split()))
_=0
for i,p in enumerate(P):
  if i+1!=p:
    _+=1
if _<=2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
num = list(map(int, input().split()))
num_sort = sorted(num)
flug = 0
for i in range(n):
  if num[i] == num_sort[i]:
    continue
  else:
    flug += 1
    if flug <= 2:
      continue
    else:
      print("NO")
      exit()
print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os, sys, re, math

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

diff = [P[i] != i + 1 for i in range(N)]
if sum(diff) == 2 or sum(diff) == 0:
    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)]
wrong = 0
for i in range(n):
    if p[i] != l[i]:
        wrong += 1
if wrong <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(i) for i in input().split()]
sortlst = [i + 1 for i in range(len(lst))]
non_match = 0
for a, b in zip(lst, sortlst):
	if a != b:
		non_match += 1
if non_match == 2 or non_match == 0:
	print('YES')
else:
	print('NO')

Submission status: Accepted

Solution code:
N = int(input())
P = [int(s) for s in input().split(' ')]
P2 = sorted(P)
ret = [1 for p, p2 in zip(P, P2) if p != p2]
print('YES' if len(ret) <= 2 else 'NO')

Submission status: Accepted

Solution code:
N = int(input())
P = [ int(s) for s in input().split() ]
P_S = sorted(P)
 
if sum(map(lambda x: x[0] != x[1], zip(P,P_S))) <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
import os, sys, re, math

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

sp = sorted(p)
diff = sum([1 if p[i] != sp[i] else 0 for i in range(N)])

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy

n = int(input())
p = list(map(int, input().split()))
q = copy.copy(p)
q.sort()
c = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

newp=sorted(p)
for i in range(len(newp)):
  ans1=p[i]
  ans2=newp[i]
  if ans1!=ans2:
    count+=1
    
if count==2 or newp==p:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
p=[int(x) for x in 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()))
p_sort = sorted(p)
non_0 = 0

for i in range(n):
    diff = p[i] - p_sort[i]
    if diff != 0:
        non_0 += 1
        if non_0 >2:
            print('NO')
            exit()

print('YES')

Submission status: Accepted

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

Q = sorted(P)

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

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

Submission status: Accepted

Solution code:
#整数の入力
A = int(input())
#スペース区切りの整数の入力リスト
S_list = list(map(int, input().split()))

A_list = []
for i in range(A):
  A_list.append(i+1)
  S_list[i] = int(S_list[i])
    

counter = 0
for i in range(A):
  if(A_list[i] != S_list[i]):
    counter = counter + 1

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

Submission status: Accepted

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

w1 = None
w2 = None
cnt = 0
for i in range(N):
  if i+1 != p[i]:
    cnt += 1
    if cnt == 1:
      w1 = i
    elif cnt == 2:
      w2 = i
    else:
      break
if cnt == 3:
  print('NO')
else:
  print('YES')

Submission status: Accepted

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

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')