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:
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    ps = LI()
    ps_s = sorted(ps)
    c = 0
    for i in range(n):
        if ps[i] != ps_s[i]:
            c += 1
    if c < 3:
        print("YES")
    else:
        print("NO")

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
p = [int(x) for x in stdin.readline().rstrip().split()]

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
p = list(map(int, input().split()))
ans = all(p[i] < p[i + 1] for i in range(n - 1))
for i in range(n):
    for j in range(i + 1, n):
        a = p[:]
        a[i], a[j] = a[j], a[i]
        f = True
        for k in range(n - 1):
            if a[k] >= a[k + 1]:
                f = False
        if f:
            ans = True
print(['NO', 'YES'][ans])

Submission status: Accepted

Solution code:
N=int(input())
Pn=[int(x) for x in input().split()]
sortedPn=sorted(Pn)
c=0
for i,Pi in enumerate(Pn):
  if sortedPn[i]!=Pi:
    c+=1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Ps = sorted(P)

cnt = 0
for i in range(N):
  if P[i] != Ps[i]:
    cnt += 1
    
#print(cnt)
print('YES' if cnt <= 2 else 'NO')

Submission status: Accepted

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

AA = sorted(A)

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

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

Submission status: Accepted

Solution code:
def InputToInt():
    A = input().split()
    for i in range(len(A)):
        A[i] = int(A[i])
    return A
    

N = int(input())
p = InputToInt()

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 = [int(x) for x in input().split()]
numofchange = 0
for i in range(N):
    if p[i] != i+1:
        numofchange += 1
if numofchange <= 2:
    ans = "YES"
else:
    ans = "NO"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
p=[int(i) for i in input().split()]
ans = p[:]
ans.sort()
for i in range(n):
  for j in range(n):
    p[i],p[j]=p[j],p[i]
    if p==ans:
      print("YES")
      exit()
    p[i],p[j]=p[j],p[i]
print("NO")

Submission status: Accepted

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

cnt = 0
for i in range(n):
    if p[i] != p_sort[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()))

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for p1, p2 in zip(p_list, p_sorted):
	if p1 != p2:
		count += 1
if count <= 2:
	print("YES")
else:
	print("NO")

Submission status: Accepted

Solution code:
def is_sorted(A):
    return sorted(A) == A

def f(n,A):
    if is_sorted(A):
        return True
    for i in range(n):
        for j in range(i):
            B = A[:]
            B[i],B[j] = B[j],B[i]
            if is_sorted(B):
                return True
    return False

