Problem:


Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.



Sample input:
5
5 2 3 4 1


Sample output:
YES

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

print("YES" if sum(x != i for i, x in enumerate(arr, 1)) <= 2 else "NO")

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    p = [int(s) for s in input().split()]
    p_sorted = sorted(p)
    rem = [(x, y) for x, y in zip(p, p_sorted) if x != y]
    if len(rem) <= 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):
    c += i + 1 != P[i]

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = input()
p = map(int, raw_input().split())
for i in range(N):
  for j in range(N):
    t = p[:]
    t[i],t[j] = t[j],t[i]
    if t==sorted(t):
      print "YES"
      exit(0)
print "NO"

Submission status: Accepted

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

Submission status: Accepted

Solution code:
LI=lambda:list(map(int,input().split()))
MI=lambda:map(int,input().split())
I=lambda:input()

n=int(I())
p=LI()
a=[i+1 for i in range(n)]
if a==p:print("YES");exit()
b=0
for i in range(n):
  if a[i]!=p[i]:b+=1
if b==2:print("YES")
else:print("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]:
        continue
    cnt+= 1

if cnt <= 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()))

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

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

Submission status: Accepted

Solution code:
N = int(input())
*P, = map(int,input().split())
Q = sorted(P)
c = sum(p!=q for p,q in zip(P,Q))
if c<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
N = int(input())

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

p_sort = sorted(p_arr)

ndiff = sum([1 for x,y in zip(p_arr, p_sort) if x!=y])

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

Submission status: Accepted

Solution code:
import math
import fractions
import copy
import numpy as np
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)
"""
#decomment when needed
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
"""
n = ip()
a = iprow()
s = 0
for i in range(n):
    if a[i] != i+1:s+=1
