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
l = list()

for i in range(N-1):
    if p[i] > p[i+1]:
        cnt += 1
        l.append(i)

if cnt > 2:
    print('NO')
if cnt == 1:
    print('NO')
if cnt == 0:
    print('YES')
if cnt == 2:
    p[l[0]], p[l[1]+1] = p[l[1]+1], p[l[0]]
    cnt = 0
    for i in range(N-1):
        if p[i] > p[i+1]:
            cnt += 1
            l.append(i)
    if cnt == 0:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(x) for x in input().split()]
 
count = 0
index = []
 
for i in range(n-1):
  if p[i] < p[i+1]:
    pass
  else:
    count += 1
    index.append(i)

if count == 0:
  print('YES')
elif count != 2:
  print('NO')
else:
  p[index[0]], p[index[1] + 1] = p[index[1] + 1], p[index[0]]
  sorted_p = sorted(p)
  if p == sorted_p:
    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 == 0 or count == 2:
	print('YES')
else:
	print('NO')

Submission status: Accepted

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

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

if cnt>2 or cnt==1:
    print("NO")
    exit()
if cnt==0:
    print("YES")
    exit()
else:
    for i in range(N-1):
        if p[i]>p[i+1]:
            a.append(p[i])
            a.append(p[i+1])
if a[0]>a[2] and a[1]>a[3]:
    print("YES")      
else:
    print("NO")

Submission status: Accepted

Solution code:
import logging

logging.basicConfig(level=logging.INFO, format="%(message)s")
#logging.disable(logging.CRITICAL)

def main():
    N = int(input())
    L = list(map(int, input().split()))
    logging.info("Hello!")

    L_sorted = sorted(L)

    counter = 0
    diff = []
    for i in range(N):
        if L[i] != L_sorted[i]:
            counter += 1
            if counter == 3:
                print("NO")
                exit()
            else:
                diff.append(L[i])

    print("YES")

            
if __name__ == "__main__":
    main()

Submission status: Accepted

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

sum_p = 0
count = 1

for i in p:
  if i == count:
    count += 1
  else:
    sum_p += 1
    count += 1

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

Submission status: Accepted

Solution code:
import math
import random # ランダムな配列の生成用

cnt  = 0

def quick_sort(arr):
    """ クイックソート 
    """
    global cnt

    if len(arr) < 2:
        # 1. 走査する配列長が0か１の場合戻る。
        return arr

    # 2. 走査する範囲の中央の要素をピボットとして選ぶ。
    pivot = math.floor(len(arr)/2)
    pivot_height = arr[pivot]
    left, right = 0, len(arr) - 1

    while(left < right):
        # 3. ピボットと比べ、大きい要素は左へ小さい要素は右へ交換する。
        while(arr[left] < pivot_height):
            # 左側の基準値より大きい位置まで移動
            left += 1
        while(arr[right] > pivot_height):
            # 右側の基準値より小さい位置まで移動
            right -= 1

        if (left < right):
            # leftがrightを超えていない場合、leftとrightを交換
            cnt += 1
            arr[left], arr[right] = arr[right], arr[left]
        else:
            break
    # 4. 左右２つに配列を分割してこの関数を再帰的に繰り返す。
    arr[:left] = quick_sort(arr[:left])      # ピボットの左側をクイックソート
    arr[left+1:] = quick_sort(arr[left+1:])  # ピボットの右側をクイックソート
    return arr


if __name__ == "__main__":
    s=input()	
    li = list(map(int,input().split()))	
    # 長さ10のランダムな配列

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int((input()))
S = input().split()
s=[]
b=[]
#print(S)
for i in range (N):
  s.append(int(S[i]))
#print(s)
for i in range(N):
  if s[i]==i+1:
    pass
  else:
    b.append(i)
#print(b)
if len(b)<3:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())

a = []
a_li = input().split()

for i in range(n):
    a.append(int(a_li[i]))
    
ans = "YES"
cnt = 0
cnt2 = 0

for i in range(n):
    if a[i] != i+1:
        cnt2 += 1
        if a[a[i]-1] != i+1:
            cnt += 1
            
    if (cnt >= 1) or (cnt2 >= 3):
        ans = "NO"
        break
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

p = [int(x) for x in input().split(' ')]
sorted_p = sorted(p)
count = 0
for i in range(N):
    if p[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())
