Problem:


Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 50

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

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

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

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

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

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

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

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

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



Sample input:
5
5 2 3 4 1


Sample output:
YES

Submission status: Accepted

Solution code:

def ans():
    n = int(input())
    ps = list(map(int, input().split()))
    cnt = 0
    for i in range(n):
        if i+1 != ps[i]:
            cnt += 1
        if cnt >= 3:
            return False
    return True


if ans():
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
np = sorted(p)
count = 0
for i in range(n):
    if p[i] != np[i]:
        count += 1
if count in [0,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 else "NO")

Submission status: Accepted

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


def check(s):
    flag = 0
    for i in range(0, len(s) - 1):
        if s[i] > s[i + 1]:
            flag = 1
            break
    return flag


flag = check(p)
if flag == 0:
    print('YES')
else:
    for i in range(0, len(p) - 1):
        if p[i] > p[i + 1]:
            break
    for j in range(i + 1, len(p) - 1):
        if p[j] > p[j + 1]:
            break
    temp = p[j + 1]
    p[j + 1] = p[i]
    p[i] = temp

    flag = check(p)
    if flag == 0:
        print('YES')
    else:
        print('NO')

Submission status: Accepted

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

Submission status: Accepted

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

P = sorted(p)

count = 0

for i in range(N):
    if p[i] == P[i]:
        count += 1
    else:
        continue

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
li=[]
for i in range(N):
    if p[i] == i+1:
        pass
    else:
        li.append([p[i],i+1])
if li == []:
    print('YES')
elif len(li) == 2:
    if li[0][0] == li[1][1]:
        print('YES')
    else:
        print('NO')
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 i+1 != p[i]:
        count += 1
    if count > 2:
        break

res = "NO" if count > 2 else "YES"
print(res)

Submission status: Accepted

Solution code:
N = int(raw_input())
p = list(map(int,raw_input().split()))
p_sort = sorted(p)

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

Submission status: Accepted

Solution code:
#ABC135B - 0 or 1 Swap
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:
    print("YES")
elif count == 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
#ABC135B - 0 or 1 Swap
N = int(input())
p = list(map(int,input().split()))
for i in range(N):
    if p[i] != i+1:
        a = p[i]
        p[i] = i+1
        p[a-1] = a
        break
if p == [i for i in range(1,N+1,1)]:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
list_p=list(map(int,input().split()))
np_list_p=np.array(list_p)
np_list_s =np.array(sorted(list_p))
minus=list(np_list_p - np_list_s)
print("YES" if minus.count(0) == N or minus.count(0)== N-2 else "NO")

Submission status: Accepted

Solution code:
n = int(input())
lista = [int(i) for i in input().split()]
a=0
for i in range(n):
    if (i+1)!=lista[i]:
        a=a+1

if a==0:
    print("YES")
elif a==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] != sorted(p)[i]:
        c+=1
print("YES" if c == 2 or c == 0 else "NO")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#input 数列の要素数N、並び替えた数列p
#output pの要素を一組並べ替えて昇順にすることができるか "Yes" or "No"

N = int(input())
plist = tuple(map(int,input().split()))
answer_p = list(plist)
answer_p.sort()
able = False

for i in range(N-1):
    if able == True:
        break
    p = list(plist)
    j = i + 1
    for j in range(N):
        if able == True:
            break
        p = list(plist)
        p[i] = plist[j]
        p[j] = plist[i]
        if p == answer_p:
            print('YES')
            able = True

if able == False:
    print('NO')

Submission status: Accepted

Solution code:
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 or c == 0:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
dic = {}
for i in range(1, N+1):
    dic[i] = p[i-1]
 
cnt = 0
for key,val in dic.items():
    if key!=val:
        cnt += 1
 
if cnt==2 or cnt==0:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n=int(input())
p=list(map(int,input().split()))
N=list(range(1,n+1))
count=0
for x,y in zip(p,N):
    if x-y!=0:
        count+=1
print('YES' if count<=2 else 'NO')

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
false_count = 0
for index in range(len(a_list)):
  if index + 1 != a_list[index]:
    false_count += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
