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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# https://atcoder.jp/contests/abc135/tasks/abc135_b

n = int(input())
p = [int(x) for x in input().split()]
q = sorted(p)
r = [p1 != q1 for p1, q1 in zip(p, q)]
print("YES" if r.count(True) == 2 or r.count(True) == 0 else "NO")

Submission status: Accepted

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

for i, j in zip(p, s):
    if i != j:
        count += 1

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

Submission status: Accepted

Solution code:
def swap_ij(xs, i, j):
    xs[i], xs[j] = xs[j], xs[i]


def main():
    N = int(input())
    p = [int(x) for x in input().split()]
    sorted_p = sorted(p)

    for i in range(N):
        for j in range(i, N):
            swap_ij(p, i, j)
            if p == sorted_p:
                print("YES")
                return
            else:
                swap_ij(p, i, j)

    print("NO")


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
p = LIST()

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

if acm <=2:
	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):
    d = int(p[i] != i + 1)
    c = c + d
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()))

success = True
change = 0

for i in range(N):
  if P[i] == i+1:
    continue

  else:
    if P[P[i]-1] == i+1:
      change  += 1
    else:
      success = False
      break

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

Submission status: Accepted

Solution code:
input()
p = list(map(int, input().split()))
a = 0
for i, p_ in enumerate(p):
  a += (p_ != (i + 1))
print('YNEOS'[a > 2 :: 2])

Submission status: Accepted

Solution code:
N = int(input())
p = tuple(map(int, input().split()))
print('YES' if sum(i != j for i, j in zip(p, range(1, N+1))) <= 2 else 'NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import math


def main():
    N = int(input())
    P = list(map(int, input().split()))
    tmp = [f != s for f, s in zip(P, sorted(P))]
    if sum(tmp) <= 2:
        print('YES')
    else:
        print('NO')


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().split()))
ss = list(i for i in range(1, n + 1))
cnt = 0
flag = False
for i in range(n):
    if s[i] == ss[i]:
        cnt += 1
    if cnt >= n - 2:
        flag = True
    else:
        flag = False

print("YES") if flag else print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for n, p in zip(range(1, N+1), P):
    if n != p:
        if count == 0:
            count += 1
            i = P.index(p)
            j = P.index(n)
            P[j] = P[i]
            P[i] = n
        else:
            print('NO')
            break
else:
    print('YES')

Submission status: Accepted

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

count = 0
for i, j in zip(p, pp):
    if i != j:
        count += 1
    if count == 3: break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq
N = int(input())
P = list(int(x) for x in input().split())
list = P.copy()
heapq.heapify(list)
count = 0

for i in range(N):
    if P[i] != heapq.heappop(list):
        count += 1

    if count == 3:
        break

if count == 3:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
# Code for B - 0 or 1 Swap
# Use input() to fetch data from STDIN
n = input()
a = [int(x) for x in input().split()]

b = sorted(a)

total = 0
for x, y in zip(a, b):
    if x != y:
        total += 1

if total >= 3:
    print("NO")
else:
    print("YES")

Submission status: Accepted

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

diff = 0
for ind, i in enumerate(p):
    if ind+1!=i:
        diff+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

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

Submission status: Accepted

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

v = sum(1 for i,j in zip(range(1, N+1), p) if i!=j)
print("YES" if v<=2 else "NO")

Submission status: Accepted

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

flag=0

for i in range(0,N-1):
    if i+1!=P[i]:
        for j in range(i+1,N):
            if j+1!=P[j]:
                if j!=N-1:
                    for k in range(j+1,N):
                        if k+1!=P[k]:
                            flag=1
                        break
if flag==0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

count = 0
for n in range(N):
    if P[n] != n+1:
        count += 1
    if count > 2:
        break

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

Submission status: Accepted

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

count = 0
for n in range(N):
    p_dif = P[n] - P_sorted[n]
    if p_dif != 0:
        count += 1
    if count > 2:
        break

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

Submission status: Accepted

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

for a in range(N):
    if list[a] != list_sort[a]:
        count += 1


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

Submission status: Accepted

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

b=sorted(a)

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

Submission status: Accepted

