AtCoder Beginner Contest 135 - 0 or 1 Swap

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-----
5
5 2 3 4 1

-----Sample Output-----
YES

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

Difficulty: EASY

Starter code:

Solutions:
import copy
n = int(input())
li = list(map(int, input().split()))
li1 = copy.copy(li)
li1.sort()
cnt = 0
for i in range(n):
	if li[i] != li1[i]:
		cnt += 1
if cnt <= 2:
	print('YES')
else:
	print('NO')


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


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


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


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


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


(N, *p) = map(int, open(0).read().split())
if sum((p[i] != i + 1 for i in range(N))) <= 2:
	print('YES')
else:
	print('NO')


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


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


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


_ = input()
p = list(map(int, input().strip().split(' ')))
print('NO' if sum((1 for (a, b) in zip(sorted(p), p) if a != b)) > 2 else 'YES')


n = int(input())
P = list(map(int, input().split()))
L = []
for i in range(1, n + 1):
	if P[i - 1] != i:
		L.append(i)
print('YES' if len(L) < 3 else 'NO')


(n, *P) = map(int, open(0).read().split())
s = sum((i != p for (i, p) in enumerate(P, 1)))
print('YES' if s <= 2 else 'NO')


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


from typing import List

def answer(n: int, p: List[int]) -> str:
	count = 0
	for (i, num) in enumerate(p, start=1):
		if i != num:
			count += 1
	return 'YES' if count <= 2 else 'NO'

def main():
	n = int(input())
	p = list(map(int, input().split()))
	print(answer(n, p))

def __starting_point():
	main()
__starting_point()


N = int(input())
p = list(map(int, input().split()))
a = 0
for i in range(N):
	if p[i] != i + 1:
		a += 1
if a >= 3:
	print('NO')
else:
	print('YES')


N = int(input())
P = list(map(int, input().split()))
C = [P[i - 1] == i for i in range(1, N + 1)].count(0)
print('YES' if C == 0 or C == 2 else 'NO')


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


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


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


N = int(input())
A = [int(a) for a in input().split()]
print('YES' if sum([1 for i in range(N) if A[i] != i + 1]) <= 2 else 'NO')


N = int(input())
lst1 = list(map(int, input().split()))
lst2 = sorted(lst1)
c = 0
for (i, j) in zip(lst1, lst2):
	if i != j:
		c += 1
if c == 2 or c == 0:
	print('YES')
else:
	print('NO')


import math
import collections
import itertools
import copy

def YesNo(Bool):
	if Bool:
		print('YES')
	else:
		print('NO')
	return

def resolve():
	N = int(input())
	p = list(map(int, input().split()))
	ans = False
	tmp = copy.deepcopy(p)
	isOK = True
	for k in range(N - 1):
		if tmp[k] >= tmp[k + 1]:
			isOK = False
	if isOK:
		ans = True
	for i in range(N):
		for j in range(i + 1, N):
			tmp = []
			tmp = copy.deepcopy(p)
			(tmp[i], tmp[j]) = (tmp[j], tmp[i])
			isOK = True
			for k in range(N - 1):
				if tmp[k] >= tmp[k + 1]:
					isOK = False
			if isOK:
				ans = True
	YesNo(ans)
resolve()


N = int(input())
P = map(int, input().split())
cnt = 0
for (i, p) in enumerate(P, start=1):
	if i != p:
		cnt += 1
print('YES' if cnt == 2 or cnt == 0 else 'NO')


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


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


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


N = int(input())
p = list(map(int, input().split()))
if p == sorted(p):
	print('YES')
else:
	ret = 'NO'
	is_sorted = False
	for i in range(N - 1):
		for j in range(i + 1, N):
			if p[i] < p[j]:
				continue
			else:
				k = p.copy()
				(k[i], k[j]) = (k[j], k[i])
			if k == sorted(p):
				ret = 'YES'
				is_sorted = True
				break
		if is_sorted:
			break
	print(ret)


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


n = int(input())
p_list = [int(i) for i in input().split()]
np_list = sorted(p_list)
count = 0
for i in range(n):
	if p_list[i] != np_list[i]:
		count += 1
if count == 2 or count == 0:
	ans = 'YES'
else:
	ans = 'NO'
print(ans)


N = int(input())
p = [int(s) for s in input().split()]
cnt = [v != i + 1 for (i, v) in enumerate(p)].count(True)
print('YES' if cnt in (0, 2) else 'NO')


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