N = int(input())
p = list(map(int, input().split()))
sorted_p = sorted(p)
count = 0
count = len([i for i in range(N) if p[i] != sorted_p[i]])
ans = 'YES' if count == 2 or count == 0 else 'NO'
print(ans)

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

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

def check():
    cs = []
    for i in range(N):
        if A[i] != i + 1:
            cs.append(i)
    if len(cs) == 0:
        return True
    if len(cs) != 2:
        return False
    return True

if check():
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

for i_p, i_pa in zip(p, p_asc):
    if i_p != i_pa:
        diff += 1
    if diff > 2:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

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

t = sorted(p)

count = 0

for x,y in zip(p,t):
    if x != y:
        count += 1

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

Submission status: Accepted

Solution code:
N, *p = map(int, open(0).read().split())
cnt = 0
for i in range(N):
    if not 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()))

z = 0

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

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

Submission status: Accepted

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

lis_ans = sorted(lis_p)

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

z = 0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
s=input()
t=list(map(int,input().split()))
list=[]
a=int(s)
for j in range(a):
  list.append(t[j])
list.sort()
c=0
for i in range(a):
  if t[i]!=list[i]:
    c = c-1
if c<-2:
  print('NO')
else:
  print('YES')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np


def solve(N: int, p: "List[int]"):
    sorted_list = [i for i in range(1, N + 1)]
    if N - sum(np.array(p) == sorted_list) == 0:
        print("YES")
    elif N - sum(np.array(p) == sorted_list) == 2:
        print("YES")
    else:
        print("NO")


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:
#!/usr/bin/env python3
import sys
import numpy as np


def solve(N: int, p: "List[int]"):
    sorted_list = [i for i in range(1, N + 1)]
    if p == sorted_list:
        print("YES")
    elif N - sum(np.array(p) == sorted_list) == 2:
        print("YES")
    else:
        print("NO")


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:
def main():
    n, *p = map(int, open(0).read().split())

    cnt = 0
    for i, cur_p in enumerate(p):
        if i + 1 != cur_p:
            cnt += 1
        if cnt > 2:
            print('NO')
            return()
    print('YES')
    return()

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, *p = map(int, open(0).read().split())

    p_sort = sorted(p)

    cnt = 0
    for cur_p, cur_p_sort in zip(p, p_sort):
        if cur_p != cur_p_sort:
            cnt += 1
        if cnt > 2:
            print('NO')
            return()
    print('YES')
    return()

if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
p= list(map(int, input().split()))
cnt = 0
org=np.arange(1,N+1,1)
for n in range(N):
    if org[n] != p[n]:
        cnt += 1
print('NO' if cnt>2 else 'YES')

Submission status: Accepted

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

Submission status: Accepted

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

Correct = []
for i in range(N):
  Correct.append(i+1)
  
count = 0
for i in range(N):
  if(Correct[i] != P[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()))
s = list(range(1,N+ 1))
count = 0
for i in range(N):
  if s[i] != p[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()))

count = 0

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

Submission status: Accepted

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

p_sort = sorted(p)
count = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
n = int(input())
p = list(map(int, input().split()))
p_sorted = sorted(copy.copy(p))
flag = False
for i in range(n):
    swap = p[i]
    for j in range(n):
        pre_p = copy.copy(p)
        p[i] = p[j]
        p[j] = swap
        if p == p_sorted:
            flag = True
        p = pre_p
if flag:
    print("YES")
else:
    print("NO")

Submission status: Accepted

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

counter = 0

for i in range(0,N):
	if i + 1 != L[i]:
		counter += 1

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

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
count=0
pair=[]
for i in range(N):
  if p[i]== i+1:
    continue
  else:
    if count<2:
      count+=1
      pair.append(i)
    else:
      print("NO")
      exit(0)
      
print("YES")

Submission status: Accepted

Solution code:
N=int(input())
p=list(map(int,input().split()))
foundpair=False
foundi=False
sortedp=sorted(p)
pair=[]
if p==sortedp:
  print("YES")
  exit(0)
for i in range(N):
  if p[i]== sortedp[i]:
    continue
  else:
    if foundpair==False:
      if foundi:
        foundpair=True
        pair.append(i)
      else:
        foundi=True
        pair.append(i)
    else:
      print("NO")
      exit(0)
      
