Problem:


Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0



Sample input:
3
1 3 2
3 1 2


Sample output:
3

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
count=0
a=0
b=0

for i in itertools.permutations(range(1,n+1)):
  count+=1
  if i == p:
    a=count
  if i==q:
    b=count
print(abs((count-a)-(count-b)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
l1 = tuple(map(int,input().split()))
l2 = tuple(map(int,input().split()))
l = [(i+1) for i in range(n)]
x = list(itertools.permutations(l))
s,e = 0,0
for i in range(len(x)):
	if x[i] == l1:
		s = i+1
for i in range(len(x)):
	if x[i] == l2:
		e = i+1
print(abs(e-s))

Submission status: Accepted

Solution code:
import sys
import numpy as np
from itertools import permutations, combinations
def input(): return sys.stdin.readline().rstrip()


def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    li = list(permutations(range(1, n+1)))
    li.sort()
    print(abs(li.index(p)-li.index(q)))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def main():
  N = int(input())
  P = tuple(map(int, input().split()))
  Q = tuple(map(int, input().split()))

  orders = list(itertools.permutations(range(1, N+1)))

  a = -1
  b = -1
  for i, seq in enumerate(orders):
    if seq == P:
      a = i
    if seq == Q:
      b = i

  print(abs(a-b))


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

Submission status: Accepted

Solution code:
import itertools

n = int(input())
a = list(range(1,n+1))
a = list(itertools.permutations(a,n))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

print(abs(a.index(p) - a.index(q)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
tmp = list(itertools.permutations(range(1,n+1)))
tmp = sorted(tmp)
for index,t in enumerate(tmp):
  if list(t) == P and list(t) == Q:
    p = index+1
    q = index+1
  elif list(t) == P:
    p = index+1
  elif list(t) == Q :
    q = index+1
  
print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
l=[i+1 for i in range(n)]
p_num=-1
q_num=-1
for i,peri in enumerate(itertools.permutations(l,n)):
    peri=list(peri)
    if p_num==-1:
        flag=True
        for j in range(n):
            if peri[j]!=p[j]:
                flag=False
                break
        if flag:
            p_num=i
    if q_num==-1:
        flag=True
        for j in range(n):
            if peri[j]!=q[j]:
                flag=False
                break
        if flag:
            q_num=i
    if p_num!=-1 and q_num!=-1:
        break
if p_num>q_num:
    print(p_num-q_num)
else:
    print(q_num-p_num)

Submission status: Accepted

Solution code:
import math

N = int(input())

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

def param(x):
  N = len(x)
  if N == 1:
    return 0
  
  p = x[0]
  y = x[1:N]
  for i in range(N-1):
    if y[i] > p:
      y[i] -= 1
  
  s = param(y)
  return s + int(math.factorial(N-1))*(p-1)

print(abs(param(P)-param(Q)))

Submission status: Accepted

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

Z = ['1','2','3','4','5','6','7','8']

X = Z[:N]

from itertools import permutations

A=[]
for l in list(permutations(X)):
  a = "".join(l)
  A.append(a)

# print(A)

p = "".join(P)
q = "".join(Q)

a = A.index(p)
b = A.index(q)

print( abs(a-b) )

Submission status: Accepted

Solution code:
import itertools

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

jr = list(itertools.permutations(range(1,N+1),N))

m = 0
P_num = 0
Q_num = 0

#print(P)

for i in jr:
  if list(i) == P:
    P_num = m
  if list(i) == Q:
    Q_num = m
  m += 1
  #print(m)

print(abs(P_num-Q_num))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
p=tuple(map(str,input().split()))
q=tuple(map(str,input().split()))

test=[]
for i in range(1,N+1):
    test.append(str(i))
tables=list(itertools.permutations(test))

print(abs(tables.index(p)-tables.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations
 
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
l = list(permutations(range(1,N+1)))
 
print(abs(l.index(P)-l.index(Q)))

Submission status: Accepted

Solution code:
import itertools

n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))


a=list(itertools.permutations(range(1,n+1)))

for x in range(len(a)):
  if a[x]==p:
    g=x
  if a[x]==q:
    f=x

print(abs(g-f))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
n_2 = [i for i in range(1,n+1)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))

c = 1
ans = [0,0]
for i in permutations(n_2,n):
    #print(i)
    if list(i) == p:
        ans[0] += c
    if list(i) == q:
        ans[1] += c
    c += 1

print(abs(ans[1]-ans[0]))

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys

sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

import queue
from collections import deque

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

def main():
    num = int(input())
    data1 = tuple(map(int, input().split()))
    data2 = tuple(map(int, input().split()))

    p = list(itertools.permutations([i + 1 for i in range(num)], num))

    print(abs(p.index(data1) - p.index(data2)))




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import itertools
import math
N = int(input())
P = tuple(map(int, input().split(" ")))
Q = tuple(map(int, input().split(" ")))


for idex, j in enumerate(itertools.permutations(range(1, N+1))):
    if P == j:
        a = idex+1
    if Q == j:
        b = idex + 1

print(abs(a-b))

Submission status: Accepted

Solution code:
import math
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=[i for i in range(1,n+1)]
b=[i for i in range(1,n+1)]
p1=0
q1=0
for i in range(0,n):
  x=a.index(p[i])
  p1 +=math.factorial(n-1-i)*(x-1)
  a.remove(p[i])
  y=b.index(q[i])
  q1 +=math.factorial(n-1-i)*(y-1)
  b.remove(q[i])
print(abs(p1-q1))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
num_a=0
num_b=0
cnt=0
for v in itertools.permutations(list(range(1,n+1)), n):
    if v==a:
        num_a=cnt
    if v==b:
        num_b=cnt
    cnt+=1
print(abs(num_b-num_a))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
perms = list(permutations(range(1, N+1), N))
print(abs(perms.index(P) - perms.index(Q)))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    a, b = 0, 0
    for i, e in enumerate(permutations(range(1, n + 1))):
        if e == p:
            a = i
        if e == q:
            b = i
    print(abs(a - b))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
 
l = list(itertools.permutations(range(1,N+1), N))
print(abs(l.index(P) - l.index(Q)))

Submission status: Accepted

Solution code:
from itertools import permutations

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

per_loop = map(list, (permutations(range(1, n+1))))
cnt = 0
x = []
for t in per_loop:
    if t == p or t == q:
        x.append(cnt)
    cnt += 1
if len(x) == 2: 
    print(abs(x[0]-x[1]))
else:
    print(0)

Submission status: Accepted

Solution code:
import itertools

def countOrder(n, plist, qlist):

    ans = 0
    p = 0
    q = 0

    irelist = list(itertools.permutations(range(1, n+1), n))

    for index, i in enumerate(irelist):
        if i == plist:
            p = index
        if i == qlist:
            q = index

    ans = abs(p-q)

    return ans

def main():
    n = int(input())
    plist = tuple(map(int, input().split()))
    qlist = tuple(map(int, input().split()))
    print(countOrder(n, plist, qlist))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import itertools

def solve():
    n = math.factorial(N)
#    print(n)
    arr = [i for i in range(1,N+1)]
    arr = tuple(arr)
#    print(arr)
#    print(P)
    j = 1
    for i in list(itertools.permutations(arr)):
#        print(i)
        if i == tuple(P):
            a = j
            break
        j += 1
    j = 1
    for i in list(itertools.permutations(arr)):
        if i == tuple(Q):
            b = j
            break
        j += 1
    print(abs(a-b))    

if __name__ == "__main__":
    N = int(input())
    P = list(map(int, input().split()))  
    Q = list(map(int, input().split()))
    solve()

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = tuple([int(x) for x in input().split()])
Q = tuple([int(x) for x in input().split()])
numbers = []
for i in range(N):
    numbers.append(i + 1)
numbers = tuple(numbers)

combination = sorted(list(itertools.permutations(numbers)))

a = combination.index(P)
b = combination.index(Q)

print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))

for i,p in enumerate(permutations(range(1,N+1))):
  if p == P:a=i
  if p == Q:b=i

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

def main():
    n = int(input())
    p = tuple([int(x) for x in input().split()])
    q = tuple([int(x) for x in input().split()])
    L = sorted(list(permutations(list(range(1, n+1)), n)))
    for idx, l in enumerate(L):
        if l == p:
            a = idx
        if l == q:
            b = idx
    ans = abs(a - b)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = b = 0
cnt = 0
for ptn in itertools.permutations(range(1,N+1)):
    cnt += 1
    ok1 = True
    for i in range(N):
        if P[i] != ptn[i]:
            ok1 = False
            break
    if ok1: a = cnt
    ok2 = True
    for i in range(N):
        if Q[i] != ptn[i]:
            ok2 = False
            break
    if ok2: b = cnt
print(abs(a-b))

Submission status: Accepted

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

def kaijou(n):
    ans = 1
    while n > 0:
        ans *= n
        n -= 1
    return ans

def calc(misiyou, num, cnt, x):
    if cnt == len(num):
        return x
    else:
        siyou = num[cnt]
        ind = misiyou.index(siyou)
        misiyou.remove(siyou)
        misiyou2 = misiyou.copy()
        x2 = x + (kaijou(len(misiyou2)-1))*(ind-1)
        return calc(misiyou2, num, cnt+1, x2)

lis1 = [i for i in range(N+1)]
a = calc(lis1, P, 0, 1)
lis2 = [i for i in range(N+1)]
b = calc(lis2, Q, 0, 1)
print(abs(a-b))

Submission status: Accepted

Solution code:
#25
import itertools
N = int(input())
n = [i for i in range(1,N+1)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))
cou = 0
a,b = 0,0

