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:
from itertools import permutations 
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
X = list(P)
lst = []
for x in permutations(sorted(X), N):
  lst.append(x)
print(abs(lst.index(Q) - lst.index(P)))

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    p=list(map(int,input().split()))
    q=list(map(int,input().split()))
    fact = [1]
    for i in range(1,9):
        fact.append(i*fact[i-1])
    tmp = []
    for i in range(n):
        tmp.append(i+1)
    i=0
    ansp = caldict(fact,i,p,tmp)
    tmp = []
    for i in range(n):
        tmp.append(i+1)
    i=0
    ansq = caldict(fact,i,q,tmp)

    print(abs(ansp-ansq))
def caldict(f,i,p,tmp):
    if i < len(p)-1:
        nowi = tmp.index(p[i])
        now = tmp.pop(nowi)
        i+=1
        return nowi * f[len(tmp)] + caldict(f,i,p,tmp)
    else:
        return 1


if __name__=='__main__':
    main()

Submission status: Accepted

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

l = [int(i) for i in range(1, N+1)]

r = sorted(list(p for p in itertools.permutations(l)))
# print(r)

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
i = 1
Nin = [ i for i in range(1,N+1)]
for li in itertools.permutations( Nin ):
  li = list(li)
  if li == P:
    pnum = i
  if li == Q:
    qnum = i
  i += 1
print(abs(pnum-qnum) )

Submission status: Accepted

Solution code:
import itertools
n = int(raw_input())
p = map(int, raw_input().split())
q = map(int, raw_input().split())

ls = range(1, n+1)
seq = list(itertools.permutations(ls))
p_idx = seq.index(tuple(p)) + 1
q_idx =  seq.index(tuple(q)) + 1
print abs(p_idx - q_idx)

Submission status: Accepted

Solution code:
from itertools import permutations
from sys import stdin
N = int(stdin.readline().rstrip())
P = tuple([int(_) for _ in stdin.readline().rstrip().split()])
Q = tuple([int(_) for _ in stdin.readline().rstrip().split()])
pidx, qidx = 0, 0
for idx, p in enumerate(list(permutations(i for i in range(1, N+1))), start=1):
    if p == P:
        pidx = idx
    if p == Q:
        qidx = idx
print(abs(pidx - qidx))

Submission status: Accepted

Solution code:
import math

n = int(input())
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
diff = 0

for i in range(n-1):
    diff += math.factorial(n-1-i) * (a[i] - b[i])
    for j in range(n-1-i):
        if a[i+1+j] > a[i]:
            a[i+1+j] -= 1
            
        if b[i+1+j] > b[i]:
            b[i+1+j] -= 1

print(abs(diff))

Submission status: Accepted

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

import itertools
seq = list(itertools.permutations([i for i in range(1,n+1)]))

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

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

n_list = [i + 1 for i in range(N)]
p_list = list(itertools.permutations(n_list))

ans = abs(p_list.index(P) - p_list.index(Q))

print(ans)

Submission status: Accepted

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

a = 0
b = 0
import itertools
for r in list(itertools.permutations(range(1, n+1))):
  for i in range(n):
    if r[i] < p[i]:
      a += 1
      break
    elif r[i] > p[i]:
      break
    else:
      continue
  for i in range(n):
    if r[i] < q[i]:
      b += 1
      break
    elif r[i] > q[i]:
      break
    else:
      continue
      
print(abs(a-b))

Submission status: Accepted

Solution code:
import math
n=int(input())
A,B=[list(range(1,n+1)) for i in range(2)]
P,Q=[list(map(int,input().split())) for i in range(2)]
p=0
for i in range(n-1):
    p+=(A.index(P[i]))*math.factorial(n-i-1)
    A.remove(P[i])
q=0
for i in range(n-1):
    q+=(B.index(Q[i]))*math.factorial(n-i-1)
    B.remove(Q[i])