ps = list(map(int, input().split()))


wrong_p = []
wrong_idx = []
for i in range(N):
    if ps[i] != i+1:
        wrong_idx.append(i)

if len(wrong_idx) == 0:
    print("YES")
elif len(wrong_idx) > 2:
    print("NO")
elif len(wrong_idx) == 1:
    print("NO")
else:
    if ps[wrong_idx[0]] == wrong_idx[1]+1:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from string import ascii_uppercase
import sys, bisect, math, heapq

stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
p = read_int()

def solve():
    diff_count = 0
    for i in range(N):
        if p[i] != i + 1:
            diff_count += 1
    if diff_count >= 3:
        return "NO"
    return "YES"

if __name__ == "__main__":
    print(solve())

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]"):

    if check(p):
        print("YES")
        return

    p1 = p.copy()
    if p[0] > p[1] and p[N-2] > p[N-1]:
        p1[0] = p[N-1]
        p1[N-1] = p[0]

    if check(p1):
        print("YES")
        return

    p2 = p.copy()
    current = p[0]
    for i in range(1, len(p)):
        if current > p[i]:
            p2[i-1] = p[i]
            p2[i] = p[i-1]
        current = p[i]

    if check(p2):
        print("YES")
        return

    p3 = p.copy()
    current = p[0]
    ind = 0
    hoge = [0] * 2

    for i in range(1, len(p)):
        if current > p[i]:
            if ind <= 1:
                hoge[ind] = i + ind - 1
                ind += 1
            else:
                print("NO")
                return
        current = p[i]

    if ind == 0 or ind == 1:
        print("NO")
        return

    p3[hoge[0]] = p[hoge[1]]
    p3[hoge[1]] = p[hoge[0]]
    if check(p3):
        print("YES")
        return
    else:
        print("NO")
        return



def check(p):
    # print(p)
    current = p[0]
    for i in range(1, len(p)):
        # print(current)
        if current > p[i]:
            return False
        current = p[i]

    return True

# 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:
import sys
N = int(input())
p = input()
list1 = list(map(int,p.split()))

list2 = []
for i in range(1,N + 1):
    list2.append(i)

if list1 == list2:
    print("YES")
    sys.exit()
for i in range(0,N):
    if list1[i] != i+1:
        if i+1 == list1[list1[i]-1]:
            list1[list1[i]-1] = list1[i]
            list1[i] = i+1
            

        if list1 == list2:
            print("YES")
            sys.exit()
        else:
            print("NO")
            sys.exit()

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    N = int(sys.stdin.readline())
    p = list(map(int, sys.stdin.readline().split()))
    s = sorted(p)

    flag =0
    for i in range(N):
        for j in range(i+1,N):
            l = p[:]
            temp = l[i]
            l[i] = l[j]
            l[j] = temp
            # print("p:",p)
            # print("l:",l)
            if l==s or p==s:
                flag=1
            l = p
            # print("re:",p2)

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:


def solve():
    N = int(input())
    p = [int(i) for i in input().split()]
    p_sort = sorted(p)
    count = 0
    for i in range(N):
        if p[i] != p_sort[i]:
            count += 1
        if count >= 3:
            break
    if count in [0, 2]:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(raw_input())
p = [int(i) for i in raw_input().split(' ')]
diff = [1 for a, b in zip(sorted(p), p) if a != b]
print 'YES' if len(diff) == 2 or len(diff) == 0 else 'NO'

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

from copy import copy

def resolve():
    N = [int(x) for x in sys.stdin.readline().split()][0]
    p_list = [int(x) for x in sys.stdin.readline().split()]

    

    is_broken = False
    old = p_list[0]
    for p in p_list[1:]:
        if old > p:
            is_broken = True
            break
        old = p

    if not is_broken:
        print("YES")
        return

    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            p_list_alt = copy(p_list)
            p_list_alt[i], p_list_alt[j] = p_list[j], p_list[i]

            is_broken = False
            old = p_list_alt[0]
            for p in p_list_alt[1:]:
                if old > p:
                    is_broken = True
                    break
                old = p

            if not is_broken:
                print("YES")
                return

    print("NO")

            
            

import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """5
5 2 3 4 1"""
        output = """YES"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """5
2 4 3 5 1"""
        output = """NO"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """7
1 2 3 4 5 6 7"""
        output = """YES"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """2