p[pair[0]],p[pair[1]]=p[pair[1]],p[pair[0]]
if p==sortedp:
  print("YES")
else:
  print("NO")

Submission status: Accepted

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

def is_sorted(array):
    v0 = array[0]
    for i in range(1, len(array)):
        v = array[i]
        if v0 > v:
            return False
        v0 = v
    return True
    
if is_sorted(P):
    print('YES')
    exit()

for i in range(0, N - 1):
    for j in range(i + 1, N):
        tmp = P[i]
        P[i] = P[j]
        P[j] = tmp

        if is_sorted(P):
            print("YES")
            exit()

        tmp = P[i]
        P[i] = P[j]
        P[j] = tmp

print("NO")

Submission status: Accepted

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

r = "YES"
cnt = 0
for i in range(n - 1):
	if(s[i + 1] - s[i] < 1):
		cnt += 1
		if(cnt == 1):
			a_value = s[i]
			a_index = i
		elif(cnt == 2):
			b_value = s[i + 1]
			b_index = i + 1
		elif(cnt > 2):
			r = "NO"
			break

if(cnt == 1):
	r = "NO"
elif(cnt == 2):
	s[a_index] = b_value
	s[b_index] = a_value
	if(s[a_index + 1] - s[a_index] < 1) or (s[b_index] - s[b_index - 1] < 1):
		r = "NO"

print (r)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
P=list(map(int,input().split()))
P_answer=[i+1 for i in range(N)]
swap=[0 for i in range(N)]
k=0

for i in range(N):
    if P[i]!=i+1:
        k+=1

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

Submission status: Accepted

Solution code:
n=int(input())
x=list(input().split())
for i in range(n):
  x[i]=int(x[i])
xsort=sorted(x)
flag=0
for i in range(n):
  if x[i]!=xsort[i]:
    flag+=1

if flag==0 or flag==2:
  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()
count = 0
for i, j in zip(A, B):
  if i != j:
    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()]

tmp=0
s=0
cnt=0
e=0 

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

# for i in range(0,n-1):
#    if p[i] > p[i+1]:
#       if i == 0:
#          cnt+=1
#          s=i # 交換する場所
#       else:
#          cnt+=1
#          e=i+1  # 交換する場所
#    if i == n-2 and cnt==2:
#       tmp=p[s]
#       p[s]=p[e]
#       p[e]=tmp
   
# if sorted(p) == p:
#    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(1,n+1):
    if p[i-1] == i:
        count += 1
        
if n - count <= 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]:
    cnt += 1

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

Submission status: Accepted

Solution code:
N = int(input())

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

LL = sorted(L)

count = 0

for i in range(N):
    if L[i]==LL[i]:
        pass
    else:
        count += 1

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
flag=False
ans_flag=True
for i in range(1,n):
  if(a[i-1]>a[i]):
    for j in range(n-2,-1,-1):
      if(a[j]>a[j+1]):
        tmp=a[i-1]
        a[i-1]=a[j+1]
        a[j+1]=tmp
        flag=True
        break
  if(flag==True):
    break
for i in range(n-1):
  if(a[i]>a[i+1]):
    print("NO")
    ans_flag=False
    break
if(ans_flag==True):
  print("YES")

Submission status: Accepted

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

N=int(input())
p=[int(i) for i in input().split()]
ch=[]

def judge():
    flag = 1
    for j in range(N):
        if p[j] == j+1:
            continue
        else:
            ch.append(j)
            flag = 0
    return flag,ch

flag,ch = judge()

if flag == 0:
    temp = p[ch[0]]
    p[ch[0]] = p[ch[1]]
    p[ch[1]] = temp

flag,ch = judge()

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

dif = 0
for i in range(len(a)):
    if a[i] != b[i]:
        dif +=1

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i, pi in enumerate(p):
    if (i+1) != pi:
        count+=1
    if count > 2:
        break

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(raw_input())
x = map(int, raw_input().strip().split())
y = sorted(x)
c = len([i for i in xrange(N) if x[i] != y[i]])
print "YES" if c <= 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 p[i]!=i+1:
        cnt += 1
if cnt<=2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
P = [int(_) for _ in stdin.readline().rstrip().split()]
target = 0
for i in range(N):
    if P[i] != i+1:
        target += 1 