print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools as it
s=list(range(1,int(input())+1))
A=list(it.permutations(s))
P,Q=[tuple(map(int,input().split())) for i in range(2)]
print(abs(A.index(P)-A.index(Q)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = II()
    p = LI_()
    q = LI_()
    l = itertools.permutations(range(n), n)
    l = list(l)
    l.sort()
    print(abs(l.index(tuple(p)) - l.index(tuple(q))))
    
    return


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

Submission status: Accepted

Solution code:
from itertools import permutations
import math
read = input 
rn = lambda :list(map(int, read().split()))
n, = rn()
init_list = range(1, n + 1)
l_1 = rn()
l_2 = rn()
i_1 = -1
i_2 = -1
# print(l_1, l_2)
for i,p in enumerate(permutations(init_list)):
    p = list(p)
#     print(i, p)
    if p == l_1:
        i_1 = i
    if p == l_2:
        i_2 = i
print(abs(i_1 - i_2))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))
p=0
q=0
p_flag=False
q_flag=False
anslist=list(range(1,N+1))
li=list(map(list,itertools.permutations(anslist)))
for i in range(len(li)):
  if P==li[i]:
    p=i
    p_flag=True
  if Q==li[i]:
    q=i
    q_flag=True
  if p_flag and q_flag:
    break
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()))
a = []
ref = []
for i in range(N):
    a.append(i+1)

ref = list(itertools.permutations(a))

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

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(list(range(1,n+1))))
print(abs(l.index(p) - l.index(q)))

Submission status: Accepted

Solution code:
import itertools
def solve(n, p, q):
    p = tuple(p)
    q = tuple(q)
    v = [i+1 for i in range(n)]
    for i, x in enumerate(itertools.permutations(v)):
        if p == x:
            a = i
        if q == x:
            b = i
    return abs(a-b)

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

Submission status: Accepted

Solution code:
from itertools import *

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

LUT = list(permutations(range(1, N + 1))).index
print(abs(LUT(P) - LUT(Q)))

Submission status: Accepted

Solution code:
from math import factorial


def get_rank(l, n):
    s = 0
    es = set(range(1, n + 1))
    for i, e in enumerate(l):
        s += len([j for j in es if j < e]) * factorial(n - (i + 1))
        es -= set([e])
    return s + 1


def main():
    n = int(input())
    p = [int(i) for i in input().split(' ')]
    q = [int(i) for i in input().split(' ')]

    print(abs(get_rank(p, n) - get_rank(q, n)))


main()

Submission status: Accepted

Solution code:
from itertools import permutations

N, *PQ = map(int, open(0).read().split())
P, Q = tuple(PQ[:N]), tuple(PQ[N:])

A = tuple(permutations(range(1, N + 1)))
a = A.index(P)
b = A.index(Q)

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

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

Submission status: Accepted

Solution code:
N = int(input())
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())

import itertools
L = list(itertools.permutations(list(range(1,N+1))))
print(abs(L.index(Q) - L.index(P)))

Submission status: Accepted

Solution code:
def wczytaj_liste(): 
    wczytana_lista = input()
    lista_znakow = wczytana_lista.split()
    ostateczna_lista = []
    for element in lista_znakow:
        ostateczna_lista.append(int(element))
    return ostateczna_lista 

def wartosc_bezwgledna(A): 
    if A >= 0: 
        return A
    else: 
        return -A 
    
def silnia(A): 
    if A == 0:
        return 0
    odp = 1
    for i in range(1, A+1):
        odp *= i
    return odp

from itertools import permutations 

def generuj_permutacje(N):
    lista = []
    if N == 0:
        return []
    if N == 1:
        return [1]
    else: 
        for i in range(1,N+1):
            lista.append(i)
    perm = permutations(lista)
    odp = []
    for i in list(perm): 
        odp.append(i)
    return odp  

