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())

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

def nobori():
    for i in range(n-1):
        if B[i]>B[i+1]:
            return 0
    return 1

ans ="NO"
for i in range(n):
    for j in range(i,n):
        B = A[:]
        B[i],B[j] = B[j],B[i]
        if nobori()==1:
            ans = "YES"
print(ans)

Submission status: Accepted

Solution code:
import sys

fin = sys.stdin.readline
N = [int(elem) for elem in fin().split()][0]
p_list = [int(elem) for elem in fin().split()]

sorted_p = sorted(p_list)

count = 0
for i in range(len(p_list)):
    if p_list[i] != sorted_p[i]:
        count += 1

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

Submission status: Accepted

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

B=copy.deepcopy(A)
B.sort()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    p = [int(x) for x in input().split()]
    if p == sorted(p):
        print('YES')
        return
    for i in range(n):
        for j in range(i + 1, n):
            q = p[:]
            q[i], q[j] = q[j], q[i]
            if q == sorted(q):
                print('YES')
                return
    print('NO')


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def ok():
    print("YES")
    exit(0)
    
def ng():
    print("NO")
    exit(0)

n = int(input())
sorted_p = list(range(1, n + 1))
p = list(map(int,input().split()))

for i in range(n):
    for j in range(i+1, n):
        p[i], p[j] = p[j], p[i]
        if p == sorted_p:
            ok()
        p[i], p[j] = p[j], p[i]

if p == sorted_p:ok()
else: ng()

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int,input().split()))
cnt = 0
for i,p in enumerate(P) :
    if p != i+1 :
        cnt += 1

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

Submission status: Accepted

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


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()))
t = 0
for k in range(N):
    if p[k] != (k+1):
        t += 1
print("YES" if t <= 2 else "NO")

Submission status: Accepted

Solution code:
n=int(input())
p=[int(i) for i in input().split()]
l=0
for i in range(n):
    if i+1!=p[i]:
        l+=1
if l<=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:
    print("NO")
else:
    print("YES")

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
if L==sorted(L):
	print("YES")
	exit(0)
for i in range(N):
	for j in range(i+1,N):
		t=L[::]
		t[i],t[j]=t[j],t[i]
		if t==sorted(t):
			print("YES")
			exit(0)
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
print('YES' if count<=2 else 'NO')

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    lp = rli()
    ls = sorted(lp)
    cnt = 0
    for p, s in zip(lp, ls):
        if p != s:
            cnt += 1
    if cnt <= 2:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
#debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = II()
    p_list = LI()

    sortedp = sorted(p_list)

    cnt = 0
    for p, sp in zip(p_list, sortedp):
        if p != sp:
            cnt += 1

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

solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

if cnt<=2:
    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:
        pass
    else:
        count += 1
if count <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

q = sorted(p)

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

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

Submission status: Accepted

Solution code:
n = int(input())
p = [int(i) for i in input().split()]
q = sorted(p)
cnt = 0
for i in range(n):
    if p[i] != q[i]:
        cnt += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
p = list(li())

cnt = 0

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

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


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    p = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, p)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

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

def solve(N: int, p: "List[int]"):
    s = list(range(1, N + 1))
    cnt = 0
    for i in range(N):
        if s[i] != p[i]:
            cnt += 1
    if cnt == 0 or cnt == 2:
        ret = YES
    else:
        ret = NO

    print(ret)
    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:
import sys
input=sys.stdin.readline
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==0 or count==2:
    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 a[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 = map(int, input().split())

if sum(pi != i + 1 for i, pi in enumerate(p)) <= 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:
N = int(input())
P = list(map(int, input().split()))
diff = 0

check = list(range(1, N + 1))
for p, c in zip(P, check):
    diff += (p != c)

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right; from collections import Counter, defaultdict, deque, OrderedDict; from copy import deepcopy; from fractions import gcd; from functools import lru_cache, reduce; from math import ceil, floor; from sys import stdin, setrecursionlimit
import heapq as hq; import itertools as its; import operator as op


# globals
inf = float('inf')
input = stdin.readline


def main():
    n = get_int()
    P = get_li()
    Q = list(range(1, n+1))
    diff = 0
    for p, q in zip(P, Q):
        if p != q:
            diff += 1
    if diff == 0 or diff == 2:
        print('YES')
    else:
        print('NO')
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def gets(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    setrecursionlimit(4100000)
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input())
    p = [int(i) for i in input().split()]

    s = sorted(p)

    print('YES' if sum(i != j for i, j in zip(p, s)) < 3 else 'NO')


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
wrong = 0
for i, a in enumerate(A, 1):
    if i == a:
        continue
    else:
        wrong += 1
if wrong == 2 or wrong == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
ar=list(map(int,input().split()))
def srt(ar):
    return ar==sorted(ar)
b=0
if(srt(ar)):b=1
for i in range(n):
    for j in range(i,n):
        ar[i],ar[j]=ar[j],ar[i]
        if(srt(ar)):b=1
        ar[i],ar[j]=ar[j],ar[i]
if(b):
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

q = sorted(p)

if p==q:
    print('YES')
    exit()

for i in range(n):
    for j in range(i+1,n):
        r = [x for x in p]
        r[i]=p[j]
        r[j]=p[i]
        if q==r:
            print('YES')
            exit()
print('NO')

Submission status: Accepted

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

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

Q=sorted(P)

import copy

for i in range(N):
    for j in range(i,N):
        R=copy.deepcopy(P)

        R[i],R[j]=R[j],R[i]

        if Q==R:
            print("YES")
            sys.exit()

print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
X = sorted(P)
num = 0
for x, p in zip(X, P):
    if x != p:
        num += 1
if num == 0 or num == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
arr = list(map(int, input().split()))
base = [i + 1 for i in range(n)]

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

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

Submission status: Accepted

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

count = 0
for i, n in enumerate(num):
    if n != i+1:
        count += 1

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

Submission status: Accepted

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

diff = 0
for i in range(len(a)):
    if a[i] != b[i]:
        diff += 1
# print(a)
# print(b)
print("YES" if diff == 0 or diff == 2 else "NO")

Submission status: Accepted

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

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

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

Submission status: Accepted

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

count = 0
for a, b in zip((sorted(inputs)), inputs):
    if a != b:
        count += 1
print('YES' if count == 0 or count == 2 else 'NO')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC135 B

import sys
import math
import bisect
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
mod = 10**9 + 7

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:
a = int(input())
b = list(map(int, input().split()))
c = sorted(b.copy())
d = 0
for i in range(len(b)):
    if c[i] != b[i]:
        d += 1
if d <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
N = int(input())
As =   list(map(int,input().split(" ")))

Asorted = As[:]
Asorted.sort()
diff = 0
for i in range(N):
    if Asorted[i] != As[i]:
        diff += 1
if diff == 0 or diff == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p_list = [int(elem) for elem in input().split()]
print("YES" if sum(i != p for i, p in enumerate(p_list, start=1)) <= 2 else "NO")

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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]:
		cnt +=1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    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:
        print('YES')
    else:
        print('NO')
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print('NO')

Submission status: Accepted

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

import itertools as ite
import math
import random
import sys
import codecs
from collections import defaultdict

INF = 10 ** 18
MOD = 10 ** 9 + 7

sys.setrecursionlimit(1000000)

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

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

Submission status: Accepted

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

ans = (
    'YES' if sum(i != q for i, q in enumerate(p, 1)) <= 2 else
    'NO'
)

print(ans)

Submission status: Accepted

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

q = p[:]
q.sort()

count = 0
for pp, qq in zip(p, q):
    if pp != qq:
        count += 1

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n = int(input())
a = [int(i) for i in readline().split()]
sa = sorted(a)

for i in range(n):
    for j in range(i+1,n):
        r = a[:]
        r[i], r[j] = r[j], r[i]
        
        if sa == r:
            print("YES")
            exit()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


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


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, P):
    d = 0
    for a, b in zip(P, sorted(P)):
        if a != b:
            d += 1
    if d > 2:
        return 'NO'
    return 'YES'


def main():
    N = read_int()
    P = read_int_n()
    print(slv(N, P))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
inf = 10 ** 18
P = 10 ** 9 + 7

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

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

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

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
def main():
    N = int( input())
    now = 0
    P = list( map( int, input().split()))
    for i in range(1, N+1):
        if i != P[i-1]:
            now += 1
    if now == 2 or now == 0:
        print("YES")
    else:
        print("NO")
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
P = list(map(int, input().split()))
A = list(range(1, N+1))
S = sum(1 if p != a else 0 for p, a in zip(P, A))
if S == 0 or S == 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

def main():
    N = int(input())
    # s = input()
    # a, b = map(int, input().split())
    p = list(map(int, input().split()))
    # a = [int(input()) for i in range(N)]

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

# A, B = list(map(int, sys.stdin.readline().split()))
# if (A + B) & 1:
#     print('IMPOSSIBLE')
# else:
#     print((A + B)//2)


N = int(sys.stdin.readline())
P = list(map(int, sys.stdin.readline().split()))
sp = list(sorted(P))
ok = False
for i, j in itertools.product(range(N), range(N)):
    P[i], P[j] = P[j], P[i]
    ok |= sp == P
    P[i], P[j] = P[j], P[i]
if ok:
    print('YES')
else:
    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):
    if P[i] == i+1:
        pass
    else:
        count += 1
if count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n, a = input(), map(int, raw_input().split())
print 'YES' if sum(x != y for x, y in zip(a, sorted(a))) <= 2 else 'NO'

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    n = int(input())
    p = list(map(int, input().split()))
    mod_p = sorted(p)
    count = 0

    for pi, mod_pi in zip(p, mod_p):
        if pi != mod_pi:
            count += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

num = 0
for i, p in enumerate(ps, 1):
    if i != p:
        num += 1
        if num > 2:
            print('NO')
            break
else:
    print('YES')

Submission status: Accepted

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

print('YES' if sum(i!=j for i,j in zip(range(1,n+1),A)) <= 2 else 'NO')

Submission status: Accepted

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