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 = [int(i) for i in input().split()]

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

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

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
pp = p[:]
p.sort()
count = 0

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

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

Submission status: Accepted

Solution code:
N = int(input())

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


cnt = 0

s_1 = 0
for i in range(N):
    if P[i] != i + 1:
        if cnt == 1:
            print('NO')
            exit()

        s_1 = i

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

                v = P[s_1]
                P[s_1] = P[s_2]
                P[s_2] = v
                cnt += 1
                break
            else:
                continue

print('YES')

Submission status: Accepted

Solution code:
import copy

N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst)
sum = 0
for i in range(N):
  if (lst[i] != lst2[i]):
    sum += 1
if (sum == 0 or sum == 2):
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
c = list(filter(lambda i: a[i]!=b[i], range(n)))
print('YES' if len(c) in [0,2] else 'NO')

Submission status: Accepted

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

sabun = [P[i]-P_t[i] for i in range(N)]
c = N - sabun.count(0)
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()))
import copy
new_p=sorted(p)
cnt=0
a=[]
for i in range(N):
    if p[i]==new_p[i]:
        pass
    else: 
        cnt+=1
        a.append(i)
if cnt==2:
    newnew_p=copy.copy(p)
    newnew_p[a[0]]=p[a[1]]
    newnew_p[a[1]]=p[a[0]]
    if newnew_p==new_p:
        print("YES")
    else: print("NO")
elif cnt==0:
    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 x, y in zip(p, q):
    if x != y:
        count += 1
    else:
        pass

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

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
P=np.array(input().split(),dtype=np.int32)


if (np.sort(P) == P).all():
    print("YES")
else:
    sort_P=np.sort(P)
    count=0
    for i in range(N):
        if sort_P[i]!=P[i]:
            count+=1
    if count==2:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
A_=sorted(A)
if A==A_:
  print("YES")
  exit()
for i in range(N):
  for j in range(i):
    B=A[::]
    B[i],B[j]=B[j],B[i]
    if B==A_:
      print("YES")
      exit()
print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l_a = list(map(int,input().split()))
l_b = sorted(l_a)
if l_a[0] != 1:
    k = l_a.index(1)
    l_a[0],l_a[k] = l_a[k],l_a[0]
else:
    for i in range(1,N):
        if l_a[i] != l_a[i-1]+1:
            k = l_a.index(i+1)
            l_a[i],l_a[k] = l_a[k],l_a[i]
            break
if l_a == l_b:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

mis_match = sum([ 1 for s in p if not p.index(s) +1 == s])

judge="NO"
if mis_match in [0,2]:
    judge="YES"
print(judge)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# input
n = int(input())
a = list(map(int, input().split()))
c = o = 0
for i in range(n):
    if a[i] != i+1 and c == 0:
        c = 1
        o = a[i]
        if a[o-1] != i+1:
            print('NO')
            exit()
    elif a[i] != i+1 and c == 1:
        if i+1 != o:
            print('NO')
            exit()
print('YES')

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#coding : utf-8

import os
import sys

N = int(input())

p = list(map(lambda x: int(x), input().split(" ")))

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

cnt = 0

for i in range(N):
	if p[i] != q[i]:
		cnt += 1
		if cnt > 2:
			break

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    A = list(map(int,input().split()))
    count = 0
    #print(A)
    for i in range(N):
        if (i+1) != A[i]:
            count += 1
    if count==0 or count == 2:
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

sp = sorted(p)
mes = ""
cnt = 0

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

Submission status: Accepted

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

flag = 0
for i in range(n):
    for j in range(i+1, n):
        p[i], p[j] = p[j], p[i]
        for k in range(n):
            for l in range(k+1, n):
                if p[k] > p[l]:
                    flag = 1
                    break
        if flag == 0:
            print("YES")
            exit()
        p[i], p[j] = p[j], p[i]
        flag = 0

flag = 0
for i in range(n):
    for j in range(i+1, n):
        if p[i] > p[j]:
            flag = 1
if flag:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
N = int(input())
s = list(map(int, input().split()))
index1 = 0
value1 = 0
index2 = 0
value2 = 0
count = 0

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc135/tasks/abc135_b
import numpy as np