N = int(input())
P = list(map(int, input().split()))
cnt = 0
for i in range(N):
	if i + 1 != P[i]:
		cnt += 1
print('NO' if cnt > 2 else 'YES')


input()
(*p,) = map(int, input().split())
print('YNEOS'[2 < sum((i != j for (i, j) in zip(p, sorted(p))))::2])


n = int(input())
p = map(int, input().split())
judge = [v != k + 1 for (k, v) in enumerate(p)]
if sum(judge) <= 2:
	print('YES')
else:
	print('NO')


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


N = int(input())
p = list(map(int, input().split()))
result = 'NO'
sorted_p = sorted(p)
count = 0
for i in range(N):
	if p[i] != sorted_p[i]:
		count += 1
if count <= 2:
	result = 'YES'
print(result)


N = int(input())
p = [int(i) for i in input().split()]
r = sorted(p)
for i in range(N):
	for j in range(i + 1, N):
		q = []
		for k in range(N):
			if k == i:
				q.append(p[j])
			elif k == j:
				q.append(p[i])
			else:
				q.append(p[k])
		if p == r or q == r:
			print('YES')
			return
print('NO')


import sys
import itertools
tokens = itertools.chain.from_iterable(list(map(str.split, sys.stdin)))
yesno = ['NO', 'YES']
n = int(next(tokens))
p = [int(next(tokens)) for _ in range(n)]
s = sum((1 if a != b else 0 for (a, b) in zip(p, sorted(p))))
print(yesno[s == 2 or s == 0])


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


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


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


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


def check(n, list1):
	new = []
	for i in range(n):
		new.append(list1[i])
	new.sort()
	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 = list(map(int, input().split()[:n]))
if check(n, numbers):
	print('YES')
else:
	print('NO')


N = int(input())
(*p,) = map(int, input().split())
v = sum((1 for (i, j) in zip(range(1, N + 1), p) if i != j))
print('YES' if v <= 2 else 'NO')


n = int(input())
p = list(map(int, input().split()))
ps = sorted(p)
cnt = 0
for (i, j) in zip(p, ps):
	if i != j:
		cnt += 1
print('YES' if cnt <= 2 else 'NO')


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


n = int(input())
l = [int(x) for x in input().split()]
s_l = sorted(l)
print('YES' if sum([1 if l[i] != s_l[i] else 0 for i in range(n)]) <= 2 else 'NO')


n = int(input())
nums = list(map(int, input().split()))
sorted_nums = sorted(nums)
not_equal = 0
for (p, q) in zip(nums, sorted_nums):
	if p != q:
		not_equal += 1
if not_equal <= 2:
	print('YES')
else:
	print('NO')


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


input()
P = list(map(int, input().split()))
a = 0
for (p, q) in zip(P, sorted(P)):
	if p != q:
		a += 1
print('YES' if a < 3 else 'NO')


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


n = int(input())
a = list(map(int, input().split()))
imp = 0
for (i, x) in enumerate(a, 1):
	if i != x:
		imp += 1
if imp <= 2:
	print('YES')
else:
	print('NO')


(N, *P) = list(map(int, open(0).read().split()))
count = sum((p != i + 1 for (i, p) in enumerate(P)))
print('YES' if count <= 2 else 'NO')


N = int(input())
ans = 0
d = [int(s) for s in input().split()]
for i in range(N):
	if d[i] != i + 1:
		ans += 1
if ans >= 3:
	print('NO')
else:
	print('YES')


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


def inpl():
	return list(map(int, input().split()))
N = int(input())
P = inpl()
Q = sorted(P)
for i in range(N):
	for j in range(i, N):
		R = [p for p in P]
		(R[i], R[j]) = (R[j], R[i])
		for k in range(N):
			if R[k] != Q[k]:
				break
		else:
			print('YES')
			return
else:
	print('NO')


def resolve():
	n = int(input())
	p = map(int, input().split())
	cnt = 0
	for (i, j) in enumerate(p):
		if i + 1 != j:
			cnt += 1
	print('YES' if cnt < 3 else 'NO')
resolve()


N = int(input())
P = [int(i) for i in input().split()]
C = 0
for i in range(N):
	if i + 1 != P[i]:
		C += 1
if C >= 3:
	print('NO')
else:
	print('YES')


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


n = int(input())
p = [int(i) for i in input().split()]
print('YES' if sum((p[i] != i + 1 for i in range(n))) <= 2 else 'NO')