def caunt_order(): 
    N = wczytaj_liste()[0]
    P = tuple(wczytaj_liste())
    Q = tuple(wczytaj_liste())
    permutacje = generuj_permutacje(N)
    for i in range(len(permutacje)):
        if P == permutacje[i]:
            nr_P = i
        if Q == permutacje[i]:
            nr_Q = i
    odp = nr_P - nr_Q
    print(wartosc_bezwgledna(odp))

caunt_order()

Submission status: Accepted

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

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

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

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())
 
L = list(itertools.permutations(list(range(1,N+1))))
print(abs(L.index(Q) - L.index(P)))

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

L = list(itertools.permutations(list(range(1,N+1))))
print(abs(L.index(Q) - L.index(P)))

Submission status: Accepted

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

T = list(itertools.permutations(range(1, N+1), N))
for i in range(len(T)):
    if T[i] == P:
        s = i
    if T[i] == Q:
        d = i
print(abs(s-d))

Submission status: Accepted

Solution code:
import itertools
A=list(itertools.permutations(range(1,int(input())+1)))
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 = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
L = list(itertools.permutations(list(range(1, N+1))))

ans = abs(L.index(P)-L.index(Q))
print(ans)

Submission status: Accepted

Solution code:
import itertools

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


#print(p)
seq = [i for i in range(1,n+1)]
l = list(itertools.permutations(seq))
l.sort()

a = l.index(tuple(p))
b = l.index(tuple(q))

#print(l[0],p)
print(abs(a-b))

Submission status: Accepted

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

c=0
def dfs(X):
  global a,b,c
  if len(X)==N:
    c+=1
    if X==P:
      a=c
    if X==Q:
      b=c
  else:
    for i in range(1,N+1):
      if i not in X:
        X.append(i)
        dfs(X)
        X.remove(i)

dfs([])
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 fac(n):
    res = 1
    for i in range(2, n+1):
        res *= i
    return res


ans = 0
for _ in range(2):
    res = 0
    use = [1] * (n + 1)
    for i in range(n):
        k = sum(use[1:p[i]])
        res += k * fac(n-i-1)
        use[p[i]] = 0
    p = q
    ans = abs(ans - res)
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

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

allpat = list(itertools.permutations(seq))

p_num = 0
q_num = 0

for i in range(len(allpat)):
    if allpat[i] == p:
        p_num = i
    if allpat[i] == q:
        q_num = i

print(abs(p_num-q_num))

Submission status: Accepted

Solution code:
n = int(input())
def bub(x,l):
  if x == n:
    return [tuple(l)]
  ret = list()
  for i in range(1,n+1):
    if i not in l:
      ret += bub(x+1,l+[i])
  return ret
a = bub(0,list())
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())

seq = []
for i in range(1,n+1):
  seq.append(i)
  
#print(seq)

per = list(itertools.permutations(seq))


#print(type(per[0]))

A = tuple(map(int, input().split()))
#print(A)

#print(per.index(A))

B = tuple(map(int, input().split()))
#print(B)

#print(per.index(B))

print(abs(per.index(B)-per.index(A)))

Submission status: Accepted

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

px = 1
lst = [i + 1 for i in range(n)]
tmp = 1 
for i in range(n - 1):
  tmp *= i + 1
for i, x in enumerate(p[:-1]):
  idx = lst.index(x)
  lst.pop(idx)
  px += idx * tmp
  tmp //= n - i - 1

qx = 1
lst = [i + 1 for i in range(n)]
tmp = 1 
for i in range(n - 1):
  tmp *= i + 1
for i, x in enumerate(q[:-1]):
  idx = lst.index(x)
  lst.pop(idx)
  qx += idx * tmp
  tmp //= n - i - 1

print(abs(px - qx))

Submission status: Accepted

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

fact = [1]*10
for i in range(2,10):
    fact[i] = fact[i-1]*i

index1 = 1
l = [i for i in range(n)]
while len(p)>0:
    i = p.pop(0)
    l.remove(i)
    cnt = len([k for k in l if k < i])
    index1 += cnt * fact[len(l)]