for i in itertools.permutations(n,N):
    cou += 1
    if p == list(i) :
        a = cou 
    if q == list(i):
        b = cou

print(abs(a-b))

Submission status: Accepted

Solution code:
# 21:04-                                                                                                                                                                                                    

from itertools import permutations

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

perms = list(permutations(range(1, N+1)))

perm2key = {perms[i]:i for i in range(len(perms))}

print(abs(perm2key[Q]-perm2key[P]))

Submission status: Accepted

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

import itertools

t=list(i+1 for i in range(n))
tt=list(itertools.permutations(t))

a=0
b=0
for c,it in enumerate(tt):
  if p==list(it):
    a = c
  if q==list(it):
    b = c

print(abs(a-b))

Submission status: Accepted

Solution code:
#c
import math

n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
orderp=0
orderq=0

for i in range(n):
  orderp+=(p[i]-1)*math.factorial(n-i-1)
  orderq+=(q[i]-1)*math.factorial(n-i-1)
  for j in range(i+1,n):
    if p[j]>p[i]:
      p[j]-=1
    if q[j]>q[i]:
      q[j]-=1
  #print(orderp,orderq)
  #print(p,q)


print(abs(orderp-orderq))

Submission status: Accepted

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

import itertools

l=list(itertools.permutations(range(1,n+1)))
#print(l)
print(abs(l.index(p)-l.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
itr = permutations([i for i in range(1, n+1)])
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

for i, tpl in enumerate(itr):
    if tpl == p:
        val1 = i
    if tpl == q:
        val2 = i

print(abs(val1-val2))

Submission status: Accepted

Solution code:
N = int(input())
P = input().replace(" ","")
Q = input().replace(" ","")

from collections import deque
q = deque()
q.append("")

a = -1
b = -1
cnt = 0
while q:
  cnt += 1
  st = q.popleft()
  if len(st) == N:
    if st == P:
      a = cnt
    if st == Q:
      b = cnt
    continue
  for i in range(1, N + 1):
    if str(i) not in st:
      q.append(st + str(i))

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = input().replace(" ","")
Q = input().replace(" ","")

from collections import deque
q = deque()
q.append("")

a = -1
b = -1
cnt = 0
while q:
  cnt += 1
  st = q.popleft()
  if len(st) == N:
    if st == P:
      a = cnt
    if st == Q:
      b = cnt
    continue
  for i in range(1, N + 1):
    if str(i) not in st:
      q.append(st + str(i))

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = input().replace(" ","")
Q = input().replace(" ","")

from collections import deque
q = deque()
for i in range(1, N + 1):
  q.append(str(i))

a = -1
b = -1
cnt = 0
while q:
  cnt += 1
  st = q.popleft()
  if len(st) == N:
    if st == P:
      a = cnt
    if st == Q:
      b = cnt
    continue
  for i in range(1, N + 1):
    if str(i) not in st:
      q.append(st + str(i))

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))