n = int(input())
a = [int(i) for i in input().split()]
c = 0
for i in range(n):
	c += (a[i] == i + 1) * 1
print(['YES', 'NO'][c < n - 2])


n = int(input())
P = list(map(int, input().split()))
ans = sum((a != b for (a, b) in zip(P, sorted(P))))
print('YES' if ans <= 2 else 'NO')


n = int(input())
p = list(map(int, input().split()))
ans = False
for i in range(n):
	for j in range(i, n):
		(p[i], p[j]) = (p[j], p[i])
		if all((p[k] < p[k + 1] for k in range(n - 1))):
			ans = True
		(p[i], p[j]) = (p[j], p[i])
print(['NO', 'YES'][ans])


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


n = int(input())
p = list(map(int, input().split()))
for i in range(n):
	for j in range(n):
		a = p[:]
		(a[i], a[j]) = (a[j], a[i])
		if a == sorted(a):
			print('YES')
			return
print('NO')


n = int(input())
p = [int(x) for x in input().split()]
if sum([p[i] != i + 1 for i in range(n)]) < 3:
	print('YES')
else:
	print('NO')


N = int(input())
n = list(map(int, input().split()))
count = 0
for i in range(N):
	count += n[i] != i + 1
print(('YES', 'NO')[count > 2])


n = list(map(int, list(open(0))[1].split()))
m = sorted(n)
a = b = 0
for _ in range(len(n)):
	a += 0 if n[_] == m[_] else 1
b = 1 if a > 2 else 0
print('YNEOS'[b::2])


import copy
N = int(input())
p = list(map(int, input().split()))
pcopy = copy.deepcopy(p)
ans = sorted(p)
is_ascend = False
if p == ans:
	is_ascend = True
for i in range(N):
	for j in range(i + 1, N):
		p = copy.copy(pcopy)
		(p[i], p[j]) = (p[j], p[i])
		if p == ans:
			is_ascend = True
if is_ascend:
	print('YES')
else:
	print('NO')


import numpy as np
N = int(input())
p = list(map(int, input().split()))
q = sorted(p)
(p, q) = (np.array(p), np.array(q))
if np.sum(p != q) <= 2:
	print('YES')
else:
	print('NO')


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


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


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


input()
print('YNEOS'[sum((i + 1 != int(j) for (i, j) in enumerate(input().split()))) > 2::2])


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


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


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


import sys

def solve():
	input = sys.stdin.readline
	N = int(input())
	P = [int(p) for p in input().split()]
	diff = 0
	for i in range(N):
		if P[i] != i + 1:
			diff += 1
	if diff <= 2:
		print('YES')
	else:
		print('NO')
	return 0

def __starting_point():
	solve()
__starting_point()


n = int(input())
p = tuple(map(int, input().split()))
q = sorted(p)
c = 0
for i in range(n):
	if p[i] != q[i]:
		c += 1
print('YNEOS'[c != 2 and c != 0::2])


n = int(input())
p = list(map(int, input().split()))
c = sum([x != p[i] for (i, x) in enumerate(sorted(p))])
print('YES' if c == 0 or c == 2 else 'NO')


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


n = int(input())
a = list(map(int, input().split(' ')))

def swap(a, b):
	return (b, a)
ok = 0
f = 1
for i in range(n):
	if a[i] - 1 != i:
		f = 0
if f:
	ok = 1
for i in range(n):
	for j in range(i + 1, n):
		(a[i], a[j]) = swap(a[i], a[j])
		f = 1
		for k in range(n):
			if a[k] - 1 != k:
				f = 0
		if f:
			ok = 1
		(a[i], a[j]) = swap(a[i], a[j])
print('YES' if ok else 'NO')


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


n = int(input())
s = list(map(int, input().split()))
t = s.copy()
t.sort()
cnt = 0
for i in range(n):
	if s[i] != t[i]:
		cnt += 1
if cnt < 3:
	print('YES')
else:
	print('NO')


n = input()
p = [int(i) for i in input().split()]
c = 0
for (i, j) in zip(p, sorted(p)):
	if i != j:
		c += 1
print('YES' if c <= 2 else 'NO')


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


n = int(input())
p = [int(i) for i in input().split()]
sp = sorted(p)
print('YES' if sum((p[i] == sp[i] for i in range(n))) >= n - 2 else 'NO')