def calc(N: int, values: np.ndarray) -> bool:
    sorted_values = np.sort(values)
    difference = sorted_values != values
    difference_count = difference.sum()
    return difference_count == 0 or difference_count == 2


def load_input_as_int_array() -> np.ndarray:
    return np.array([int(v) for v in input().split(" ")])


def main() -> None:
    N = load_input_as_int_array()
    values = load_input_as_int_array()
    result = calc(N, values)
    result_str = "YES" if result else "NO"
    print(result_str)


main()

Submission status: Accepted

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

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

Submission status: Accepted

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

tmp = N + 1
count = 0
ret = 1
for i in range(N):
    if p[i] == i + 1:
        continue
    else:
        count += 1
        if count == 3:
            ret = 0
            break
        elif count == 2:
            if tmp == i + 1:
                ret = 1
            else:
                ret = 0
                break
        tmp = p[i]

if ret == 1:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

cnt = 0
for p, rp in zip(P, sorted(P)):
    if p != rp:
        cnt += 1
if cnt <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

r_count = 0
target_index_dict = {}
for i, number in enumerate(p_list):
    if i+1 != number:
        r_count += 1
        target_index_dict[i] = number

if not target_index_dict:
    ans = 'YES'
else:
    if len(target_index_dict) >= 3:
        ans = 'NO'
    else:
        a_key = list(target_index_dict.keys())[0]
        a_value = target_index_dict[a_key]
        b_key = list(target_index_dict.keys())[1]
        b_value = target_index_dict[b_key]
        if (a_key+1 == b_value) and (b_key+1 == a_value):
            ans = 'YES'

print(ans)

Submission status: Accepted

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

sort = sorted(ls)

count = 0
for i in range(N):
  if sort[i] == ls[i]:
    count += 1

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

Submission status: Accepted

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

up = sorted(p)
cntup = 0
for i in range(n):
  if(up[i] == p[i]):
    cntup += 1

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

Submission status: Accepted

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

count = 0

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

Submission status: Accepted

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

s = sorted(l)

ret = []
for i in range(len(l)):
    if l[i] != s[i]:
        if len(ret) >= 2:
            print("NO")
            exit(0)

        ret.append(i)

le = len(ret)
if le == 0 or  le == 2:
    print("YES")
    exit(0)

print("NO")

Submission status: Accepted

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

a = range(1, N+1)

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

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

Submission status: Accepted

Solution code:
#ABC 135, B
n = int(input())
P = [int(el) for el in input().split(' ')]

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

P_sorted = sorted(P)


if P == P_sorted:
    print("YES")
else:
    count = 0
    for a, b in zip(P, P_sorted):
        if a != b:
            count += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
A = [int(i) for i in input().split()]
B = list(sorted(A))
if A == B:
    print("YES")
    sys.exit()
for i in range(N):
    for j in range(i,N):
        if i == j:
            continue
        A[i],A[j]=A[j],A[i]
        if A == B:
            print("YES")
            sys.exit()
        A[i],A[j]=A[j],A[i]
print("NO")

Submission status: Accepted

Solution code:
def checkSorted(n,l):
    f=0
    s=0
    count=0
    for i in range(1,n):
        if l[i]<l[i-1]:
            count+=1
            if f==0:
                f=i
            else:
                s=i
    if count>2:
        return 0
    if count==0:
        return 1
    if count==2:
        l[f-1],l[s]=l[s],l[f-1]
    elif count==1:
        l[f-1],l[f]=l[f],l[f-1]
    for i in range(1,n):
        if l[i]<l[i-1]:
            return 0
    return 1
n=int(input())
l=list(map(int,input().split()))
if checkSorted(n,l)==1:
    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+1)[1:]:
  if i != a[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()))
ps=sorted(p)
cnt=0
for i in range(N):
    if p[i]==ps[i]:
        cnt+=1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys;
import string;
sys.setrecursionlimit(20000) # 再起呼び出しの最大数をセット

import copy
import math
import itertools

from functools import lru_cache
#@lru_cache(maxsize = None)