if target <= 2:
    print('YES')
else:
    print('NO')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
p = list(int(i) for i in input().split()) 
q = sorted(p)
#print(q)
cnt = 0
for x in range(n):
    #print(p[x], q[x])
    if(p[x] != q[x]):
        cnt += 1
    if(cnt>2):
        print("NO")
        exit()

print("YES")
#print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
Pi = list(map(int, input().split()))
sortedPi = sorted(Pi)
wrongPiCnt = 0
for i in range(N):
    if Pi[i] - sortedPi[i] != 0:
        wrongPiCnt +=1
if wrongPiCnt > 2:print('NO')
else:print('YES')

Submission status: Accepted

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

cnt = 0
index = 0
nl = list(range(1, n + 1))
for pi in p:
  if nl[index] != pi:
    cnt += 1 
  index += 1

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

Submission status: Accepted

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

cnt =0

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

Submission status: Accepted

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

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    counter = 0

    for i in range(n):
        if(num_list[i] == i+1):
            continue
        else:
            counter += 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

n = int(input())
p = list(map(int,input().split()))
k = 0

for i in range(len(p)):
    if p[i]!=i+1:
        k+=1
if(k<=2):
    print("YES")
elif(k>=3):
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
ref = list(range(1, n + 1))
res = 0

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

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

Submission status: Accepted

Solution code:
#135b
N = int(input())
list_p = list(map(int, input().split()))

swap = 0

for i in range(N-1):
    if list_p[i] != i+1:
        swap+=1
        if swap>2:
            print("NO")
            break
else:
    print("YES")

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 < 3:
  print("YES")
else:
  print("NO")

Submission status: Accepted

Solution code:
n = int(input())
correct = list(range(1 , n + 1))
a = [int(_) for _ in input().split()]
wrong = 0
if n == len(a):
    for i in range(len(correct)):
        if a[i] != correct[i]:
            wrong += 1
    if 0 <= wrong <= 2:
        print('YES')
    else:
        print('NO')
else:
    print('hoge!')

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

list_correct = list(range(1, len(list_N)+1))

score = 0
for i in range(n):
    if list_correct[i] != list_N[i]:
        score = score + 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from copy import deepcopy


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

    for i in range(n):
        for j in range(i, n):
            # if i == j:
            #     continue
            p_temp = deepcopy(p)
            temp = p_temp[i]
            p_temp[i] = p_temp[j]
            p_temp[j] = temp
            # print(p, p_temp)
            if p_temp == sorted(p):
                print('YES')
                return

    print('NO')


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import copy

N = int(input())
p_list = list(map(int, input().split()))
sort_list = copy.copy(p_list)
sort_list.sort()
p_i = -1
for k in range(N):
    if p_list[k] != k+1:
        if p_i < 0:
            p_i = k
        else:
            p_list[p_i], p_list[k] = p_list[k], p_list[p_i]
            break
if p_list == sort_list:
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int,input().split()))
#print(p)
cnt =0
check=0

for x in p:
    #print('cnt'+ str(cnt))
    #print('check'+ str(check))
    cnt += 1
    #print(x)
    if x == cnt:
        continue
    else:
        check += 1

#print('cnt'+ str(cnt))
#print('check'+ str(check))

if check==0 or check==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())
p=list(map(int, input().split()))

count=0

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

Submission status: Accepted

Solution code:
n  = int(input())
p  = list(map(int,input().split()))
q  = sorted(p)
a=[]
for i in range(n):
	if not p[i]==i+1:
		a.append(p[i])

n = len(a)
if n==0 or n ==2:
	print("YES")

else:
	print("NO")

Submission status: Accepted

Solution code:
N=int(input())
list1=list(map(int,input().split()))
list2=[]
for i in range(1,N):
  list2.append(i)
a=0
for j in range(0,N-1):
  if not list1[j]==list2[j]:
    a+=1
if a>2:
  print("NO")
else:
  print("YES")

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
nums=list(map(int,input().split()))
# 3 2 4 5 1
nums_ans=sorted(nums)
dif_count=0
for i in range(n):
    if nums[i] != nums_ans[i]:
        dif_count+=1
if dif_count <= 2:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
n = int(input())
p_list = input().split()

count = 0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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