n = int(input())
A = [int(x) for x in input().split()]
print("YES" if f(n,A) else "NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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

    sorted_pn = sorted(pn)

    diff = 0
    for (p, sp) in zip(pn, sorted_pn):
        if (p != sp):
            diff += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
for p, q in zip(P, Q):
    if p != q:
        res += 1
print("YES" if res <= 2 else "NO")

Submission status: Accepted

Solution code:
import sys

def debug(*args):
    print(*args, file=sys.stderr)


def solve(N, A):
    S = sorted(A)
    cnt = sum(x != y for x, y in zip(A, S))
    if cnt in (0, 2):
        return "YES"
    return "NO"

def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    print(solve(N, A))

main()

Submission status: Accepted

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

matigai=0

for al in range(len(alist)):
    if alist[al]!=al+1:
        matigai+=1

#print(matigai)

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

Submission status: Accepted

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

Submission status: Accepted

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


# print(p)
sp = sorted(p)
# print(sp)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == "__main__":
    n = int(input())
    p_list = li()

    p_sort = sorted(p_list)
    diff = 0
    for p1, p2 in zip(p_list, p_sort):
        if p1 != p2:
            diff = diff + 1
    if diff <= 2:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    P = LI()
    cor = sorted(P)
    for i in range(N):
        for j in range(N):
            ok = True
            for k,c in enumerate(cor):
                a = P[k]
                if k == i:
                    a = P[j]
                elif k == j:
                    a = P[i]
                if a!=c:
                    ok=False
                    break
            if ok:
                print("YES")
                exit()
    print("NO")
if __name__ == '__main__':
    main()

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
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 input(): return sys.stdin.readline().strip()
def INT(): return int(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()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
p = LIST()
s = sorted(p)
# print(p)
# print(s)
cnt = 0
for i in range(n):
    if p[i] != s[i]:
        cnt += 1
if cnt == 2 or cnt == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

n=int(input())
ps=list(map(int,input().split()))
cnt=0
for i,p in enumerate(ps):
    if i+1!=p:
        cnt+=1
    if cnt>2:
        print("NO")
        break
else:
    print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

a_list = p_list.copy()
a_list.sort()

ans = 0

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
for k in range(n - 1):
    if a[k] >= a[k + 1]:
        break
else:
    print('YES')
    exit(0)
for i in range(n):
    for j in range(i + 1, n):
        tmp = a.copy()
        tmp[i], tmp[j] = tmp[j], tmp[i]
        for k in range(n - 1):
            if tmp[k] >= tmp[k + 1]:
                break
        else:
            print('YES')
            exit(0)
print('NO')

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
s = tuple(sorted(P))
if tuple(P) == s:
  print('YES')
  exit()
for i in range(N):
  for j in range(i+1, N):
    if tuple(P[:i] + [P[j]] + P[i+1:j] + [P[i]] + P[j+1:]) == s:
      print('YES')
      exit()
print('NO')

Submission status: Accepted

Solution code:
# ABC135
def main():
    n = int(input())
    P = tuple(map(int, input().rstrip().split()))
    dif = []
    for i, j in enumerate(P, start=1):
        if i != j:
            dif += [(i, j)]
    if len(dif) == 0:
        print("YES")
    elif len(dif) != 2:
        print("NO")
    elif dif[0] != dif[1][::-1]:
        print("NO")
    else:
        print("YES")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
pli=list(map(int,input().split()))
cnt=0
for i,j in zip(pli,sorted(pli)):
	if i!=j:cnt+=1
if cnt==2 or cnt==0:print("YES")
else:print("NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0

for idx, pp in enumerate(p):
    if idx + 1 != pp:
        cnt += 1
    
    if cnt >= 3:
        print('NO')
        exit()
print('YES')

Submission status: Accepted

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

cnt = 0
for pi, qi in zip(p, q):
    if pi != qi:
        cnt += 1

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
if p == list(range(1, N + 1)):
    result = True
else:
    result = False
    for i in range(N):
        for j in range(i + 1, N):
            q = [p[k] for k in range(N)]
            q[i], q[j] = q[j], q[i]
            if q == list(range(1, N + 1)):
                result = True
print("YES" if result else "NO")

Submission status: Accepted

Solution code:
N=int(input())
p=[int(i) for i in input().split()]
flag=1
for k in range(N-1):
    if p[k]>p[k+1]:
        flag=0
        break
if flag==1:
    print("YES")
    exit()
for i in range(N):
    for j in range(N):
        if i>=j:
            continue
        q=[p[i] for i in range(N)]
        q[i],q[j]=q[j],q[i]
        flag=1
        for k in range(N-1):
            if q[k]>q[k+1]:
                flag=0
                break
        if flag==1:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
sP = sorted(P)
for i in range(N):
    for j in range(N):
        P[i], P[j] = P[j], P[i]
        if P==sP:
            print("YES")
            exit()
        P[i], P[j] = P[j], P[i]
print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def swap(arr,i,j):
    arr1 = arr[:]
    arr1[i],arr1[j] = arr1[j],arr1[i]

    return arr1

def increasing(arr):
    for i in range(len(arr)-1):
        if arr[i+1] <= arr[i]:
            return False

    return True

def main():
    n = int(input())
    arr = list(map(int,input().split()))
    if increasing(arr):
        print('YES')
        return

    for i in range(n):
        for j in range(n):
            arr1 = swap(arr,i,j)
            if increasing(arr1):
                print('YES')
                return

    print('NO')

main()

Submission status: Accepted

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

ps = sorted(p)
c = 0
for i, v in enumerate(p):
    if v != ps[i]:
        c += 1
    if c > 2:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

Solution code:
n = int(input())

num_list = [ int(v) for v in input().split() ]
right_list = [ i+1 for i in range(n) ]

wrong = 0
for i in range(n):
    if num_list[i] != right_list[i]:
        wrong += 1
if wrong in [0,2]:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
from collections import deque
import itertools
from itertools import permutations
import sys
import bisect
sys.setrecursionlimit(10**6)
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YN=['YES','NO']
YNeos='YNeos'
mo=10**9+7
imp='IMPOSSIBLE'


n=I()
a=LI()
ans=1
c=[i for i in a]
c.sort()
def rp(i,j):
    b=[i for i in a]
    b[i],b[j]=b[j],b[i]
    return b
for i in range(n):
    for j in range(i,n):
        d=rp(i,j)
        if d==c:
            ans=0

print(YN[ans])

Submission status: Accepted

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

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

def solve(N: int, p: "List[int]"):
    p_sort = copy.deepcopy(p)
    p_sort.sort()

    n = 0
    for a,b in zip(p,p_sort):
        if a != b:
            n += 1

        if n > 2:
            print(NO)
            return

    print(YES)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

def ok(Qs):
    N = len(Qs)
    for i in range(N - 1):
        if Qs[i] > Qs[i + 1]:
            return False
    return True

if ok(Ps):
    print("YES")
else:
    flag = False
    for i in range(N - 1):
        for j in range(i + 1, N):
            Qs = Ps[:]
            Qs[i], Qs[j] = Qs[j], Qs[i]
            if ok(Qs):
                flag = True
    if flag:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
def solve(n, ps):
    qs = sorted(ps)
    count = sum(p != q for p, q in zip(ps, qs))
    return count == 0 or count == 2


def main():
    n = int(input())
    ps = [int(p) for p in input().split()]
    print("YES" if solve(n, ps) else "NO")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

flag = all(p[i] <= p[i+1] for i in range(n - 1))

for i in range(n):
	for j in range(i + 1, n):
		p[i], p[j] = p[j], p[i]
		if all(p[i] <= p[i+1] for i in range(n - 1)):
			flag = True
			break
		p[i], p[j] = p[j], p[i]
	
if flag:
	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(1,n+1):
  if p[i-1] != 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()))

a = 0
for i, p in enumerate(P):
    if i+1 != p:
        a += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

def judge(p, x, y):
  if x == 0:
    re = p[y]
  else:
    re = p[0]
  for i in range(N):
    if i == x:
      t = p[y]
    elif i == y:
      t = p[x]
    else:
      t = p[i]
    if re > t:
      return False
    re = t
  return True

for i in range(N):
  for j in range(i, N):
    if judge(p, i, j):
      print("YES")
      exit()
print("NO")

Submission status: Accepted

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

print(['NO', 'YES'][c == 0 or c == 2])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i, j in zip(p, sorted(p)):
    if i != j:
        c += 1

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

Submission status: Accepted

Solution code:

N = int(input())

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

q = p.copy()

q.sort()

now = 0

for i in range(N):

    if p[i] != q[i]:

        now += 1

if now == 2 or now == 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-1):
        if p[i]!=i+1:
                cnt+=1

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
from functools import reduce
import sys
import math
import bisect
import random
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return list(map(float, sys.stdin.readline().split()))
def LS():return sys.stdin.readline().split()
def SR(n): return [list(sys.stdin.readline())[:-1] for i in range(n)]
def IR(n): return [int(sys.stdin.readline()) for i in range(n)]
def LIR(n): return [list(map(int, sys.stdin.readline().split())) for i in range(n)]
def LSR(n): return [list(map(list, sys.stdin.readline().split())) for i in range(n)]
_gcd = lambda x, y: _gcd(y, x%y) if (x%y) else y
_lcm = lambda x, y: x*y // _gcd(x, y)
def gcd(*numbers):
    return reduce(_gcd, numbers)