PPP = 1000000007

# 整数の入力
n = int(input())

# スペース区切りの入力
#n, x = input().split()

# スペース区切りの整数の入力
#a, b = map(int, input().split())
#n, x = map(int, input().split())
#print("sp", n, k, file=sys.stderr)
p = [int(x) for x in input().split()]
#b = [int(x) for x in input().split()]
#print("a", a, file=sys.stderr)
#p, q, r = map(int, input().split())
#s = [int(x) for x in input().split()]
#t = [int(x) for x in input().split()]

# 改行区切りの整数の入力
#a = []
#for x in range(n):
#  a.append(int(input()))
#  a.append([int(x) for x in input().split()])
#b = []
#for x in range(m):
#  b.append([int(x) for x in input().split()])
#p = [int(x) for x in input().split()]
#a.sort()
#print(l, file=sys.stderr)

# 文字列の入力
#s = input()

# 出力
#print("{} {}".format(a+b+c, s))
#a.sort()
#print(set(a), file=sys.stderr)
#print(len(set(a)))

#cache = dict()

import re
import fractions

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def ton(n, k, x):
  s = ""
  while n > 0:
    s = str(n%k) + s
    n //= k
  return s


#print("a", a, file=sys.stderr)
#print("b", b, file=sys.stderr)

def getEnergy():
  for y in b:
    yield y

def doit():
  ng = 0
  for i in range(n):
    if i + 1 != p[i]:
      ng += 1
  
  print("", ng, file=sys.stderr)
  return ng == 0 or ng == 2

def yn():
  if doit():
    return "YES"
  else:
    return "NO"

#print(doit([0] * 4))
#doit(a)
#print(doit())
try:
  #doit()
  #print(doit())
  print(yn())
except Exception as e:
  print("e", e, file=sys.stderr)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
p_list = list(map(int, input().split()))

count = 0
for i in range(n):
    if p_list[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()))
p_sort = sorted(p)
flag = False

for i in range(n):
    for j in range(n):
        p_tmp = p[:]
        tmp = p_tmp[i]
        p_tmp[i] = p_tmp[j]
        p_tmp[j] = tmp
        match = True
        for k in range(n):
            if p_tmp[k] != p_sort[k]:
                match = False
        if match:
            flag = True
            break
    if flag:
        break

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

Submission status: Accepted

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

c = 0
for i in range(N):
  if p[i] != a[i]:
    c += 1
if c <= 2 :
  print("YES")
if c > 2:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    p = list(map(int,input().split()))
    neoP = sorted(p)
    cnt = 0
    for i in range(len(p)):
        if p[i] != neoP[i]:
            cnt += 1

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

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 整数の入力
N = int(input())
# スペース区切りの整数の入力
p = [int(x) for x in input().split()]


# 1からNまでの昇順リスト
correct = range(1, N+1)
# カウンタ
count = 0


# リストpとcorrectを比較する
for i in range(len(p)):
  if p[i] != correct[i]:
    count += 1


# カウンタを確認、3以上だったらNo
print('NO') if count >= 3 else print('YES')

Submission status: Accepted

Solution code:
import numpy as np

N =  int(input())
num_arr = np.array(list(map(int, input().split())))
right_num_arr = np.array(list(range(1,N+1,1)))

dif = [abs(i) for i in num_arr - right_num_arr]

if dif == [0]*N:
  print("YES")
elif (sorted(set(dif))[0] == 0)&(len(set(dif))==2):
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

flag = 0
tmp = 0
for i in range(1, n + 1):
    if i != lst[i-1]:
        tmp = lst[i-1]
        flag += 1
        if flag > 2:
            print("NO")
            exit()
    

print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

if __name__ == '__main__':
    n = int(input())
    p = [int(i) for i in input().split()]
    p2=copy.copy(p)
    p2.sort()

    count=0
    p12=[]
    p22=[]
    for i in range(n):
        if p[i] != p2[i]:
            count = count+1
            p12.append(p[i])
            p22.append(p2[i])
    if (count == 2 and p12[0]==p22[1] and p12[1] == p22[0]) or count==0:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