1 2"""
        output = """YES"""
        self.assertIO(input, output)
    def test_入力例_5(self):
        input = """2
2 1"""
        output = """YES"""
        self.assertIO(input, output)





if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

Submission status: Accepted

Solution code:
N = int(input())
arr = input().split(" ")

res = []

for i in range(1,N+1):
  if(int(arr[i-1]) != i):
    res.append(arr[i-1])

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

Submission status: Accepted

Solution code:
import string
while True:
    try:
        pd=0
        n=int(input())
        l=list(map(int,input().split()))
        L=l[:]
        L.sort()
        for i in range(n):
            for j in range(n):
                l[i],l[j]=l[j],l[i]
                if L==l:
                    pd=1
                    break
                else:
                    l[i], l[j] = l[j], l[i]
            if pd==1:
                break
        if pd==1:print("YES")
        else:print("NO")
    except:break

Submission status: Accepted

Solution code:
import numpy as np
import math
N =int(input())
c=list(map(int, input().split())) 
a = np.arange(1,N+1)
#print(sorted(c-a))
d = sorted(c-a)
if (abs(d[0])-abs(d[N-1])==0) and (np.count_nonzero(d)==2):
    print('YES')
elif (np.count_nonzero(d)==0):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
from sys import stdin

readline = stdin.readline


def main():
    _, *p = map(int, open(0).read().split())
    count = 0
    sp = sorted(p)
    for a, b in zip(sp, p):
        if a != b:
            count += 1
    print('YES' if count <= 2 else 'NO')


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for i in range(n):
    minj = i
    for j in range(i+1, n):
        if p[j] < p[minj]:
            minj = j
    t = p[i]
    p[i] = p[minj]
    p[minj] = t
    if i != minj:
        counter += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
count = 0
for bp, ap in zip(p, sorted(p)):
    if bp != ap:
        count += 1

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

S = input().split()

array = np.zeros(N)

counter = 0

for i in range(len(S)):
    array[i] = int(S[i])

for i in range(len(S) - 1):
    check = np.min(array[i + 1:])
    if(array[i] > check):
        swap = array[np.argmin(array[i + 1:]) + i + 1]
        array[np.argmin(array[i + 1:]) + i + 1] = array[i]
        array[i] = swap
        counter = counter + 1

if(counter <= 1):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
N = list(map(int, input().split()))
cnt = 0
for i in range(0, len(N)-1):
    syou = N[i]
    syounum = i
    for j in range(i+1,len(N)):
      if syou > N[j]:
        syounum = j
        syou = N[j]
    if syounum != i:
      cnt+=1
      tmp = N[i]
      N[i] = N[syounum]
      N[syounum] = tmp
if cnt <= 1:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import copy
def main():
    N = input()
    array1 = list(map(int, input().split()))
    array2 = copy.deepcopy(array1)
    array1.sort()
    n = 0
    for c1,c2 in zip(array1,array2):
        if c1 != c2:
            n += 1
            if n == 3:
                print("NO")
                return
    print("YES")
    return
main()

Submission status: Accepted

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

q=sorted(p)
cnt=0

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
ANS = list(range(1,N+1))
count = 0 
dif = list()

for i in range(N):
	if ANS[i] != P[i]:
		count += 1 
		dif.append(P[i])

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

Submission status: Accepted

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

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

if p==label:
    print("YES")
else:
    import numpy as np
    a=np.array(p)
    b=np.array(label)
    if np.sum(a==b)==n-2:
        index=np.where(a!=b)
        ia=p[index[0][0]]
        ib=p[index[0][1]]
        p[index[0][1]]=ia
        p[index[0][0]]=ib
        if label==p:
            print("YES")
        else:
            print("NO")
    else:
        print("NO")

Submission status: Accepted

Solution code:
n = int(input())
pn = []
pn = [int(x) for x in input().split()]
 
miss = 0
ok = True
for i in range(0,n):
    if i != pn[i]-1:
        if miss == 0:
            miss = pn[i]-1
        else:
            if miss != i:
                ok = False
                break
 
if ok:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
P = list(map(int,input().split()))
dic ={}
for i, num in enumerate(P):
    i+=1
    if num !=i:
        dic[num]=i
if len(dic)>2:
    print('NO')
    exit()
a = list(dic.keys())
b = list(dic.values())
if len(dic)==0 or (a[0]==b[1] and a[1]==b[0]):
    print('YES')

Submission status: Accepted

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

N = P[:]
N.sort()

cnt = 0
for i, j in zip(N, P):
    if i != j:
        cnt += 1

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

Submission status: Accepted

Solution code:
import numpy as np

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

flip = np.sum(P!=np.arange(1, 1+N))
if flip<=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

N=int(N)

k=0

for i in (range(N)):
    for j in (range(N)):
        p[i],p[j]=p[j],p[i]
        if p == sorted(p):
            k=1
            break
        p[i],p[j]=p[j],p[i]
    if k==1:
        break

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

Submission status: Accepted

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

if gap.count(0)== n-2 or gap.count(0) ==n:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
N = int(input())
p = [int(i) for i in input().split()]
count = 0
candidates =[]
for i in range(N):
    if p[i] == i+1:
        continue
    else:
        candidates.append([i+1,p[i]])
if len(candidates) == 0:
    print("YES")
elif len(candidates) ==1:
    print("NO")
elif len(candidates) ==2:
    if candidates[0][0] == candidates[1][1] and candidates[0][1] == candidates[1][0]:
        print("YES")
    else:
        print("NO")
else:
    print("NO")

Submission status: Accepted

Solution code:
# ' 'dで区切ってprintする方法
# print(*a, sep=' ')

import math

n = int(input())

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

not_same = 0

sorted_p = sorted(p)

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

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

Submission status: Accepted

Solution code:
n = int(input())
cl = [i for i in range(1,n+1)]
pl = [int(i) for i in input().split()]
for i in range(1,n+1):
    if not i == pl[i-1]:
        tmp=pl[i-1]
        pl[i-1]=pl[tmp-1]
        pl[tmp-1]=tmp
        break
if cl==pl:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())

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

ref = [i for i in range(1, len(p) + 1)]

res = 0
for i,v in enumerate(p):
    if ref[i] != v:
        res += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

comp_arr = []
for i in range(n):
    comp_arr.append(i+1)

sub_num = 0

for i in range(n):
    if comp_arr[i] != arr[i]:
       sub_num = sub_num + 1

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

Submission status: Accepted

Solution code:
input()
p=list(map(int,input().split()))
t=0
for m,n in zip(p,sorted(p)):
	if m!=n: t+=1
print(("NO","YES")[t<3])

Submission status: Accepted

Solution code:
import copy

n = int(input())
data = list(map(int, input().split()))
s_data = copy.deepcopy(data)
s_data.sort()
c = 0
for i in range(n):
    if data[i] != s_data[i]:
        c += 1

if c == 2 or c == 0:
    # 入れ替えたときちゃんと合うか
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

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

l = [p[n]-num[n] for n in range(N)]
l2 = [j for j in l if j!=0]
if len(l2) == 2:
    if sum(l2) == 0:
        print('YES')
elif len(l2) == 0:
    print('YES') 
else:
    print('NO')

Submission status: Accepted

Solution code:

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

sorted_p = sorted(p)

# tmp = p[0]
# tmp_index = 0

# for i in range(N):
#     for j in range(N-i-1):
#         if j == 0:
#             continue
#         else:
#             if p[i] > p[i+j]:
#                 tmp = p[i]
#                 p[i] = p[i+j]
#                 p[i+j] = tmp
#
#                 count = True
#
#     # if count:
#     #     break


count = 0
for b in range(N):
    if not(p[b] == sorted_p[b]):
        t_index = p.index(sorted_p[b])

        tmp = p[b]
        p[b] = p[t_index]
        p[t_index] = tmp

        count += 1

# print(p)
# print(sorted_p)

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

Submission status: Accepted

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

sw = []
for i in range(N-1):
  if p[i]>p[i+1]:
    sw.append(i)

if len(sw)==0:
  print('YES')
  
if len(sw)==1:
  if (sw[0]>0 and p[sw[0]-1]>p[sw[0]+1]) or (sw[0]<N-2 and p[sw[0]]>p[sw[0]+2]):
    print('NO')
  else:
    print('YES')
if len(sw)==2:
  if (sw[0]>0 and p[sw[0]-1]>p[sw[1]+1]) or (sw[1]<N-2 and p[sw[0]]>p[sw[1]+2]):
    print('NO')
  else:
    print('YES')
if len(sw)>2:
  print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = list(p)
q.sort()
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())
nums = list(map(int, input().split()))
ans = sorted(nums)
if nums == ans:
    print("YES")
else:
    count = 0
    for i, j in enumerate(nums, 1):
        if i != j:
            count += 1
            if count >= 3:
                print("NO")
                break
    else:
        print("YES")

Submission status: Accepted

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

def check_abc(T):
    c = 0
    a = 0
    for t in T:
        if t < a:
            c += 1
        a = t
    if c == 0:
        print('YES')
        exit()

check_abc(P)
for i in range(N - 1):
    for j in range(i + 1, N):
        T = []
        for p in P:
            T.append(p)
        T[i] = P[j]
        T[j] = P[i]
        check_abc(T)

print('NO')

Submission status: Accepted

Solution code:
n = input()
n = int(n)
ps = list(map(int, input().split()))
pp = sorted(ps)
remaind = 2
pt = False
for s,p in zip(ps,pp):
    if s != p:
        remaind -= 1

if remaind >= 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

# print(p)
p_sorted = sorted(p)
# print(p_sorted)

haming = 0
ans = "YES"
for ori_p, sort_p in zip(p, p_sorted):
    if ori_p != sort_p:
        haming += 1
    if haming > 2:
        ans = "NO"
        break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
ct=0
ct2=0
out=[]
for i in range(n-1):
    if p[i]>p[i+1]:
        ct+=1
        out.append(i)
if ct>2:print('NO')
elif ct==0:print('YES')
elif ct==1:
    p[out[0]],p[out[0]+1]=p[out[0]+1],p[out[0]]
    for i in range(n-1):
        if p[i]>p[i+1]:
            ct2+=1
    if ct2==0:print('YES')
    else:print('NO')
else:
    p[out[0]],p[out[1]+1]=p[out[1]+1],p[out[0]]
    for i in range(n-1):
        if p[i]>p[i+1]:
            ct2+=1
    if ct2==0:print('YES')
    else:print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

def solve(N, A):
    problems = []
    for i, x in enumerate(A):
        if x != i+1:
            problems.append(i)
    if len(problems) < 2: return True
    if len(problems) > 2: return False
    i0, j0 = problems
    A[i0], A[j0] = A[j0], A[i0]
    return all(A[k] < A[k+1] for k in xrange(N-1))

print "YES" if solve(N, A) else "NO"

Submission status: Accepted

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

Submission status: Accepted

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

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]"):
    count = 0

    for i, count_down in enumerate(range(1, N)):

        if not count_down == p[i]:
            count += 1

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

    return


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

p_sort=sorted(info)
tem_1=0
tem_2=0
tem=0
for i in range(n-1):
  if info[i]>info[i+1]:
    tem_1=i
    for j in range(n-i-2):
      if info[i+j+1]>info[i+j+2]:
        tem_2=i+j+2
        break
    break
tem=info[tem_1]
info[tem_1]=info[tem_2]
info[tem_2]=tem


for i in range(n):
  if info[i]!=p_sort[i]:
    print("NO")
    break
  
  if i==n-1:
    print("YES")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

n_slist = sorted(range(n+1)[1:])

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
Q = list(range(1,N+1))
J = [x - y for (x, y) in zip(P, Q)]
R = [i for i in J if i != 0]
if len(R)<=2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
N = input()
r = input()

data = [int(i) for i in r.split(" ")]
sorted_data = sorted(data)

diff_count = 0
for i in range(0, len(data)):
    if data[i] != sorted_data[i]:
        diff_count += 1

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

Submission status: Accepted

Solution code:
num = int(input())

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

l_sort = sorted(l)


a = 0

for x, y in zip(l, l_sort):
  if x != y:
    a += 1
    
if a == 0 or a == 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

ps = sorted(p)

not_match = [x != y for x, y in zip(p, ps)].count(True)

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

Submission status: Accepted

Solution code:
import itertools

N = int(input())
PS = list(map(int, input().split()))
PSS = sorted(PS)

RES ='NO'
if PS != PSS:
    for i, j in itertools.combinations(range(N), 2):
        PC = PS[:]
        PC[j], PC[i] = PC[i], PC[j]
        if PSS == PC:
            RES = 'YES'
            break
else:
    RES ='YES'

print(RES)