n = int(input())
list_p = [int(i) for i in input().split()]
count = 0
for i in range(0, n):
	if i + 1 != list_p[i]:
		count += 1
	if count > 2:
		print('NO')
		return
print('YES')


N = int(input())
List = list(input().split())
List = list(map(int, List))
List_sorted = sorted(List)
i = 0
while i < len(List) - 1:
	j = i + 1
	ListTest = list(List)
	count = 0
	while j < len(List):
		if ListTest == List_sorted:
			print('YES')
			return
		(ListTest[i], ListTest[j]) = (ListTest[j], ListTest[i])
		if ListTest == List_sorted:
			print('YES')
			return
		else:
			ListTest = list(List)
		j += 1
	i += 1
print('NO')


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


input()
l = list(map(int, input().split()))
print('YES' if sum((i != j for (i, j) in zip(l, sorted(l)))) in (0, 2) else 'NO')


N = int(input())
(*P,) = map(int, input().split())
n_diff = 0
for (i, p) in enumerate(P, 1):
	if i != p:
		n_diff += 1
print('YES' if n_diff <= 2 else 'NO')


n = int(input())
p = list(map(int, input().split()))
q = sorted(p)
cnt = sum((p[i] != q[i] for i in range(n)))
print(['NO', 'YES'][cnt <= 2])


n = int(input())
a = list(map(int, input().split()))
print('YES' if [i == j for (i, j) in zip(a, sorted(a))].count(False) <= 2 else 'NO')


def ans135(N: int, p: str):
	p = list(map(int, p.split()))
	psort = sorted(p)
	if p == psort:
		return 'YES'
	else:
		count = 0
		for i in range(len(p)):
			if p[i] != psort[i]:
				count += 1
		if count == 2:
			return 'YES'
		else:
			return 'NO'
N = int(input())
p = input()
print(ans135(N, p))


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
print('YES' if cnt <= 2 else 'NO')


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


N = int(input())
P = list(map(int, input().split()))
ido = []
for i in range(N):
	if P[i] != i + 1:
		ido.append(P[i])
if len(ido) == 2 or len(ido) == 0:
	print('YES')
else:
	print('NO')


N = int(input())
I = list(map(int, input().split()))
n = 0
for k in range(N):
	if I[k] != k + 1:
		n = n + 1
if n == 0 or n == 2:
	print('YES')
else:
	print('NO')


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


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


x = int(input())
p = list(map(int, input().strip().split()))
k = sorted(p)
count = 0
for i in range(0, x):
	if p[i] == k[i]:
		count += 1
if x - count == 2 or x - count == 0:
	print('YES')
else:
	print('NO')


input()
(*p,) = map(int, input().split())
print('NO' if 2 < sum([1 for (i, j) in zip(p, sorted(p)) if i != j]) else 'YES')


N = int(input())
p = map(int, input().split())
l = [i != pi for (i, pi) in zip(range(1, N + 1), p)]
if sum(l) <= 2:
	print('YES')
else:
	print('NO')


n = int(input())
C = list((i for i in range(1, n + 1)))
L = list(map(int, input().split()))
cnt = 0
for (c, l) in zip(C, L):
	if c != l:
		cnt += 1
print('YES' if cnt == 0 or cnt == 2 else 'NO')


n = int(input())
p = list(map(int, input().split()))
t = list(range(1, n + 1))
a = sum([p[i] == t[i] for i in range(n)])
print('YES' if n - a == 2 or n == a else 'NO')


input()
p = list(map(int, input().split()))
a = 0
for (i, p_) in enumerate(p):
	a += p_ != i + 1
print('YNEOS'[a > 2::2])


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


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


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


n = int(input())
a = [int(i) for i in input().split()]
b = a[:]
a.sort()
d = 0
for i in range(n):
	if a[i] != b[i]:
		d += 1
if d < 3:
	print('YES')
else:
	print('NO')


N = int(input())
P = list(map(int, input().split()))
M = [i for i in range(1, N + 1)]
ans = 0
for (i, j) in zip(P, M):
	if i != j:
		ans += 1
if ans == 2 or ans == 0:
	print('YES')
else:
	print('NO')


n = int(input())
p = list(map(int, input().split()))
_p = sorted(p)
c = 0
for (a, b) in zip(p, _p):
	if a != b:
		c += 1
print('YES' if c <= 2 else 'NO')


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