if s==0 or s==2:j(1)
else:j(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def yes():
    print("YES")  # type: str


def no():
    print("NO")  # type: str


def solve(N: int, p: "List[int]"):
    B = sorted(p)
    count = 0
    for bb, pp in zip(B, p):
        if bb != pp:
            count += 1
    if count <= 2:
        yes()
    else:
        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:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N =  INT()
p = LIST()

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

Submission status: Accepted

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

ps = sorted(p)

c = len([1 for x,y in zip(p, ps) if x != y])
if c == 2 or c == 0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n =getN()

nums = getList()
flag = 0
for i, x in enumerate(nums):
    if x != (i+1):
        flag += 1

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

Submission status: Accepted

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

same = 0
for i,p in enumerate(P):
    if i+1 == p:
        same += 1
print('YES' if same==N or same==N-2 else 'NO')

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    if (a-b)%2 == 0:
        print((a+b)//2)
    else:
        print("IMPOSSIBLE")
    return

#B
def B():
    n = I()
    p = LI()
    k = [p[i] for i in range(n)]
    k.sort()
    s = 0
    for i in range(n):
        s += (p[i] != k[i])
    if s < 3:
        print("YES")
    else:
        print("NO")
    return

#C
def C():
    n = I()

    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    
    n = int(input())
    arr = list(map(int, input().split()))
    kek = sorted(arr)
    
    cnt = 0
    for i in range(n):
        cnt += (arr[i] != kek[i])
    
    if cnt > 2:
        print("NO")
    else:
        print("YES")
    
    return 0

main()

Submission status: Accepted

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

cnt = 0
for i, j in enumerate(a):
    if i + 1 != j:
        cnt += 1

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

Submission status: Accepted

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

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

Submission status: Runtime Error

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

if p == sorted(p):
    print('YES')
else:
    ret = 'NO'
    # 全通りの組み合わせをみて、昇順かどうか調べる
    for i in range(N-1):
        for j in range(i+1, N):

            # p[i]とp[j]より小さければ抜ける
            if p[i] < p[j]:
                continue
            else:
                k = p.copy()
                k[i], k[j] = k[j], k[i]

            is_sorted = True
            for l in range(N-1):
                if k[l+1] < k[l]:
                    is_sorted = False
                    break

            if is_sorted:
                ret = 'YES'
                break
        if is_sorted:
            break

    print(ret)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
#ABC135 B

N = int(input())
P = list(map(int,input().split()))
PP = sorted(P)
count = 0
for i in range(1,N):
    for j in range(2,N+1):
        p = P[i]
        q = P[j]
        P[i] = q
        P[j] = p
        if P == PP:
            count += 1

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

Submission status: Runtime Error

Solution code:
A, B = map(int, input().split())
       if (A + B) % 2 != 0:
           print("IMPOSSIBLE")
       else:
               print(int((A+B)/2))

Submission status: Runtime Error

Solution code:
N=input("enter a number")
list=[]
order=True
check=True
pos=0
temp=0
counter=0
numbers=input("enter the permutation as n digit number")
for number in numbers:
    list.append(int(number))
p0=list[0]


for n in range(1,len(list)):
    #checking is the given permutatoin is in ascendig order or no
    if p0<list[n]:
        p0=list[n]
    else:
        order=False

if order==True:
    print("the given permutation is in ascending order")
if order==False:
    for i in range(1,len(list)+1):
        if i==list[i-1]:
            pass
        else:
            counter+=1
            temp=list[i-1]
            pos=list.index(i)           #swap
            list[i-1]=list[pos]
            list[pos]=temp
if counter==1:
    print("Yes we can sort it in ascending order")
else:
    print("No it cant be sorted in one step")

Submission status: Runtime Error

Solution code:
def check(n, list1): 
    new = [] 
    for i in range(n): 
        new.append(list1[i]) 
          
    new.sort() 
      
    # Check if 0 or 1 swap required  
    # to get the sorted array  
    swaps = 0
    for i in range(n): 
        if list1[i] != new[i]: 
            swaps += 1
              
    if swaps == 0 or swaps == 2: 
        return True
    else: 
        return False
  
n = int(input())
numbers=[]
for i in range(n):
    numbers.append(int(input()))

if check(n, numbers): 
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

Solution code:

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

for i in range(n):
    if p[i]<p[i+1]:
        tmp=p[i]
        p[i]=p[i+1]
        p[i+1]=tmp
        break
if new_p==p:
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

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

for i in range(n-1):
    if p[i]<=p[i+1]:
        tmp=p[i]
        p[i]=p[i+1]
        p[i+1]=tmp
        break
if new_p==p:
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
P = list(map(int, input().split()))
p0 = 0
p1 = P[0]
temp = []
for i, p in enumerate(P[1:]):
    if p >= p1:
        p0 = p1
        p1 = p
        continue
    else:
        temp.append(i)
        p1 = p

if len(temp) > 2:
    print("NO")
    exit()
if len(temp) == 0:
    print("YES")
    exit()

P[temp[0]], P[temp[1]+1] = P[temp[1]+1], P[temp[0]]

p0 = 0
p1 = P[0]
temp = []
for i, p in enumerate(P[1:]):
    if p >= p1:
        p0 = p1
        p1 = p
        continue
    else:
        temp.append(i)
        p1 = p

if len(temp) == 0:
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input)
List = list(map(int, input().split()))
mid = -1
flag = True
for i in range(N):
  if List[i] == i+1:
    pass
  else:
    flag = False
    if mid == -1:
      mid = List[i]
    else:
      if mid == List[i]:
        print("YES")
      else:
        print("NO")
if flag:
  print("YES")

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
n = int(input())
p = list(map(int,input().split()))
ps = sorted(p)

a = np.array(lis)
b = np.array(lim)
if a-b == np.zeros((1,n)):
    print('YES')
else:
    total = 0
    for i in range(n):
        if p[i] != ps[i]:
            total += 1
    if total <= 2:
        print('YES')
    else:
        print('NO')

Submission status: Runtime Error

Solution code:
n = int(input())
p = list(map(int,input().split()))
ps = sorted(p)
import numpy as np
a = np.array(lis)
b = np.array(lim)
if a-b == np.zeros((1,n)):
    print('YES')
else:
    total = 0
    for i in range(n):
        if p[i] != ps[i]:
            total += 1
    if total <= 2:
        print('YES')
    else:
        print('NO')

Submission status: Runtime Error

Solution code:
n = int(input())
p = list(map(int, input().split()))
answer = 0
for i in range(n):
  if p[i] + 1 != p[i + 1]:
  	answer += 1
  if answer <= 2:
    print('Yes')
  else:
    print('No')

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

int main() {
  ll input(n);
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> ans(a);
  sort(ans.begin(), ans.end());
  ll cnt = 0;
  REP(i, n) {
    cnt += !(a[i] == ans[i]);
  }

  if (cnt == 0 || cnt == 2) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(map(int, input().split()))
Min_S = min(S)
Min_idx = S.index(Min_S)


S[0], S[Min_idx] = S[Min_idx], S[0]
if S == Sort_S:
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

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

Max_S = max(S)
Max_idx = S.index(Max_S)
Min_S = min(S)
Min_idx = S.index(Max_S)
Sort_S = sorted(S)

S[Max_idx], S[Min_idx] = S[Min_idx], S[Max_idx]
if S == Sort_S:
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


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

    N = ii()
    P = il()
    cnt = 0
    for n in range(N):
        if P[n] != n + 1:
            cnt += 1
        if P[P[n]] != n+1:
            print('NO')
            exit()
    print('NO' if cnt > 3 else 'YES')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
    input()
    g = input()
    a = list(map(str, g.split()))
    b = list(map(int, g.split()))
    if sorted(b) == b:
        print("YES")
    else:
        m = 0
        for i in range(len(b)):
            if b[i] == sorted(b)[i]:
                m+=1
        if m==len(b)-2:
            print("YES")
        else:
            print("NO")

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if sorted(H) == H:
  print("YES")
  sys.exit()
  
for i in range(0,N-1):
  for j in range(1,N):
    tmp = H.replace(H[i],H[j])
    if tmp == sorted(H):
      print("YES")
      sys.exit()
print("NO")

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input.split()))
lis=l.sorted()
lis.reverse()
ans=0
for i ,j in zip(l,lis):
    if i!=j:
        ans+=1