index2 = 1
l = [i for i in range(n)]
while len(q)>0:
    i = q.pop(0)
    l.remove(i)
    cnt = len([k for k in l if k < i])
    index2 += cnt * fact[len(l)]

print(abs(index1-index2))

Submission status: Accepted

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

# print(p)
count = 1
for i in itertools.permutations([i+1 for i in range(N)]):
    if list(i) == p:
        a = count
    if list(i) == q:
        b = count
    count += 1

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

l = list(permutations(range(1, n+1)))

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

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools


def main():
    n = int(input())
    p = tuple(input().split())
    q = tuple(input().split())

    a = list(itertools.permutations([str(i+1) for i in range(n)]))
    print(abs(a.index(q)-a.index(p)))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def sort(P, Q):
    N = len(P)
    for i in range(N):
        if P[i] < Q[i]:
            return (P, Q)
        elif P[i] > Q[i]:
            return (Q, P)
    else:
        return (-1, -1)

def swap(array, i, j):
    temp = array[i]
    array[i] = array[j]
    array[j] = temp

    return array

def get_next_bigger(array):
    N = len(array)
    # 最後尾から要素iと要素i-1を比較して「要素i-1 < 要素i」のiを探す
    for i in reversed(range(N)):
        if array[i-1] < array[i]:
            # array[i:N]の中で、array[i-1]より大きい要素でかつ最小の要素をarray[i-1]と入れ替える
            minimum_number = N + 1
            minimum_element = i-1
            for j in range(i, N):
                if array[i-1] < array[j] and minimum_number > array[j]:
                    minimum_number = array[j]
                    minimum_element = j
            temp_array = swap(array, i-1, minimum_element)
            new_array = temp_array[:i]
            temp_array2 = temp_array[i:]
            temp_array2.sort()
            new_array.extend(temp_array2)
            return new_array

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

# 辞書順で小さいほうを選択
(minimum, maximum) = sort(P, Q)

if minimum == -1:
    print(0)
    sys.exit()

# 小さいほうを一つずつ大きくしてもう一方と同じになるまでの手数が答え
distance = 0
while True:
    if minimum == maximum:
        break
    else:
        distance += 1
    
    # minimumを一つ大きくする
    new_minimum = get_next_bigger(minimum)
    minimum = new_minimum

print(distance)

Submission status: Accepted

Solution code:
import itertools

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

p_idx, q_idx = -1, -1
for idx, item in enumerate(itertools.permutations(list(range(1, n+1)))):
    if p == list(item):
        p_idx = idx
    if q == list(item):
        q_idx = idx
    if p_idx > 0 and q_idx > 0:
        break

print(abs(p_idx - q_idx))

Submission status: Accepted

Solution code:
import itertools
from collections import defaultdict
# 順列生成
def permutation(list_):
    return list(itertools.permutations(list_))