Solution code:
def solve(n, A):
    def check(L):
        for i in range(1, len(L)):
            if L[i] < L[i - 1]:
                return False
        return True
    if check(A):
        return 'YES'
    for i in range(n):
        for j in range(i + 1, n):
            if check(A[0:i] + [A[j]] + A[i + 1:j] + [A[i]] + A[j + 1:]):
                return 'YES'
    return 'NO'

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
pl = list(map(int, input().split()))
pl_sort = sorted(pl)

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

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
r=[i+1 for i in range(N)]
ans=False
for i in range(0,N-1):
    for j in range(i+1,N):
        p[i],p[j]=p[j],p[i]
        if p==r:
            ans=True
        #print(p)
        p[i], p[j] = p[j], p[i]
if p==r:
    ans = True
print('YES' if ans else 'NO')

Submission status: Accepted

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


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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
Nlist=list(range(1,N+1))

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

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

Submission status: Accepted

Solution code:
N = int(input())
p = input().split()
p = [int(num) for num in p]
q = sorted(p)
if p == q:
  print("YES")
else:
  t = 0
  for i in range(N):
    if p[i] != q[i]:
      t += 1
      #print(p[i],q[i],t)
    else:
      continue
  if t == 2:
    print("YES")
  else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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]"):
    sorted_p = sorted(p)
    ans = [i!=j for i,j in zip(p,sorted_p)]
    if sum(ans) <= 2:
        print(YES)
    else:
        print(NO)
    return


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Q=[i for i in range(1,N+1)]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())
l=list(map(int,input().split()))
t=[]
b=[]
for x in range(1,a+1):
    t.append(x)
for x in range(a):
    b.append( l[x]==t[x])
if b.count(True)==a:print("YES")
elif b.count(False)!=2:print("NO")
else:
    fi=b.index(False)
    li=b.index(False,fi+1)
    tmp=l[fi]
    l[fi]=l[li]
    l[li]=tmp
    b.clear()
    for x in range(a):
        b.append( l[x]==t[x])
    if b.count(True)==a:print("YES")
    else:print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
x = p.copy()
x.sort()
count = 0
for i in range(n):
    if x[i] != p[i]:
        count += 1
if count <=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

P=sorted(A)
cnt=0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

P=sorted(A)
cnt=0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import itertools
# import math

import sys
input = sys.stdin.readline


def solve(N, P):
    PP = sorted(P)
    cnt = 0
    for l1, l2 in zip(P, PP):
        if l1 != l2:
            cnt += 1

    return 'YES' if cnt <= 2 else 'NO'


N = int(input())
# S = input()
# A, B = map(int, input().split())
P = list(map(int, input().split()))

# P = []
# S = []
# for i in range(N):
#     pp, ss = map(int, input().split())
#     P.append(pp)
#     S.append(ss)

print(solve(N, P))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ll=sorted(l)
cnt=0
for l1,l2 in zip(l,ll):
  if l1 != l2:
    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()]

p_sorted = sorted(p)

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

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

Submission status: Accepted

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

sorted_p = sorted(p_i)
count = 0

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

q = [int(y) for y in range(1, N+1)]

count = 0

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:

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

srt = sorted(a)

res = "YES"
cnt = 0
for c,s in zip(a,srt):
    if c != s:
        cnt += 1 
    if cnt > 2:
        res = "NO"
        break
print(res)

Submission status: Accepted

Solution code:
#135 b 昇順だ
n=int(input())
p=list(map(int,input().split()))
ans="NO"
cnt=0
for i in range(n):
    if p[i]!=(i+1):
        cnt+=1
if cnt==2 or cnt==0:
    ans="YES"
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
p_asc = [i for i in range(1, n + 1)]
p_diff = [pi - p_asci for pi, p_asci in zip(p, p_asc)]
if (p_diff.count(0) == n) or (p_diff.count(0) == n - 2):
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Q = sorted(P)
ans = 0
for p, q in zip(P, Q):
    if p != q:
        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()))

p_sorted = sorted(p)
ans = [p[i]==p_sorted[i] for i in range(N)]
if ans.count(False) == 2 or ans.count(False) == 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:
        count += 1
if count == 0 or count == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

newlist = sorted(list)

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

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

Submission status: Accepted

Solution code:
N = int(input())

lstI = list(map(int, input().split()))
sortedList = sorted(lstI)

count = 0
for i in range(0, len(lstI)):
    if lstI[i] != sortedList[i]:
        count += 1

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

Submission status: Accepted

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

l=list(range(1,n+1))
cnt=0

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

