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:
def main():

    """ convenient functions
    # for i, a in enumerate(iterable)
    # q, mod = divmod(a, b)
    # divmod(x, y) returns the tuple (x//y, x%y)
    # Higher-order function: reduce(operator.mul, xyz_count, 1)
    # manage median(s) using two heapq https://atcoder.jp/contests/abc127/tasks/abc127_f
    """

    """convenient decorator
    # @functools.lru_cache():
    # to facilitate use of recursive function
        # ex:
        # from functools import lru_cache
        # import sys
        # sys.setrecursionlimit(10**9)
        # @lru_cache(maxsize=None)
        # def fib(n):
        #     if n < 2:
        #         return n
        #     return fib(n-1) + fib(n-2)
        # print(fib(1000))
    """
    
    # import numpy as np
    import sys
    sys.setrecursionlimit(10**7)
    from itertools import accumulate, combinations, permutations, product # https://docs.python.org/ja/3/library/itertools.html
    # accumulate() returns iterator! to get list: list(accumulate())
    from math import factorial, ceil, floor, sqrt
    def factorize(n):
        """return the factors of the Arg and count of each factor
        
        Args:
            n (long): number to be resolved into factors
        
        Returns:
            list of tuples: factorize(220) returns [(2, 2), (5, 1), (11, 1)]
        """
        fct = []  # prime factor
        b, e = 2, 0  # base, exponent
        while b * b <= n:
            while n % b == 0:
                n = n // b
                e = e + 1
            if e > 0:
                fct.append((b, e))
            b, e = b + 1, 0
        if n > 1:
            fct.append((n, 1))
        return fct
    def combinations_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        # TODO: How should I do when n - r is negative?
        if n < 0 or r < 0:
            raise Exception('combinations_count(n, r) not defined when n or r is negative')
        if n - r < r: r = n - r
        if r < 0: return 0
        if r == 0: return 1
        if r == 1: return n
        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]
        for p in range(2,r+1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p-1,r,p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot
        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
        return result
    def combinations_with_replacement_count(n, r):
        """Return the number of selecting r pieces of items from n kinds of items allowing individual elements to be repeated more than once.
        
        Args:
            n (long): number
            r (long): number
        
        Raises:
            Exception: not defined when n or r is negative
        
        Returns:
            long: number
        """
        if n < 0 or r < 0:
            raise Exception('combinations_with_replacement_count(n, r) not defined when n or r is negative')
        elif n == 0:
            return 1
        else:
            return combinations_count(n + r - 1, r)
    from bisect import bisect_left, bisect_right
    from collections import deque, Counter, defaultdict # https://docs.python.org/ja/3/library/collections.html#collections.deque
    from heapq import heapify, heappop, heappush, heappushpop, heapreplace,nlargest,nsmallest # https://docs.python.org/ja/3/library/heapq.html
    from copy import deepcopy, copy # https://docs.python.org/ja/3/library/copy.html
    import operator
    from operator import itemgetter
    # ex1: List.sort(key=itemgetter(1))
    # ex2: sorted(tuples, key=itemgetter(1,2))
    from functools import reduce, lru_cache
    def chmin(x, y):
        """change minimum
        if x > y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current minimum value
            y (long): potential minimum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x > y:
            x = y
            return (x, True)
        else:
            return (x, False)
    def chmax(x, y):
        """change maximum
        if x < y, x = y and return (x, True).
        convenient when solving problems of dp[i]
        
        Args:
            x (long): current maximum value
            y (long): potential maximum value
        
        Returns:
            (x, bool): (x, True) when updated, else (x, False)
        """
        if x < y:
            x = y
            return (x, True)
        else:
            return (x, False)

    from fractions import gcd # Deprecated since version 3.5: Use math.gcd() instead.
    def gcds(numbers):
        return reduce(gcd, numbers)
    def lcm(x, y):
        return (x * y) // gcd(x, y)
    def lcms(numbers):
        return reduce(lcm, numbers, 1)

    # first create factorial_list
    # fac_list = mod_factorial_list(n)
    INF = 10 ** 18
    MOD = 10 ** 9 + 7
    modpow = lambda a, n, p = MOD: pow(a, n, p) # Recursive function in python is slow!
    def modinv(a, p = MOD):
        # evaluate reciprocal using Fermat's little theorem:
        # a**(p-1) is identical to 1 (mod p) when a and p is coprime
        return modpow(a, p-2, p)
    def modinv_list(n, p = MOD):
        if n <= 1:
            return [0,1][:n+1]
        else:
            inv_t = [0,1]
            for i in range(2, n+1):
                inv_t += [inv_t[p % i] * (p - int(p / i)) % p]
            return inv_t
    def modfactorial_list(n, p = MOD):
        if n == 0:
            return [1]
        else:
            l = [0] * (n+1)
            tmp = 1
            for i in range(1, n+1):
                tmp = tmp * i % p
                l[i] = tmp
            return l
    def modcomb(n, k, fac_list = [], p = MOD):
        # fac_list = modfactorial_list(100)
        # print(modcomb(100, 5, modfactorial_list(100)))
        from math import factorial
        if n < 0 or k < 0 or n < k: return 0
        if n == 0 or k == 0: return 1
        if len(fac_list) <= n:
            a = factorial(n) % p
            b = factorial(k) % p
            c = factorial(n-k) % p
        else:
            a = fac_list[n]
            b = fac_list[k]
            c = fac_list[n-k]
        return (a * modpow(b, p-2, p) * modpow(c, p-2, p)) % p
    def modadd(a, b, p = MOD):
        return (a + b) % MOD
    def modsub(a, b, p = MOD):
        return (a - b) % p
    def modmul(a, b, p = MOD):
        return ((a % p) * (b % p)) % p
    def moddiv(a, b, p = MOD):
        return modmul(a, modpow(b, p-2, p))

    class UnionFindTree:
        """union find tree class
        TODO: fix this description...
        how to use (example):
        >>  uf = UnionFindTree(N) 
        >>  if uf.find_root(a) == uf.find_root(b):
        >>      do something
        >>  else:
        >>      do something
        >>      uf.unite(a, b)
        """
        def __init__(self, N):
            self.root = [-1] * (N+1)
            self.rank   = [0]  * (N+1)
            self.connected_num = [1] * (N+1)
    
        def find_root(self,x):
            root = self.root
            while root[x] != -1:
                x = root[x]
            return x
    
        def unite(self,x,y):
            root = self.root
            rank = self.rank
            connected_num = self.connected_num
            find_root = self.find_root
    
            rx = find_root(x)
            ry = find_root(y)
            if rx != ry:
                if rank[rx] < rank[ry]:
                    root[rx] = ry
                    rx,ry = ry,rx
                else:
                    if rank[rx] == rank[ry]:
                        rank[rx] += 1
                    root[ry] = rx       
                connected_num[rx] += connected_num[ry]

    """ initialize variables and set inputs
    # initialize variables
        # to initialize list, use [0] * n
        # to initialize two dimentional array, use [[0] * N for _ in range(N)]
    # set inputs
        # open(0).read() is a convenient method:
        # ex) n, m, *x = map(int, open(0).read().split())
        #     min(x[::2]) - max(x[1::2])
        # ex2) *x, = map(int, open(0).read().split())
        #     don't forget to add comma after *x if only one variable is used
    # preprocessing
        # transpose = [x for x in zip(*data)]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
        # flat = [flatten for inner in data for flatten in inner]
        # ex) [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [1, 2, 3, 4, 5, 6, 7, 8, 9]
    # calculate and output
        # output pattern
        # ex1) print(*l) => when l = [2, 5, 6], printed 2 5 6
    """

    # functions used
    r = lambda: sys.stdin.readline().strip()
    r_int = lambda: int(r())
    R = lambda: list(map(int, r().split()))
    Rmap = lambda: map(int, r().split())
    Rfloat = lambda: list(map(float, r().split()))
    Rtuple = lambda: tuple(map(int, r().split()))

    """ how to treat input
    # single int: int(r())
    # single string: r()
    # single float: float(r())
    # line int: R()
    # line string: r().split()
    # line (str, int, int): [j if i == 0 else int(j) for i, j in enumerate(r().split())]
    # lines int: [R() for _ in range(n)]
    """

    if __name__ != '__main__':
        # for test
        sys.stdin = open('sample.txt')
        
    # ----------------------------------
    # main

    N = int(r())
    P = R()
    tmp = 0
    for i, p in enumerate(P):
        if i+1 != p:
            tmp += 1
        if tmp >= 3:
            print('NO')
            exit()
    print('YES')
    
    # end of main
    # ----------------------------------

    """memo: how to use defaultdict of list
    # initialize
    Dic = defaultdict(list)
    # append / extend
    Dic[x].append(y)
    # for
    for k, v in Dic.items():
    """
    
    """memo: how to solve binary problems
    # to make binary digits text
    >>> a = 5
    >>> bin_str_a = format(a, '#06b')
    >>> print(bin_str_a)
    0b0101 # first 2 strings (='0b') indicates it is binary
    """

if __name__ == '__main__':
    main()

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_sorted[i]!=p[i]:
    count += 1
if count <= 2:
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
import sys

def main():
    N = int(input())
    p = list(map(int, input().split()))
    q = sorted(p)
    d = 0
    for i in range(N):
        if p[i] != q[i]:
            d += 1

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

i_tmp = 0
swap_list = []
for i in n_list:
    if i - i_tmp != 1:
        swap_list.append(i)
    i_tmp += 1

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

Submission status: Accepted

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

sorted_P = sorted(P, reverse=False)

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

def shou(a):
    for i in range(len(a)-1):
        if a[i]>a[i+1]:
            return -1
    return 1

f=0
for i in range(n):
    for j in range(n):
        tmp=a[i]
        a[i]=a[j]
        a[j]=tmp
        if shou(a)==1:
            f=1
        tmp=a[i]
        a[i]=a[j]
        a[j]=tmp
if f>0:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
N = int(raw_input())
inp = [int(i) for i in raw_input().split()]

sorted_inp = sorted(inp)

diff = 0

for i in range(len(inp)):
  if inp[i] != sorted_inp[i]:
    diff += 1

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()))
int_queue = [i for i in range(1,n+1)]
ans = 0
for j in range(0,n):
  if p[j]-int_queue[j] == 0:
    continue
  else:
    ans += 1
if ans == 2 or ans == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
import sys
import math

len = int(input())
numList = [int(n) for n in input().split()]

errCount = 0
for i in range(len):
    if numList[i] != int(i+1):
        errCount+=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

s = input().split()
N=int(s[0])
s = input().split()
a=np.array(range(1,N+1))
s=[int(i) for i in s]
if np.count_nonzero(a != s)<=2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def check(a):
  for i in range(1,len(a)):
    if a[i-1]>a[i]:
      return False
  return True
n=int(input())
a=list(map(int,input().split()))
if check(a):
  print('YES')
  exit()
for i in range(n):
  for j in range(n):
    if i==j:
      continue
    a[i],a[j]=a[j],a[i]
    if check(a):
      print('YES')
      exit()
    a[i],a[j]=a[j],a[i]
print('NO')

Submission status: Accepted

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

Submission status: Accepted

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



N = int(input())
Pn = list(map(int, input().split()))
ret = 0
for i,p in enumerate(Pn):
  if i+1 != p:
    ret += 1
if ret <= 2:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input();a=[int(j)-i-1for i,j in enumerate(input().split())];print("YES"if a.count(0)>len(a)-3else"NO")

Submission status: Accepted

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

relations = {}

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

cnt = 0
for key, value in relations.items():
    if key != value:
        cnt += 1

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

Submission status: Accepted

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

count = 0
for i, a in enumerate(A, 1):
  if i != a:
    count += 1
    if count > 2:
      print("NO")
      exit()
      
print("YES")

Submission status: Accepted

Solution code:
num_lines = int(input())
if num_lines<2 or num_lines>50:
	print ('Illegal')
	exit()
#print(num_lines)
a=list(map(int,input().split(" ")))
#print (a)
x=[]

j=0
f=False
for i in range(num_lines):
	if(( i==0 or a[i]>=a[i-1]) and (i==num_lines-1 or a[i]<=a[i+1])):
		continue
	else:
		x.append(i)
		j+=1
#print(j)
if j>0 and j<=4 and j !=1:
	p=a[x[0]]
	a[x[0]]=a[x[j-1]]
	a[x[j-1]]=p
	for k in range(j):
		i=x[k]
		if(( i==0 or a[i]>=a[i-1]) and (i==num_lines-1 or a[i]<=a[i+1])):
			continue
		else:
			f=True
			break
elif j!=0:
	f=True
if f:
	print("NO")
else:
	print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#abc135b

n=int(raw_input())
p=map(int,raw_input().split())
cnt=0
for i in xrange(n):
 if p[i]!=i+1:
  cnt+=1
if cnt==2 or cnt==0:
 print 'YES'
else:
 print 'NO'

Submission status: Accepted

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

sorted_p = sorted(p)

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

w = []
for i in range(0, len(p)):
  if i+1 != p[i]:
    w.append(i)

if (len(w) <= 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 i in range(n):
  if p[i] != sorted_p[i]:
    count+=1
    if count > 2:
      print('NO')
      exit()
      
print('YES')

Submission status: Accepted

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

def main():
	n = int(input().strip())
	v = list(map(int, input().strip().split()))
	w = sorted(v)
	c = 0
	for i in range(n):
		if v[i] != w[i]:
			c += 1
	if c == 0 or c == 2:
		print('YES')
	else:
		print('NO')

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
num_input = input()
list_input = input().split(" ")
N = int(num_input)
count = 0
for i in range(0,N-1):
  check = int(list_input[i])-(i+1)
  if not check == 0:
    count += 1
    if count == 3:
      break

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
p_list = list(map(int, input().split()))
slist = sorted(p_list)
if p_list == sorted(p_list):
    print('YES')
else:
    list1 = [1 if p_list[i] == slist[i] else 0 for i in range(N)]
    if list1.count(0) == 2:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    P = list(map(int, input().split()))
    c = 0
    for i, p in enumerate(P):
        if i + 1 != p:
            c += 1
    print('YNEOS'[c > 2::2])


main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

x = int(0)
l = sorted(p)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
for i in range(n):
  if(l[i]!=(i+1)):
    ind=l.index(i+1)
    l[i],l[ind]=l[ind],l[i]
    break
count=0
#print(l)
for i in range(n):
  if(l[i]!=(i+1)):
    break
  count+=1
if(count==n):
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
_,p=open(0)
i=a=0
for p in p.split():i+=1;a+=int(p)!=i
print('YNEOS'[a>2::2])

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
a.insert(0,0)
f=1
for i in range(1,n+1):
    if a[i]!=i:
        r=a[i]
        t=a[i]
        a[i]=a[r]
        a[r]=t
        break
for i in range(n):
    if a[i]>a[i+1]:
        f=0
if f==0:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
n = int(input())
p_list = list(map(int,input().split()))
p_list_s = sorted(p_list)
count = 0
for i in range(len(p_list)):
    if p_list[i] != p_list_s[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()))
Pi = [1 for i in range(N) if i + 1 != P[i]]

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

Submission status: Accepted

Solution code:
class ABC135:
    def __init__(self):
        pass

    def solve(self, N):
        ans = 0
        p = list(map(int, input().split()))
        ps = sorted(p)
        for i in range(N):
            if(p[i] != ps[i]):
                ans += 1
        if(ans == 0 or ans == 2):
            print("YES")
        else:
            print("NO")

if __name__ == '__main__':
    N = int(input())
    problem = ABC135()
    problem.solve(N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding: UTF-8

i = input()
array = list(map(int, input().split()))

v = 1
counter = 0

for num in array:
    if num != v:
        counter += 1
    v += 1

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

Submission status: Accepted

Solution code:
n = int(input())
data = input().split(" ")
for i in range (len(data)):
    data[i] = int(data[i])
arr = sorted(data)
ctr = 0
for j in range (len(data)):
    if arr[j] != data[j]:
        ctr += 1
if ctr == 0 or ctr == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

flag = 0

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

if flag == 0 or flag == 2:
  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()))
wcnt = 0

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

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

Submission status: Accepted

Solution code:
from sys import exit
n = int(input())
ls = list(map(int,input().split()))
if sorted(ls)==ls:
    print("YES")
    exit()
for i in range(len(ls)-1):
    for j in range(i+1,len(ls)):
        l = list(ls)
        l[i],l[j]=l[j],l[i]
        if sorted(l)==l:
            print("YES")
            exit()
print("NO")

Submission status: Accepted

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

A=sorted(mao)
z=0
for i in range(0,N):
  if mao[i]!=A[i]:
    z=z+1

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

Submission status: Accepted

Solution code:
print("YNEOS"[sum(i!=int(q)for i,q in enumerate(open(0).read().split()))>3::2])

Submission status: Accepted

Solution code:
print("YNEOS"[sum(i!=int(q)for i,q in enumerate(open(0).read().split()))-1>2::2])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
s=sorted(a)
for i in range(0,n):
  for j in range(i,n):
    b=a[:]
    b[i],b[j]=b[j],b[i]
    if s==b:
      print("YES")
      exit()
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==0 or cnt==2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
swaps = 0
ar = [int(x) for x in input().split()]
for i in range(len(ar)):
    mn = ar[i]
    mi = i
    for j in range(i, len(ar)):
        if mn > ar[j]:
            mn = ar[j]
            mi = j
    if mi != i:
        if swaps == 1:
            print("NO")
            exit(0)
        ar[i], ar[mi] = ar[mi], ar[i]
        swaps += 1
print("YES" if swaps <= 1 else "NO")

Submission status: Accepted

Solution code:
input();print("YNEOS"[len([1 for i,q in enumerate(input().split(),1)if int(q)!=i])>2::2])

Submission status: Accepted

Solution code:
input();print("YNEOS"[len([1 for i,q in enumerate(map(int,input().split()),1)if q!=i])>2::2])

Submission status: Accepted

Solution code:
# ABC135B - 0 or 1 Swap
def main():
    n = int(input())
    P = tuple(map(int, input().rstrip().split()))
    flg = sum(i != j for i, j in enumerate(P, 1)) <= 2
    print("YES" if flg else "NO")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

raw_input = sys.stdin.readline

f = lambda : raw_input().rstrip()    
f1 = lambda : f().split()
f2 = lambda t: map(t,f1())

def main1():
    a,b = f2(int)
    if (a+b)%2 == 0:
       print (a+b)/2
    elif a == b:
       print 0
    else:
       print "IMPOSSIBLE"

def main2():
    n = int(f())
    a = f2(int)
    b = sorted(a)
    m = 0
    for i in xrange(n):
        if a[i] != b[i]:
            m = m + 1
    if m<=2:
        print "YES"
    else:
        print "NO"

def main3():
    n = int(f())
    a = f2(int)
    b = f2(int)
    ans = 0
    for k in xrange(n):
        if a[i] >= b[i]:
            ans = ans + b[i]
        elif a[i] < b[i] :
            ans = ans + a[i]
            b[i] = b[i] - a[i]
            ans = ans + min(a[i+1], b[i])
            a[i+1] = a[i+1] - b[i]  
    print ans 
 
def main4():
    pass
    
    
if __name__ == "__main__":
    main2()

Submission status: Accepted

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

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:
import sys
import math
INPUT = sys.stdin.readline
#入力関数
def SORO_INT(): return int(INPUT())
def MULT_INT_LIST(): return list(map(int, INPUT().split()))
def MULT_INT_MAP(): return map(int, INPUT().split())
def SORO_STRING(): return INPUT()
def MULT_STRING(): return INPUT().split()

N = SORO_INT()
A = MULT_INT_LIST()
X = sorted(A)
if A == X:
    print('YES')
    sys.exit()


i = 0
count=0
for i in range(N):
    if count>2:
        print('NO')
        sys.exit()
    
    if A[i]==i+1:
        count = count
    else:
        count+=1

print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = "NO"

if sorted(P) == P:
    ans = "YES"

for i in range(N):
    for j in range(i+1,N):
        tmp = P[:]
        tmp[i],tmp[j] = tmp[j],tmp[i]
        if sorted(tmp) == tmp:
            ans = "YES"
            break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = [ int(_) for _ in input().split() ]
ls = [ _ for _ in range(1, n+1) ]
cnt = int()

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

Submission status: Accepted

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

c = 0
for i, v in zip(range(N), p):
    if i+1 != v:
        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()))
li = sorted(p)
count = 0
for i in range(n):
    if p[i] != li[i]:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")