if ans==2:
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

Solution code:
input()
p = [int(x) for x in input()]
q = sorted(p)
d = [(pi, qi) for pi, qi in zip(p, q) if pi != qi]
print("YES" if len(d) == 0 or len(d) == 2 and d[0][0] == d[1][1] and d[1][0] == d[0][1] else "NO")

Submission status: Runtime Error

Solution code:
l=[int(x) for x in input().split()]
cnt=0
x=0
y=0
b=0
for i in range(n):
    if(i+1!=l[i]):
        cnt+=1
        if(cnt==1):
            x=l[i]
        elif(cnt==2):
            y=l[i]
        elif(cnt>0):
            b=1
            break
if(b==1):
    print("NO")
elif(l[x-1]!=y and l[y-1]!=x):
    print("NO")
else:
    print("YES")

Submission status: Runtime Error

Solution code:
from math import ceil,sqrt,log,gcd,pi
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())

n=int(input())
a=list(map(int,input().split()))
b={}
for i in range(len(a)):
    b[a[i]]=i
a.sort()
x=[]
for i in range(n):
    if i!=b[a[i]]:
        x.append([i,b[a[i]]])
# print(x)
if(len(x)>2):
    print("NO")
elif(len(x)==2 and x[0][0]!=x[1][1]):
    print('NO')
else:
    print("YES")

Submission status: Runtime Error

Solution code:
from math import ceil,sqrt,log,gcd,pi
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())

n=ii()
a=li()
b={}
for i in range(len(a)):
    b[a[i]]=i
a.sort()
x=[]
for i in range(n):
    if i!=b[a[i]]:
        x.append([i,b[a[i]]])
# print(x)
if(len(x)>2):
    print("No")
elif(len(x)==2 and x[0][0]!=x[1][1]):
    print('No')
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
List = list(map(int, input().split()))
ans = 'YES'
for i in List:
  if(List[i] != List[i+1]):
    ans = 'NO'
    break
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
p=list(map(in,input().split()))
t=list(range(1,n+1))
cnt=0
for pp in p:
  for tt in t:
    if pp!=tt:
      cnt+=1
print('Yes' if 0<=cnt<=2 else 'No')

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

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

#############
# Main Code #
#############
N = inputI()
P = inputIL()

memo = []
for i in range(1,N+1):
  if P[i-1] != i:
    memo.append((P[i-1],i))
    
if len(memo) == 0:
  print("Yes")
  exit()
if len(memo) == 2:
  if memo[0][0] = memo[1][1]:
    print("Yes")
    exit()
print("No")

Submission status: Runtime Error

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: Runtime Error

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: Runtime Error

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

Submission status: Runtime Error

Solution code:
 = int(input())
P = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
  if i+1 == P[i]:
    continue
  elif i+1 != P[i] and P[P[i]] == i+1:
    cnt = 1
  elif cnt == 0:
    print('NO')
    cnt = 2
    break
if cnt!=2:
  print('NYES')

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
listp=list(map(int,input().split()))
for i in range(1,n):
  for j in range(2,n+1):
    a=listp[i-1]
    b=listp[j-1]
    listp[i-1]=b
    listp[j-1]=a
    for y in range(n):
      if listp[y]==y+1:
        print('Yes')
        exit()
for x in range(n):
  if listp[x]==x+1
    print('Yes')
    exit()
print('No')

Submission status: Runtime Error

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 else 'NO')

Submission status: Runtime Error

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

count = 0
for i, m enumerate(M, 1):
    if i != m:
        count += 1

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
n = int(input())
p = list(map(int, input().split()))

cnt = 0

for i, el in enumerate(p):
  if i == 0: continue
  if  el < p[i-1]: cnt += 1

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

ac

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
N=int(input())
M=list(map(int,input().split()))
ans=0
list_= sorted(M)
for i in range(N):
  if M[i]==list_[i]:
    ans+=1
if ans>=M-2:
  print('Yes')
else:
  print('No')

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())

wrong = 0
for i, p in enemerate(map(int, input().split()), start=1):
  wrong += int(i != p)

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
zn=int(input())
p=[int(i) for i in input().split()]
count=0
for i in range(n-1):
    for j in range(i+1,n):
        #print(p[i],p[j])
        if p[i]>p[j]:
            count+=1
if count>=3:
    print("NO")
else:
    print("YES")

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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