a = -1
b = -1
cnt = 0
for perm in itertools.permutations(range(1, N + 1), N):
  cnt += 1
  if P == perm:
    a = cnt
  if Q == perm:
    b = cnt
    
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
num_list = list(itertools.permutations(range(1,n+1)))
print(abs(num_list.index(p)-num_list.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
N = [i for i in range(1, n+1)]
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
all = [p for p in permutations(N, n)]
ans = abs(all.index(P) - all.index(Q))
print(ans)

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())

pertuple = tuple(permutations([x for x in range(1,N+1)]))

P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))

print(abs(pertuple.index(P) - pertuple.index(Q)))

Submission status: Accepted

Solution code:
n = int(input())
from itertools import permutations
itr = permutations([i for i in range(1, n + 1)])
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

for i, tpl in enumerate(itr):
    if tpl == p:
        idx1 = i
    if tpl == q:
        idx2 = i
print(abs(idx1 - idx2))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))

num = [0]*n
for i in range(n):
    num[i] = i+1

cnt = 1
for i in permutations(num):
    if P == list(i):
        a = cnt + 1
    if Q == list(i):
        b = cnt + 1
    cnt += 1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
dic = list(itertools.permutations(range(1,n+1)))
for i in range(len(dic)):
    if dic[i] == p:
        p_num = i
    if dic[i] == q:
        q_num = i