Submission status: Accepted

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

P_sorted = sorted(P)
ans = 0
for i in range(N):
  if P[i] != P_sorted[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()))

t = [i for i in range(1,n+1)]
l = [i for i in range(n) if p[i] != t[i]]

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

Submission status: Accepted

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

if n == p:
	print("YES")
	exit()

count = 0
for i, j in zip(n,p):
	if i != j:
		count += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
buf=0
ans=p.copy()
count=0
list2=[]
#print(ans==pdash)
ans.sort()
#print(ans)
#print(ans==pdash)
flag=0

for i in range(N):
    if not(ans[i]==p[i]):
       count=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()))
a = 0
for i in range(n):
    if p[i] == i + 1:
        pass
    else:
        a += 1
if a <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

O = list(range(1, n + 1))
P = list(map(int, input().split()))

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
P = [int(e) for e 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:
a = int(input())
li = list(map(int,input().split()))
count = 0

for i in range(a):
  if li[i] != i+1:
    count += 1

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

Submission status: Accepted

Solution code:
def LI():
  return [int(s) for s in input().split()]

N = int(input())
p_s = LI()
p_s_sort = sorted(p_s)

diff_count = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ps = list(map(int, input().split()))
    sorted_ps = sorted(ps)

    cnt = 0
    for p, sp in zip(list(ps), sorted_ps):
        if p != sp:
            cnt += 1
            if cnt >= 3:
                print('NO')
                return
    print('YES')


main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
input()
l=list(map(int,input().split()))
print('YES' if sum(i!=j for i,j in zip(l,sorted(l)))<3 else 'NO')

Submission status: Accepted

Solution code:
input()
l = list(map(int, input().split()))
print('YES' if sum(i!=j for i, j in zip(l, sorted(l))) < 3 else 'NO')

Submission status: Accepted

Solution code:
input()
l = list(map(int, input().split()))
print('YES' if sum(i!=j for i, j in zip(l, sorted(l))) in (0, 2) else 'NO')

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=sorted(A)
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:
def main():
    n = int(input())
    num_list = list(map(int,input().split()))

    count = 0

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#一回入れ替える
for i in range(N):
    if not A_n[i] == i+1:
        #print("swap")
        j = A_n[i]-1
        A_n[i], A_n[j] = A_n[j], A_n[i]
        break

#print(A_n)
for i in range(N):
    if not A_n[i] == i+1:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

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

def solve():
  # 昇順にソートしたリストを生成
  p_sorted = sorted(p, reverse=False)
  cnt = 0
  for i in range(0, n):
    if p[i] != p_sorted[i]:
      cnt += 1

    if cnt >= 3:
      print("NO")
      return

  print("YES")
  return


if __name__ == "__main__":
  solve()

Submission status: Accepted

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

pp = sorted(p)
diff = [0 if p[i] == pp[i] else 1 for i in range(n)]
if (sum(diff) == 0 or sum(diff) == 2):
   print('YES')
else:
   print('NO')

Submission status: Accepted

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


if __name__=="__main__":
    main()

Submission status: Accepted

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

memo = []
for i, p in enumerate(P):
    if p != Q[i]:
        memo.append(p)

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

Submission status: Accepted

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

Submission status: Accepted

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

for a, b in zip(p, sorted_p):
    if a != b:
        count += 1
    if count > 2:
        break

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

Submission status: Accepted

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

count = 0
for p, s in zip(P, sorted(P)):
  if p != s:
    count += 1

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

Submission status: Accepted

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

a = range(1, n+1)
count = n

for i in range(n):
  if p[i]==a[i]:
    n -= 1

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

Submission status: Accepted

Solution code:
def main():
    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:
            break
    if cnt == 0 or cnt == 2:
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
"""
author : halo2halo
date : 9, Jan, 2020
"""

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, *P = map(int, read().split())

S = sum(x != y for x, y in enumerate(P, 1))
print('YES' if S <= 2 else 'NO')

Submission status: Accepted

Solution code:
"""
author : halo2halo
date : 9, Jan, 2020
"""

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N, *P = map(int, read().split())
P_N = sorted(P)

# print(list(x != y for x, y in zip(P, P_N)))

S = sum(x != y for x, y in zip(P, P_N))
# print(S)
print('YES' if S == 0 or S == 2 else 'NO')