def lcm(*numbers):
    return reduce(_lcm, numbers)
sys.setrecursionlimit(1000000)
INF = float('inf')
mod = 1000000007
dire4 = [(1,0), (0,1), (-1,0), (0,-1)]
dire8 = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]

N = I()
p = LI()

n = [i for i in range(1, N+1)]
tmp = 0
for pi, i in zip(p, n): 
    tmp += (not pi==i)
print("YES" if tmp<=2 else "NO")

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
import sys
MAX_INT = int(10e9)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()
aa = sorted(a)

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from math import ceil


def solve():
    N = int(input())
    P = list(map(int, input().split()))
    count = 0
    for i, p in zip(range(1, N + 1), P):
        if i != p:
            count += 1
        if count >= 3:
            break
    return 'YES' if count <= 2 else 'NO'


def main():
    print(solve())


if __name__ == "__main__":
    main()

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
    else:
        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()))
x=0
for i,p in enumerate(P):
    if i+1!=p:
        x+=1
a=1 if x==0 or x==2 else 0
print(['NO','YES'][a])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
  return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

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

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

Submission status: Accepted

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

Q = P.copy()
Q.sort()

n = [p == q for (p, q) in zip(P, Q)].count(False)
print('YES' if n <= 2 else 'NO')

Submission status: Accepted

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

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

cnt = 0
for i, a in enumerate(A):
  if a != x[i]:
    cnt += 1
if cnt <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(v) for v in input().split()]

count = 0
for i, p in enumerate(lst):
    if i + 1 != p:
        count += 1
    if count > 2:
        break

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

Submission status: Accepted

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
MOD = 10**9 + 7
sys.setrecursionlimit(10**7)
# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())
###template###

N = ii()
Ps = list(mi())

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

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

Submission status: Accepted

Solution code:



n=int(input())
a=list(map(int,input().split(" ")))

l=[]

for i in range(1,n+1):
	l.append(i)

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

Submission status: Accepted

Solution code:
ll= lambda : list(map(int,input().split()))
testcases=1
# testcases=int(input())
for testcase in range(testcases):
    [n]=ll()
    a=ll()
    a=[0]+a
    dif=0
    for i in range(1,n+1):
        if a[i]!=i:
            dif+=1
    if dif>2:
        print("NO")
    else:
        print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

if a == sorted(a):
  print('YES')
  raise SystemExit

for i in range(N):
  for j in range(i + 1, N):
    b = a[:]
    b[i], b[j] = b[j], b[i]
    if b == sorted(a):
      print('YES')
      raise SystemExit

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()))
diff = 0
for p,q in zip(P,list(range(1,N+1))):
    if p!=q : diff+=1

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

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

# n
# a, b 
# s = input()


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

r = p[:]
r.sort()

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

Submission status: Accepted

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

Submission status: Accepted

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

dif = 0

for i,p in zip(range(1,N+1),p):
	if i != p:
		dif += 1
	if dif > 2:
		print("NO")
		exit()
print("YES")