N = int(input())
lst_p = list(map(int, input().split()))
cnt = sum((1 for i in range(N) if lst_p[i] != i + 1))
ans = 'YES' if cnt <= 2 else 'NO'
print(ans)


N = int(input())
p = list(map(int, input().split()))
n = [i + 1 for i in range(N)]
for i in range(N):
	if n[i] != p[i]:
		for j in range(N - i - 1):
			if n[i + j + 1] != p[i + j + 1]:
				(p[i], p[i + j + 1]) = (p[i + j + 1], p[i])
				break
if n == p:
	print('YES')
else:
	print('NO')


N = int(input())
P = [int(s) for s in input().split()]
index = []
for i in range(N):
	index.append(P.index(i + 1))
count = 0
for (i, j) in enumerate(index):
	if i != j:
		count += 1
if count > 2:
	print('NO')
else:
	print('YES')


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


input()
p = map(int, input().split())
diff = 0
for (idx, i) in enumerate(p, 1):
	if idx != i:
		diff += 1
print('YES' if diff == 0 or diff == 2 else 'NO')


N = int(input())
p = sum([int(a) - i - 1 != 0 for (i, a) in enumerate(input().split())])
if p > 2:
	print('NO')
else:
	print('YES')


(_, p) = open(0)
print('YNEOS'[sum((i != int(p) for (i, p) in enumerate(p.split(), 1))) > 2::2])


N = int(input())
p = list(map(int, input().split()))
f = 0
for i in range(1, N):
	if i != p[i - 1]:
		f += 1
print(['YES', 'NO'][f > 2])


(n, p) = open(0)
i = 1
k = 0
a = 'YES'
for j in p.split():
	if int(j) != i:
		k += 1
	if k > 2:
		a = 'NO'
	i += 1
print(a)


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


N = int(input())
P = list(map(int, input().split()))
sorted_P = sorted(P)
cnt = 0
for i in range(len(P)):
	if P[i] != sorted_P[i]:
		cnt += 1
	if cnt > 2:
		print('NO')
		return
print('YES')


n = int(input())
p = list(map(int, input().split()))
p_s = sorted(p)
s = sum((1 for i in range(n) if p[i] != p_s[i]))
print('YES' if s <= 2 else 'NO')


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


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


n = int(input())
p = list(map(int, input().split()))
r = sum([1 for (i, j) in zip(range(1, n + 1), p) if i != j])
print('YES' if r == 0 or r == 2 else 'NO')


(n, *a) = open(0)
a = list(map(int, a[0].split()))
b = sorted(a)
c = [a - b for (a, b) in zip(a, b)]
d = int(n) - c.count(0)
print('YES' if d == 0 or d == 2 else 'NO')


N = int(input())
lsp = list(map(int, input().split()))
ii = 0
for i in range(N):
	if i + 1 == lsp[i]:
		continue
	ii += 1
if ii > 2:
	print('NO')
else:
	print('YES')


(N, *P) = map(int, open(0).read().split())
A = [a != b for (a, b) in zip(range(1, N + 1), P)]
print('YNEOS'[sum(A) != 0 and sum(A) != 2::2])


n = int(input())
arr = list(map(int, input().split()))
c = 0
for (i, v) in enumerate(arr):
	if i + 1 != v:
		c += 1
print('YES' if c == 2 or c == 0 else 'NO')


(n, *x) = map(int, open(0).read().split())
soreted_x = sorted(x)
cnt = 0
for (a, b) in zip(x, soreted_x):
	if a != b:
		cnt += 1
print('YES' if cnt <= 2 else 'NO')


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return [float(x) for x in input().split()]

def LSS():
	return input().split()

def resolve():
	N = I()
	p = LI()
	cnt = len([j for (i, j) in zip(p, sorted(p)) if i != j])
	if cnt <= 2:
		print('YES')
	else:
		print('NO')

def __starting_point():
	resolve()
__starting_point()


N = int(input())
p = list(map(int, input().split()))
s = [x for x in range(1, N + 1) if x != p[x - 1]]
if len(s) < 3:
	print('YES')
else:
	print('NO')


n = int(input())
p = list(map(int, input().split()))
g = sum((1 for i in range(n) if i + 1 != p[i]))
print('YNEOS'[::2] if g == 2 or g == 0 else 'YNEOS'[1::2])


n = int(input())
l = [int(i) for i in input().split()]
mis = 0
for i in range(n):
	if l[i] != i + 1:
		mis += 1
print('YES' if mis < 3 else 'NO')


