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

Solution code:
x = int(input())
for i in range(x):
  exec("a_%d = int(input())" %(i))
a_list = [eval("a_%d" %(i)) for i in range(x)]
a_list.sort()
be_list = [eval("a_%d" %(i)) for i in range(x)]
count = 0
for i in range(x):
    if a_list[i] == be_list[i]:
      count += 1
if count >= x-2:
  print('YES')
else:
  print('NO')

Submission status: Runtime Error

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

for i in range(N-1):
    if p[i] > p[i+1]:
        cnt += 1
        l.append(i)

if cnt <= 1:
    print('YES')
elif (cnt==2) and (p[l[0]-1] <= p[l[1]+1] <= p[l[0]+1]) and (p[l[1]] <= p[l[0]] <= p[l[1]+2]):
    print('YES')
else: print('NO')

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
li=[]
for i in range(N):
  if A[i]!=AA[i]:
    li.append(A[i])
if len(li)>2:
  print("NO")
else:
  print("YES")

Submission status: Runtime Error

Solution code:
a = int(input())

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

d = 0
for i in range(a):
  if not p[i] == a-i:
    d += 1
      
print(d)
    
  
if d == 0 or d == 2:
  print("YES")
  
else:
  print("NO)

Submission status: Runtime Error

Solution code:
n = int(input())
ps = map(int, input().split())
p2 = ps[0] - 1
for p in ps:
    if p > p2:
        p2 = p

    else:
        print("NO")
if p2 == p[-1]:
    print("YES")

Submission status: Runtime Error

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


for i in range(n):
    for j in range(n):
        temp = list(copy(p))
        temp[i], temp[j] = temp[j], temp[i]
        sorted_temp = sorted(temp)

        if sorted_temp  == temp:
            print("YES")
            exit()

print("NO")

Submission status: Runtime Error

Solution code:
N=int(read_input())
P=[int(x) for x in read_input().split()]

N_J=list(range(1,N+1))
ans='NO'
for i in range(N):
    for j in range(N):
        P_temp=P.copy()
        p_i=P[i]
        p_j=P[j]

        P_temp[i]=p_j
        P_temp[j]=p_i
        
        if P_temp==N_J:
            ans='YES'
            
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
n = int(sys.stdin.readline())
p = [int(i) for i in sys.stdin.readline().split()]

if p == sorted(p):
    print('YES')
else:
    swap_index = []
    for i, v in enumerate(p[:-1]):
        if v > p[i + 1]:
            if len(swap_index) == 0:
                swap_index.append(i)
            else:
                swap_index.append(i + 1)
                break
    p[swap_index[0]], p[swap_index[1]] = p[swap_index[1]], p[swap_index[0]]

    if p == sorted(p):
        print('YES')
    else:
        print('NO')

Submission status: Runtime Error

Solution code:
N=int(input())
P=[int(x) for x in input().split()]
 
N_J=list(range(1,N+1))
ans='NO'
for i in range(N):
    for j in range(N):
        P_temp=P.copy()
        p_i=P[i]
        p_j=P[j]
 
        P_temp[i]=p_j
        P_temp[j]=p_i
        
        if P==N_J:
            ans='YES'
print(ans)
N=int(input())
P=[int(x) for x in input().split()]

N_J=list(range(1,N+1))
ans='NO'
for i in range(N):
    for j in range(N):
        P_temp=P.copy()
        p_i=P[i]
        p_j=P[j]

        P_temp[i]=p_j
        P_temp[j]=p_i
        
        if P==N_J:
            ans='YES'
print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

dic = {}
for i in range(n-1):
    a = p[i+1]-p[i]
    if a in dic and a != 1:
        dic[a] += 1
    elif a not in dic and a != 1:
        dic[a] = 1

flag = False
for k,v in dic.values():
    if i == 2 :
        flag = True
        break
    else:
        continue

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split())
b = a.sorted()
c = list(filter(a[i]!=b[i], range(len(a)))
print('YES' if len(c)==2 else 'NO')

Submission status: Runtime Error

Solution code:
N = int(input())
p = list(map(int, input().split()))
P = list(range(1,N+1))
tmp = [P[i]-p[i] for i in range(N)]
tmp = set(tmp)
tmp.discard(0)
if not tmp:
    print('YES')
elif len(tmp) == 2 and abs(tmp[0]) == abs(tmp[1]):
    print('YES')
else:
    print('NO')

Submission status: Runtime Error

Solution code:
n=int(input())  #数値入力 「N」だけの入力のとき
c=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
d=[i for i in range(1,n+1)]
f=0
for i in range(n):
    if d[i]-d[i]:
        f+=1
if f == 0 or f == 2:
    print("YES")
else:
    print("NO)

Submission status: Runtime Error

Solution code:
import sys

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

different_index = -1
should_place = -1
for i, p in enumerate(p_list):
    if different_index >= 0 and p != i + 1 and i != should_place:
        print("NO")
        sys.exit(1)

    if different_index == -1 and p != i + 1:
        different_index = i
        should_place = p - 1
        continue
    

if different_index == -1:
    print("YES")
elif p_list[should_place] == different_index + 1:
    print("YES")
else:
    print("NO")

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    p = string_to_int(inputs[0])

    diff_point = []
    for i, num in enumerate(range(1, len(p)+1)):
        if num != p[i]:
            diff_point.append(i)
    if len(diff_point) == 2 | | len(diff_point) == 0:
        return "YES"
    return "NO"


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Runtime Error

Solution code:
N = int(input())
al = list(map(int,input().split()))
ansal = sorted(al)

for i in range(N):
    for j in range(i,N):
        cond_al = al.copy()
        cond_al[i],cond_al[j] = cond_al[j],cond_al[i]
        if(cond_al==ansal):
            print("YES")
            exit(1)

print("NO")

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
AA=sorted(A)
li=[]
li_i=[]
for i in range(len(A)):
  if A[i]!=AA[i]:
    li_i.append(i)
    li.append(A[i])
    if len(li_i)>2:
      ans="NO"
      break
s=li_i[0]
t=li_i[1]
A[s]=li[1]
A[t]=li[0]
if A==AA:
  ans="YES"
else:
  ans="NO"
print(ans)

Submission status: Runtime Error

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


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

Submission status: Runtime Error

Solution code:
N = int(input())
p_s = list(map(int,input().split()))
cnt = 0
for i,p in enumerate(p_s):
  	if i+1 != p:
      	cnt += 1
    if cnt > 2:
      	break
else:
  	print("YES")
print("NO")

Submission status: Runtime Error

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

l = sorted(p)
count = 0

for a, b in zip(p, l):
    if (a is not b): count += 1

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

Submission status: Runtime Error

Solution code:
N = int(input())
p = [int(x) for x in input().split()]
a = sorted(P)
c = 0
for i in range(N):
  if p[i] != a[i]:
    c+=1
if c <= 2 :
  print("YES")
if c >3:
  print("No")

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
A=[int(i) for i in input().split()]
B=[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")