print(abs(p_num - q_num))

Submission status: Accepted

Solution code:
import itertools as it
import numpy as np
import math

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

rosen = list(it.permutations(range(1, N+1)))

isP = 0
isQ = 0

for i in range (0, len(rosen)):
    if list(rosen[i]) == P:
        isP = i+1
    if list(rosen[i]) == Q:
        isQ = i+1

print(abs(isP-isQ))

Submission status: Accepted

Solution code:
n = int(input())
from itertools import permutations
itr = permutations([i for i in range(1, n + 1)])
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
for i, tpl in enumerate(itr):
  if tpl == p:
    idx1 = i
  if tpl == q:
    idx2 = i
print(abs(idx1 - idx2))

Submission status: Accepted

Solution code:
###==================================================
### import
#import bisect
#from collections import Counter, deque, defaultdict
#from copy import deepcopy
#from functools import reduce, lru_cache
#from heapq import heappush, heappop
import itertools
#import math
#import string
import sys
### stdin
def input(): return sys.stdin.readline().rstrip()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip('\n') for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]
### stdout
def OutH(lst, deli=' '): print(deli.join(map(str, lst)))
def OutV(lst): print('\n'.join(map(str, lst)))
### setting
sys.setrecursionlimit(10 ** 6)
### utils
int1 = lambda x: int(x) - 1
### constants
INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)
###---------------------------------------------------

N = iIn()
P = tuple(iInLH())
Q = tuple(iInLH())

lst = list(itertools.permutations(range(1, N+1)))

for i in range(len(lst)):
    if lst[i] == P:
        idx_p = i
    if lst[i] == Q:
        idx_q = i

ans = abs(idx_p - idx_q)
print(ans)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
First_List = list(map(int,input().split()))
Second_List = list(map(int,input().split()))
Num_List = sorted(First_List)
F_Flg = 0
S_Flg = 0
Loop_Num = 1
for Sorted_Pair in itertools.permutations(Num_List, N):
    Search_Pair = list(Sorted_Pair)
    if (Search_Pair == First_List) & (F_Flg == 0):
        F_Flg = Loop_Num
    if (Search_Pair == Second_List) & (S_Flg == 0):
        S_Flg = Loop_Num
    
    if (F_Flg != 0) & (S_Flg != 0):
        break
    else:
        Loop_Num += 1