a=int(input())
l=list(map(int,input().split()))
ok=False
if sorted(l) == l:
    print("YES")
    exit(0)
for i in range(len(l)-1):

    for j in range(i+1,len(l)):
        aux=l[i]
        l[i]=l[j]
        l[j]=aux
        if sorted(l) == l:
            ok=True
        else:
            aux = l[j]
            l[j] = l[i]
            l[i] = aux
    if ok == True:
        print("YES")
        break
if ok ==False:print("NO")

Submission status: Accepted

Solution code:
INF = 100
n = int(input())
a = list(map(int, input().split()))
sorted_a = sorted(a)
count = 0
p1 = INF
p2 = INF
for i in range(n):
    if a[i] != sorted_a[i]:count+=1
    if p2==INF and p1!=INF:p2=i
    elif p1==INF:p1=i
if count < 2:
    print("YES")
    exit()
elif count == 2:
    temp = a[p1]
    temp2 = a[p2]
    rea = a
    rea[p1] = temp2
    rea[p2] = temp
    if a == rea:print("YES")
    exit()
print('NO')

Submission status: Accepted

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

s = sorted(p)
d = len([1 for i in range(n) if p[i] != s[i]])

if d in [0, 2] :
    print("YES")
else :
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

p.insert(0, 0)

wrong_list = []
for i in range(1,N+1):
  if p[i] != i:
    wrong_list.append([i,p[i]])

if len(wrong_list) == 0:
  print("YES")
elif len(wrong_list) != 2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
import numpy as np
p = np.array(list(map(int, input().split())))
ps = np.array(sorted(p))
s = np.sum(p!=ps)
if s==0 or s==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 a, b in zip(p, p_sort):
  if a != b:
    cnt += 1

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

Submission status: Accepted

Solution code:
import copy
n=int(input())
p=list(map(int,input().split()))
p_copy=copy.copy(p)
p.sort()
for i in range(n):
  for j in range(i+1,n):
    p_copy[i],p_copy[j]=p_copy[j],p_copy[i]
    if p==p_copy:
      break
    else:
      p_copy[i],p_copy[j]=p_copy[j],p_copy[i]
  if p==p_copy:
    break
if p==p_copy:
  print('YES')
else :
  print('NO')

Submission status: Accepted

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

counter = 0
for pair in zip(p_list, range(1, len(p_list) + 1)):
    if pair[0] - pair[1] != 0:
        counter += 1

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

Submission status: Accepted

Solution code:
N = int(input())
list1 = list(map(int, input().split()))
list2 = list1.copy()
check_l = []
for i in range(len(list1)):
    if (i+1) != list1[i]:
        check_l.append(i)
if len(check_l) <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = map(int, input().split())
P = list(map(int, input().split()))
PP = sorted(P)
count = 0
for n, p in enumerate(P):
    if p != PP[n]:
        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()))
w=0
for i in range(N):
	if P[i]!=i+1:
		w+=1
else:
	print("NO" if w>2 else "YES")

Submission status: Accepted

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

for i,j in zip(p, q):
    if i == j:
        continue
    else:
        count += 1
    if count > 2:
        print('NO')
        exit()
print('YES')

Submission status: Accepted

Solution code:
import sys
import collections
import copy
input = sys.stdin.readline
N = input()
P= list(int(i) for i in input().split())
chk = 0
col = []
for i in range (len(P)):
	if P[i] != i+1 :
		chk +=1
if chk ==2 or chk ==0:
	print("YES")
else:
	print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

flg = False
if sp == p:
    print("YES")
else:
    for i in range(n):
        for j in range(i+1, n):
            cp = p.copy()
            cp[i], cp[j] = cp[j], cp[i]
            if cp == sp:
                print("YES")
                flg = True
                break
        if flg:
            break
    if not flg:
        print("NO")

Submission status: Accepted

Solution code:
import copy

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

flag = 0


# 操作不要の場合
if p_list == sorted(p_list):
  print("YES")
  flag = 1
  