(n, *p) = map(int, open(0).read().split())
print('YNEOS'[sum((p[i] != i + 1 for i in range(n))) not in (0, 2)::2])


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


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


input()
r = sum((x != i for (i, x) in enumerate(map(int, input().split()), 1)))
print('YNEOS'[0 != r != 2::2])


n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(n - 1, -1, -1):
	for j in range(i + 1):
		if a[j] > a[i]:
			t = a[j]
			a[j] = a[i]
			a[i] = t
			count += 1
if count > 1:
	print('NO')
else:
	print('YES')


N = int(input())
p = [int(s) for s in input().split()]
print('YES' if sum(map(lambda x: x[0] == x[1], zip(p, sorted(p)))) >= N - 2 else 'NO')


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


N = int(input())
P = list(map(int, input().split()))
sp = sorted(P)
cnt = 0
for (a, b) in zip(sp, P):
	if a != b:
		cnt += 1
if cnt == 2 or cnt == 0:
	print('YES')
else:
	print('NO')


num = int(input())
str = input().split()
table = list(str)
table5 = [int(i) for i in table]
count = 0
table2 = sorted(table5)
table3 = [int(i) for i in table2]
for i in range(num):
	if table5[i] == table3[i]:
		continue
	else:
		count += 1
if count == 2 or count == 0:
	print('YES')
else:
	print('NO')


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


from operator import ne
N = int(input())
P = list(map(int, input().split()))
Q = sorted(P)
print('YES' if sum(map(ne, P, Q)) <= 2 else 'NO')


N = int(input())
vec = list(map(int, input().split()))
vec2 = [i for i in vec]
vec2.sort()
cnt = 0
for i in range(N):
	cnt += vec[i] != vec2[i]
if cnt <= 2:
	print('YES')
else:
	print('NO')


n = int(input())
ps = list(map(int, input().split()))
c = 0
for (i, p) in enumerate(ps, start=1):
	if not p == i:
		c += 1
print('YES' if c < 3 else 'NO')


N = int(input())
p = list(map(int, input().split()))
res = False
for i in range(N):
	for j in range(i, N):
		flg = True
		(p[i], p[j]) = (p[j], p[i])
		for k in range(N - 1):
			if p[k] > p[k + 1]:
				flg = False
		if flg:
			res = True
			break
		(p[i], p[j]) = (p[j], p[i])
print(['NO', 'YES'][res])


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


n = int(input())
a = list(map(int, input().split(' ')))
ok = 0
f = 1
for i in range(n):
	if a[i] - 1 != i:
		f = 0
if f:
	ok = 1
for i in range(n):
	for j in range(i + 1, n):
		(a[i], a[j]) = (a[j], a[i])
		f = 1
		for k in range(n):
			if a[k] - 1 != k:
				f = 0
		if f:
			ok = 1
		(a[i], a[j]) = (a[j], a[i])
print('YES' if ok else 'NO')


n = int(input())
plist = list(map(int, input().split()))
nlist = sorted(plist)
ng_cnt = 0
for (p, n) in zip(plist, nlist):
	if p != n:
		ng_cnt += 1
if ng_cnt > 2:
	print('NO')
else:
	print('YES')


N = int(input())
P = list(map(int, input().split()))
sP = sorted(P)
a = 0
for i in range(N):
	a += 1 if P[i] != sP[i] else 0
print('YES' if a <= 2 else 'NO')


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


input()
r = list(map(int, input().split()))
i = 1
count = 0
for item in r:
	if item != i:
		count += 1
	i += 1
print('YES' if count < 3 else 'NO')


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


n = int(input())
a = list(map(int, input().split()))
print('NYOE S'[sum([i == j for (i, j) in zip(a, sorted(a))]) >= n - 2::2])


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)


import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque, Counter
from decimal import Decimal
import functools

def v():
	return input()

def k():
	return int(input())

def S():
	return input().split()

def I():
	return map(int, input().split())

def X():
	return list(input())

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

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

def lcm(a, b):
	return a * b // math.gcd(a, b)
sys.setrecursionlimit(10 ** 6)
mod = 10 ** 9 + 7
cnt = 0
ans = 0
inf = float('inf')
al = 'abcdefghijklmnopqrstuvwxyz'
AL = al.upper()
n = k()
p = l()
for i in range(n):
	if p[i] != i + 1:
		cnt += 1
print('YES' if cnt <= 2 else 'NO')


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