print(abs(F_Flg-S_Flg))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
First_List = list(map(int,input().split()))
Second_List = list(map(int,input().split()))
Num_List = sorted(First_List)
F_Flg = 0
S_Flg = 0
Loop_Num = 1
for Sorted_Pair in itertools.permutations(Num_List, N):
    Search_Pair = list(Sorted_Pair)
    if (Search_Pair == First_List) & (F_Flg == 0):
        F_Flg = Loop_Num
    if (Search_Pair == Second_List) & (S_Flg == 0):
        S_Flg = Loop_Num
        
    
    Loop_Num += 1

print(abs(F_Flg-S_Flg))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

ls = list(itertools.permutations(range(1,n+1))) #順列生成

#print(ls)

print(abs(ls.index(p) - ls.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
count = 0
a,b = 0,0
for i in permutations([j+1 for j in range(N)],N):
  count += 1
  if list(i) == P:
    a = count
    if list(i) == Q:
      b = count
      break
  elif list(i) == Q:
    b = count
  else:
    continue
print(abs(a-b))

Submission status: Accepted

Solution code:
N=int(input())

import itertools
l=[]
for i in range(1,N+1):
    l.append(i)
L=list(itertools.permutations(l,N)) #Lの中から2個
L.sort()
#print(L)
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
a=10**9
b=10**9
for i in range(len(L)):
    if L[i]==P:
        a=i
    if L[i]==Q:
        b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

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

n_perm = list(itertools.permutations(range(1, n + 1)))

print(abs(n_perm.index(tuple(p)) - n_perm.index(tuple(q))))

Submission status: Accepted

Solution code:
import itertools

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

l_pattern = list(itertools.permutations([i + 1 for i in range(N)]))
P_count_order = 0
Q_count_order = 0
for i,v in enumerate(l_pattern):
    if list(v) == P:
        P_count_order = i+1
    if list(v) == Q:
        Q_count_order = i+1
print(abs(P_count_order-Q_count_order))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(input().replace(" ",""))
Q = tuple(input().replace(" ",""))
Nc = list(itertools.permutations(map(str,list(range(1,N+1)))))
print(abs(Nc.index(Q) - Nc.index(P)))

Submission status: Accepted

Solution code:
import sys
from copy import deepcopy


def perm(head: list, rest: list, result: list):
    if len(rest) == 0:
        # restを消費仕切ったら、結果に渡してあげる
        result.append(head)
        return
    for r in rest:
        restx = deepcopy(rest)
        restx.remove(r)

        headx = deepcopy(head)
        headx.append(r)

        perm(headx, restx, result)


def main():
    N = int(input())
    p = [int(x) for x in input().split()]
    q = [int(x) for x in input().split()]
    # 結果様のリストを渡さないといけない
    res = []
    perm([], [i for i in range(1, N+1)], res)

    p_i = 0
    q_i = 0
    for i,l in enumerate(res):
        if l == p:
            p_i = i
        if l == q:
            q_i = i
    print(abs(p_i-q_i))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
nums = [i+1 for i in range(n)]
ls = list(itertools.permutations(nums))

ans = abs(ls.index(p) - ls.index(q))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
p = input().replace(" ", "")
q = input().replace(" ", "")

array_list = [""]
for i in range(1, n + 1):
    array_list_next = []
    for array in array_list:
        for j in range(1, n + 1):
            if str(j) not in array:
                array_list_next.append(array + str(j))
    array_list = array_list_next

print(abs(array_list.index(p) - array_list.index(q)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
P = list(map(int, input().strip().split()))
Q = list(map(int, input().strip().split()))

P_s = tuple(P)
Q_s = tuple(Q)

p_n = 0
q_n = 0

j = 1
for i in itertools.permutations(range(1, n + 1), n):
    if i == P_s:
        p_n = j
    if i == Q_s:
        q_n = j
    j += 1
print(abs(p_n - q_n))

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import statistics
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
per_lis = itertools.permutations(list(range(1,n+1)))
a,b,i=0,0,0
for elem in per_lis:
    i+=1
    if elem==p:
        a=i
    if elem==q:
        b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import statistics
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
per_lis = itertools.permutations(list(range(1,n+1)))
a=0
b=0
i=0
for elem in per_lis:
    i+=1
    if elem==p:
        a=i
    if elem==q:
        b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
jyunretsu=list(itertools.permutations(list(range(1,n+1))))
print(abs(jyunretsu.index(p)-jyunretsu.index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations

n=int(input())
plst=list(map(int,input().split()))
qlst=list(map(int,input().split()))

token=0
for x in permutations(range(1,n+1)):
    if list(x)==plst : p=token
    if list(x)==qlst : q=token
    token+=1
print(abs(p-q))

Submission status: Accepted

Solution code:
from itertools import permutations
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
a=list(permutations(range(1,n+1)))
print(abs(a.index(p)-a.index(q)))

Submission status: Accepted

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

l = list(__import__('itertools').permutations(range(1, int(input())+1)))
print(abs(l.index(tuple(map(int, input().split()))) - l.index(tuple(map(int, input().split())))))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = sorted(list(itertools.permutations(range(1, N + 1), N)))
print(abs(l.index(P)- l.index(Q)))

Submission status: Accepted

Solution code:
import sys
import itertools

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
P = tuple(LI())
Q = tuple(LI())

li = list(itertools.permutations(range(1,N+1)))

p_index = li.index(P)
q_index = li.index(Q)
print(abs(p_index-q_index))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
nl = [i for i in range(1, n+1)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
p = tuple(p)
q = tuple(q)
l = list(permutations(nl))
a = l.index(p)
b = l.index(q)
print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

if P == Q:
    print(0)
    sys.exit()

perm = list(permutations(P))
perm.sort()
p, q = -1, -1
for i, pm in enumerate(perm):
    if pm == P:
        p = i
    if pm == Q:
        q = i
print(abs(p - q))

Submission status: Accepted

Solution code:
import itertools
 
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
 
l = []
a, b,num = 0,0,0
 
for m in range(1,n+1):
	l.append(m)
 
for i in itertools.permutations(l):
	num += 1
	if i == p:
		a = num
	if i == q:
		b = num
print(abs(a-b))

Submission status: Accepted

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

import math
ans = 0
for i in range(N):
  p = P[i]
  q = Q[i]
  for j in range(i):
    if P[i] > P[j]:
      p -= 1
    if Q[i] > Q[j]:
      q -= 1
  ans += (p-q)*(math.factorial(N-i-1))
print(abs(ans))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))

l = []
a, b,num = 0,0,0

for m in range(1,n+1):
	l.append(m)

for i in itertools.permutations(l):
	num += 1
	if i == p:
		a = num
	if i == q:
		b = num
print(abs(a-b))

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import itertools
    N = int(input())
    P = tuple([int(item) for item in input().split()])
    Q = tuple([int(item) for item in input().split()])
    # print(Q)
    all = list(itertools.permutations(range(1,N+1)))
    a = 0
    b = 0
    for i, item in enumerate(all):
        # print(item)
        if item == P:
            a = i
        if item == Q:
            b = i

    print(abs(a-b))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
a = map(str, range(1, n+1))
a = "".join(a)
l = list(permutations(a, n))

p = tuple(input().split())
q = tuple(input().split())

print(abs(l.index(p)-l.index(q)))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
a = [i for i in range(1,N+1)]
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

kaijo = 1
for i in range(N):
  kaijo *= (N - i)
A = list(itertools.permutations(a, N))
ansP = 0
for i in range(kaijo):
  if(list(A[i]) == P):
    ansP = i
ansQ = 0
for i in range(kaijo):
  if(list(A[i]) == Q):
    ansQ = i
print(abs(ansQ-ansP))

Submission status: Accepted

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

from itertools import permutations as per
a = list(per([i for i in range(1,n+1)]))

print(abs(a.index(p)-a.index(q)))

Submission status: Accepted

Solution code:
import math
import itertools
#k,x = list(list(map(int, input().split())))
n = int(input())
a = list(list(map(int, input().split())))
b = list(list(map(int, input().split())))
ans = 0

for i in itertools.permutations(range(1,n+1,1),n):

    ans +=1
    if list(i) == a:
        ansa = ans
    if list(i) == b:
        ansb = ans


print(abs(ansa-ansb))