# 1回だけ操作
if flag == 0:
  for i in range(N):
    for j in range(i, N):
      # print(i, j)
      new_p_list = copy.deepcopy(p_list)
      tmp = new_p_list[j]
      new_p_list[j] = new_p_list[i]
      new_p_list[i] = tmp
      # print(new_p_list)
      # print(sorted(p_list))
      if new_p_list == sorted(p_list):
        print("YES")
        flag = 1
        break
      
if flag == 0:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = [int(x) for x in input().split()]
count = 0
for i in range(N-1):
    if p[i] == i + 1:
        continue
    else:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

count = 0

for i in range(n):
  if ls[i] == i+1:
    continue
  else:
    count +=1
    if count >=2:
      break
    if ls[ls[i]-1]==i+1:
      ls[ls[i]-1] = ls[i]
      ls[i] = i+1

    else:
      count+=1

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

Submission status: Accepted

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

not_equal_count=0
for i in range(N):
  if P[i] != Q[i]:
    not_equal_count += 1
if not_equal_count in [0, 2]:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import copy

n = int(input())

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

p_list = copy.deepcopy(p_list_org)

p_list.sort()

cnt = 0
for i in range(n):
    if p_list[i] != p_list_org[i]:
        cnt += 1
    else:
        pass

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

Submission status: Accepted

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

flag = True
swap_count = 0
for i in range(N):
    if p[i] != i+1:
        if swap_count == 0:
            j = p[i] - 1
            tmp = p[j]
            p[j] = p[i]
            p[i] = tmp
            swap_count += 1
        else:
            flag = False
            break

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

Submission status: Accepted

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

for i in range(N):
  for j in range(i,N):
    A[i],A[j]= A[j],A[i]
    for k in range(N):
      if A[k]!= B[k]:
        break
    else:
      print('YES')
      exit()
    A[i],A[j]= A[j],A[i]
        
print('NO')

Submission status: Accepted

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

sorted_list = sorted(num_list)
false_count = 0
for i, j in zip(num_list, sorted_list):
  if i != j:
    false_count += 1
    if false_count > 2:
      print('NO')
      break

if false_count <=2:
	print('YES')

Submission status: Accepted

Solution code:
sentence = input()
N = int(sentence)
sentence = input()
p_str = sentence.split()
p_list = []
previous = -1
need_move = []
for i, p in enumerate(p_str):
    present = int(p)
    p_list.append(present)
    if previous > present:
        need_move.append(i)
        need_move.append(i-1)
    previous = present
ans_ok = "YES"
ans_no = "NO"
# 昇順ならyes
if not need_move:
    print(ans_ok)
    exit()
if len(need_move) > 4:
    print(ans_no)
    exit()
while len(need_move) > 1:
    move_index = need_move.pop(0)
    for move_index_2 in need_move:
        # 入れ替えてみて昇順か確かめる
        before_number = p_list[move_index]
        after_number = p_list[move_index_2]
        p_list[move_index] = after_number
        p_list[move_index_2] = before_number
        ok = True
        pre = -1
        for p in p_list:
            if p < pre:
                ok = False
                break
            pre = p
        # ok
        if ok:
            print(ans_ok)
            exit()
        p_list[move_index] = before_number
        p_list[move_index_2] = after_number
print(ans_no)

Submission status: Accepted

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

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

Submission status: Accepted

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

q = sorted(p)
#print(q)

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:
# coding: utf-8

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

cnt = sum([1 if a!=b else 0 for a,b in zip(p, range(1, N+1))])


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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
p = [int(i) for i in input().split()] 
a = np.zeros(N-1)
b = a
c = 0
d = 0
for j in range(N-1):
  a[j] = p[j+1] - p[j]
b = sorted(a)
if b[0] > 0:
  print('YES')
elif b[2] > 0 and b[0] == b[1]:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import copy

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

sortedP = copy.copy(p)
sortedP.sort()

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())  #数値入力 「N」だけの入力のとき
#n= 5  #数値入力 「N」だけの入力のとき
c=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
#c=[5, 2, 3, 4, 1]
d=[i for i in range(1,n+1)]
f=0
for i in range(n):
    if c[i]-d[i]:
        f+=1
#print(f)
if f == 0 or f == 2:
    print("YES")
else:
    print("NO")