def main():
    N = int(input())
    P = "".join([str(x) for x in input().split()])
    Q = "".join([str(x) for x in input().split()])
    
    ls = [ "".join(i) for i in permutation([str(i+1) for i in range(N)])]
    dic = defaultdict(int)
    for num,i in enumerate(ls):
        dic[i] = (num+1)
    print(abs(dic[P]-dic[Q]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = "".join(input().split())
Q = "".join(input().split())
S = map(str, range(1, N+1))
A = sorted(["".join(x) for x in itertools.permutations(S)])
print(abs(A.index(P) - A.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

for v in permutations(range(1,N+1)):
    count += 1
    if all([P[i]==v[i] for i in range(N)]):
        a = count
    if all([Q[i]==v[i] for i in range(N)]):
        b = count

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
N_data = [ i for i in range(1,1+ N) ]
P = tuple(map(int,input().split(" ")))
Q = tuple(map(int,input().split(" ")))

permutation_list = list(itertools.permutations(N_data,N))

def cal(data):
    for i in range(len(permutation_list)):
        if data  == permutation_list[i]:
            return i + 1

a = cal(P)
b = cal(Q)

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = [int(s) for s in input().split()]
Q = [int(s) for s in input().split()]


p_turn, q_turn = 0, 0
for i, V in enumerate(list(itertools.permutations(range(1, N+1)))):
    if p_turn == 0 and all([p == v for p, v in zip(P, V)]):
        p_turn = i + 1
    if q_turn == 0 and all([q == v for q, v in zip(Q, V)]):
        q_turn = i + 1

print(abs(p_turn-q_turn))

Submission status: Accepted

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

def factorial(n):
    if n == 1:
        return n
    else:
        return n*factorial(n-1)

def order(n, list):
    total = 0
    for i in range(1, n):
        for j in range(i, n):
            if list[i-1] <= list[j]:
                list[j] -= 1
            else:
                pass

        total += (list[i-1]-1)*factorial(n-i)
    return total

def main():
    a = order(N, P)
    b = order(N, Q)
    if 0 <= a-b:
        print(a-b)
    else:
        print(-(a-b))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

import itertools

N = int(input())
P_list = list(map(str, input().split()))
Q_list = list(map(str, input().split()))

P_tuple = tuple(P_list)
Q_tuple = tuple(Q_list)

P_all = list(itertools.permutations(sorted(P_list)))
Q_all = list(itertools.permutations(sorted(Q_list)))

P_num = P_all.index(P_tuple)
Q_num = P_all.index(Q_tuple)

print(abs(P_num - Q_num))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
ar = tuple(map(int,input().split(" ")))
br = tuple(map(int,input().split(" ")))
cr = list(itertools.permutations(ar))
cr.sort()
a = cr.index(ar)
b = cr.index(br)
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
n = list(range(1, int(input())+1))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
# p = ''.join(a)
# q = ''.join(b)
pp , qq = 0, 0
for i, j in enumerate(permutations(n)):
    if j == p:
        pp = i+1
    if j == q:
        qq = i+1
    if pp and qq:
        break
print(abs(pp-qq))

Submission status: Accepted

Solution code:
def calc(ans_list, num):
    ans_2 = []
    for a in ans_list:
        for i in num_list:
            if i not in a:
                t = a + i
                ans_2.append(t)
    if (num < n):
        return calc(ans_2, num + 1)
    return ans_2


n = int(input())
num_list = [str(i) for i in range(1, n + 1)]
p = int("".join(input().split(" ")))
q = int("".join(input().split(" ")))
ans_list = calc(num_list, 2)
ans_list = [int(i) for i in ans_list]
p_num = len([a for a in ans_list if a > p])
q_num = len([a for a in ans_list if a > q])
print(abs(p_num - q_num))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = [int(a) for a in input().split()]
Q = [int(a) for a in input().split()]


listN=list(itertools.permutations(range(1,N+1)))
countP = 0
for i in range(0,len(listN)):
    if list(listN[i])==P:
        break
    else:
        countP+=1



countQ = 0
for j in range(0,len(listN)):
    if list(listN[j])==Q:
        break

    else:
        countQ+=1

print(abs(countP-countQ))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=list(map(str, input().split()))
p_new=''.join(p)
q=list(map(str, input().split()))
q_new=''.join(q)
a=[]
pc, qc=0,0
 
for v in itertools.permutations(range(1,n+1)): #Nまでの順列の辞書順リスト生成
    a_new=map(str, v)
    a_i=''.join(a_new)
    a.append(a_i)

for i in range(len(a)): #pとqが辞書順で何番目かを特定する
    if p_new==a[i]:
        pc+=i
    if q_new==a[i]:
        qc+=i
    else:
        continue
print(abs(pc-qc))

Submission status: Accepted

Solution code:
import itertools

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

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

Pi, Qi = -1, -1
for i in range(len(LUT)):
    if LUT[i] == P:
        Pi = i
    if LUT[i] == Q:
        Qi = i

print(abs(Qi - Pi))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import itertools

N = int(input())
num1 = list(map(int,input().split()))
num2 = list(map(int,input().split()))
x, y = 0, 0
l = [_ + 1 for _ in range(N)]
A = []

for v in itertools.permutations(l, N):
    A.append(list(v))

x = A.index(num1)
y = A.index(num2)

print(abs(x - y))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
P = tuple(int(n) for n in input().split())
Q = tuple(int(n) for n in input().split())

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

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

Submission status: Accepted

Solution code:
import itertools

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

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

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

Submission status: Accepted

Solution code:
from itertools import permutations
#from bisect import *                                                                 
n = int(input())
N = [i for i in range(1,n+1)]
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]

l = [list(i) for i in permutations(N)]
#print(l)                                                                             
P_idx = l.index(P)
Q_idx = l.index(Q)

print(abs(P_idx-Q_idx))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

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

p = permutations.index(P)
q = permutations.index(Q)

print(abs(p - q))

Submission status: Accepted

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

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def sum(i,bit):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    def index_permutations(p):
        res = 0
        bit = [0]*(n+1)
        for i in range(len(p)):
            res += (p[i]-sum(p[i],bit))*f[n-i-1]
            j = p[i]
            while j <= n:
                bit[j] += 1
                j += j&-j
        return res
    n = I()
    p = LI()
    q = LI()
    f = [1]
    for i in range(1,n+1):
        f.append(f[-1]*i)
    print(abs(index_permutations(p)-index_permutations(q)))
    return

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

Submission status: Accepted

Solution code:
#abc150c
from itertools import *

n=int(raw_input())
p=tuple(map(int,raw_input().split()))
q=tuple(map(int,raw_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())
ans_a = 0
ans_b = 0
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
k = list(itertools.permutations(range(1,N+1)))
P = tuple(P)
Q = tuple(Q)
for i in range(0,len(k)):
    if P == k[i]:
        ans_a = i
        break
for j in range(0,len(k)):
    if Q == k[j]:
        ans_b = j
        break
print(abs(ans_a-ans_b))

Submission status: Accepted

Solution code:
#abc150c
from itertools import *

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 as it

N = int(input())
A = [int(i) for i in range(1,N+1)]
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

V = it.permutations(A)
i = 0
for v in V:
  if v == P:
    a = i
  if v == Q:
    b = i
  i += 1
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()))
J = set(itertools.permutations(p))
J = sorted(list(J))
for i in range(len(J)):
    if list(J[i]) == p:
        a = i + 1
    if list(J[i]) == q:
        b = i + 1
print(abs(a-b))

Submission status: Accepted

Solution code:
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
from itertools import permutations
ans = []
for i,r in enumerate(permutations(range(1,n+1))):
    if all(p[j]==r[j] for j in range(n)):
        ans.append(i)
    if all(q[j]==r[j] for j in range(n)):
        ans.append(i)
ans = abs(ans[0]-ans[1])
print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
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

n = int(input())
s = tuple(map(int, input().split()))
e = tuple(map(int, input().split()))

per = list(permutations(range(1,n+1)))
print(abs(per.index(e) - per.index(s)))

Submission status: Accepted

Solution code:
import itertools

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

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

P_in = a.index(P)
Q_in = a.index(Q)

print(abs(P_in - Q_in))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
lis = range(1, n+1)

#permulations(l,n)でリストlの要素のうちn個を選んで並べる
a = list(itertools.permutations(lis))

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

Submission status: Accepted

Solution code:
def gattai(a):
    tmp = ''
    for i in a:
        tmp += str(i)
    return tmp
import itertools
N = int(input())
a = [0]*N
p = gattai(list(map(int, input().split())))
q = gattai(list(map(int, input().split())))
for i in range(1,N+1):
    a[i-1] = i
ans1 = 0
ans2 = 0
count = 0
for i in itertools.permutations(a):
    count += 1
    if gattai(i) == p:
        ans1 = count
    if gattai(i) == q:
        ans2 = count
print(abs(ans1-ans2))

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)))
print(abs(a.index(p)-a.index(q)))