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 = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

L = [i for i in range(1, N + 1)]
P = tuple(P)
Q = tuple(Q)

i = 1
for e in itertools.permutations(L):
#     print(e)
    if e == P:
        a = i
    if e == Q:
        b = i
    i += 1
print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
Ps = "".join(map(str, P))
Qs = "".join(map(str, Q))

Dict = []
for w in permutations(range(1, N+1)):
    Dict.append("".join(map(str, w)))

for i, w in enumerate(Dict):
    if w == Ps: a = i
    if w == Qs: b = i
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, pat in enumerate(permutations(range(1, n + 1), n)):
    if pat == p:
        a = i + 1
    if pat == q:
        b = i + 1

print(abs(a - b))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

import itertools

def ri():
    return int(input())

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

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

def main():
    n = ri()
    lp = rli()
    lq = rli()
    seq = list(range(1, n+1))
    ll = list(itertools.permutations(seq))
    a = None
    for i, l in enumerate(ll):
        if list(l) == lp:
            a = i
        if list(l) == lq:
            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()))
X = list(range(1, n+1))

pattern = list(itertools.permutations(X))
pattern.sort()

a = pattern.index(P)
b = pattern.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()))

a=[i for i in range(1,n+1)]
#aa=permutations(a)
l=[]
l=[aa for aa in permutations(a)]
l=sorted(l)
for i in range(len(l)):
 if l[i]==p:
  pp=i
 if l[i]==q:
  qq=i
print(abs(pp-qq))

Submission status: Accepted

Solution code:
import itertools

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

print(abs(sorted(list(itertools.permutations(p))).index(p) -
          sorted(list(itertools.permutations(q))).index(q)))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
prr = list(map(int, input().split()))
qrr = list(map(int, input().split()))

arr = list(permutations(prr))
arr.sort()
for i in range(len(arr)):
    arr[i] = list(arr[i])

ans = arr.index(prr) - arr.index(qrr)
print(abs(ans))

Submission status: Accepted

Solution code:
from itertools import permutations
def main():
    N = int( input())
    P = list( map( int, input().split()))
    Q = list( map( int, input().split()))
    Pem = list( map( lambda x: list(x), permutations( range(1, N+1))))

    M = 1
    for i in range(1, N+1):
        M *= i
    for i in range(M):
        if Pem[i] == P:
            p = i
        if Pem[i] == Q:
            q = i
    print( abs(p-q))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial
#import sys
#from fractions import gcd
setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
t=list(permutations(range(1,n+1),n))
#print(list(permutations(range(1,4),3)))
a,b=0,0
for i in range(len(t)):
    if list(t[i])==p:
        a=i+1
    if list(t[i])==q:
        b=i+1
print(abs(a-b))

Submission status: Accepted

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

import itertools

l = [i+1 for i in range(n)]

lst = list(itertools.permutations(l,n))

a = 0
b = 0

for i in range(len(lst)):
    if lst[i] == p:
        a = i
    if lst[i] == q:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
import itertools

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def TI(): return tuple(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n=II()
    p=TI()
    q=TI()
    for i,s in enumerate(itertools.permutations(range(1,n+1))):
        if s==p:a=i
        if s==q:b=i
    print(abs(b-a))
main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
data=[]
for ii in itertools.permutations(range(1,n+1),n):
    data.append(ii)
data.sort()
for i,di in enumerate(data):
    if di==P:
        p=i
    if  di ==Q:
        q=i
print(abs(q-p))

Submission status: Accepted

Solution code:
import itertools

l = []
N = int(input())
*P, = map(int, input().split())
*Q, = map(int, input().split())
P = tuple(P)
Q = tuple(Q)
for i in range(N):
    l.append(i+1)
L = list(itertools.permutations(l))

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

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
l = list(itertools.permutations([i for i in range(1, n+1)], n))
for i in range(len(l)):
    l[i] = list(l[i])

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

a = l.index(p)
b = l.index(q)
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = list(map(int,input().split()))
#s = [int(input()) for i in range(3)]
Q = list(map(int,input().split()))
per = [i for i in range(1,N+1)]
per = permutations(per)
count = 0
for i in per:
    count += 1
    if list(i) == P:
        p = count
    if list(i) == Q:
        q = count
print(abs(p-q))

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = tuple(input().split())
Q = tuple(input().split())
p = list(itertools.permutations(P))
q = list(itertools.permutations(Q))
p.sort()
q.sort()
for i in range(len(p)):
    if p[i]==P:
        break
for j in range(len(q)):
    if q[j]==Q:
        break
print(abs(i-j))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p = list(map(lambda x: int(x) - 1, input().split()))
q = list(map(lambda x: int(x) - 1, input().split()))


def compare(a, b):
    for i in range(n):
        if a[i] != b[i]:
            return False
    return True


idx_p = -1
idx_q = -1
for i, x in enumerate(permutations(range(n))):
    if compare(p, x):
        idx_p = i
    if compare(q, x):
        idx_q = i

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

Submission status: Accepted

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

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

B.sort()

# print(B)

p=-1
q=-1
for i in range(len(B)):
    # print(P,[j for j in B[i]])
    if P == [j for j in B[i]]:
        
        p=i
    if Q== [j for j in B[i]]:
        q=i
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 for i in range(1,n+1)]
x=list(list(itertools.permutations(l,n)))
#print(x)
a=0
b=0
for i in range(len(x)):
    if p==list(x[i]):
        a=i+1
    if q==list(x[i]):
        b=i+1
print(abs(a-b))

Submission status: Accepted

Solution code:
N = int(input())
import itertools
all = list(itertools.permutations([i+1 for i in range(N)],N))
lst1 = tuple(map(int, input().split()))
lst2 = tuple(map(int, input().split()))
id1 = 0
id2 = 0
for i in range(len(all)):
    if all[i] == lst1:
        id1 = i
    if all[i] == lst2:
        id2 = i
print(abs(id1-id2))

Submission status: Accepted

Solution code:
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
from itertools import permutations
p,q=0,0
for i,R in enumerate(permutations(range(1,N+1))):
    if P==R:
        p=i
    if Q==R:
        q=i
print(abs(p-q))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    P=tuple(map(int,input().split()))
    Q=tuple(map(int,input().split()))
    a=-1
    for i,x in enumerate(permutations(range(1,N+1))):
        if x==P or x==Q:
            if a==-1:
                a=i
            else:
                print(abs(i-a))
                exit()
    print(0)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import permutations as permi
input = sys.stdin.readline
N = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
t = []
a = 0
b = 0
for pm in permi([x for x in range(1, N + 1)], N): t.append(pm)
for i in range(len(t)):
  if p == t[i]: a = i
  if q == t[i]: b = i 
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools


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


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

ans = abs(b - a)
print(ans)

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
z = [i for i in range(1, n+1)]
x = list(itertools.permutations(z))
print(abs(x.index(p)-x.index(q)))

Submission status: Accepted

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

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

main()

Submission status: Accepted

Solution code:
import itertools

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

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

  if P == p:
    p_pos = i

  if Q == p:
    q_pos = i
  i += 1
print(abs(p_pos - q_pos))

Submission status: Accepted

Solution code:
import itertools
def main():

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

    a = list(itertools.permutations([i for i in range(1, N+1)]))
    k, l = 0, 0
    for i in range(len(a)):
        if list(a[i]) == P: k = i
        if list(a[i]) == Q: l = i
    return abs(k-l)

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

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

for i,t in enumerate(itertools.permutations(tuple(list(range(1,N+1))), N)):
    if t == P:
        i_p = i
    if t == Q:
        i_q = i

print(abs(i_p-i_q))

Submission status: Accepted

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

nums = [i+1 for i in range(N)]
import itertools
count = 0
for v in itertools.permutations(nums,N):
  count += 1
  if v == P:
    a = count
  if v == Q:
    b = count
    
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()))
M = [ i for i in range(1,N+1)]
pp = list(itertools.permutations(M,N))
print(abs(pp.index(P)-pp.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 = sorted(list(itertools.permutations(list(range(1, N+1)))))
print(abs(l.index(P) - l.index(Q)))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    P = tuple([int(i) for i in input().split()])
    Q = tuple([int(i) for i in input().split()])
    a, b = -1, -1
    from itertools import permutations
    for i, per in enumerate(permutations(range(1, N+1), N)):
        if per == P:
            a = i+1
        if per == Q:
            b = i+1
    print(abs(a-b))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n = inp()
    p = tuple(inpl())
    q = tuple(inpl())
    pre = []
    for i in permutations(range(1,n+1)):
        pre.append(i)
    pre.sort()
    x,y = 0,0
    for i in range(len(pre)):
        if pre[i] == p:
            x = i
        if pre[i] == q:
            y = i
    print(abs(x-y))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''input
3
1 3 2
3 1 2
'''
# A coding delight
from sys import stdin, stdout
import gc
from copy import deepcopy
from itertools import permutations
gc.disable()
input = stdin.readline

	
# main starts
n = int(input().strip())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

a = list(permutations(P))
a.sort()
for i in range(len(a)):
	if tuple(P) == a[i]:
		first = i 
	if tuple(Q) == a[i]:
		second = i 
# print(first, second)
print(abs(first - second))

Submission status: Accepted

Solution code:
from itertools import permutations
from operator import eq
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
for i, c in enumerate(permutations(range(1, N+1), N)):
    if all(map(eq, c, P)):
        a = i
    if all(map(eq, c, Q)):
        b = i
print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
n = int(input())
ls = ["".join([str(j) for j in list(i)]) for i in permutations(range(1,n+1))]
p_ls = "".join([i for i in sys.stdin.readline().split()])
q_ls = "".join([i for i in sys.stdin.readline().split()])
print(abs(ls.index(p_ls) - ls.index(q_ls)))

Submission status: Accepted

Solution code:
N = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
from itertools import permutations
s = list(permutations([i for i in range(1,N+1)]))
for i,e in enumerate(s):
    if e == P:
        p_ans = i
    if e == Q:
        q_ans = i
print(abs(p_ans-q_ans))

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import permutations as perm
n = int(readline())
p = tuple(map(int, readline().split()))
q = tuple(map(int, readline().split()))
lst = list(perm(list(range(1, n+1)),n))
lst.sort()
pn=qn=0
for i,j in enumerate(lst, 1):
  if p == j:
    pn = i
  if q == j:
    qn = i
  if pn*qn!=0:
    break
print(abs(pn-qn))

Submission status: Accepted

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

import itertools

for i,R in enumerate(itertools.permutations(list(range(1,N+1)))):
    if P==R: a = i
    if Q==R: 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()))

l = [i+1 for i in range(N)]
i = 0
p = 0
q = 0

for v in itertools.permutations(l, N):
    if(P == list(v)):
        p = i
    if(Q == list(v)):
        q = i
    i += 1

print(abs(p - q))

Submission status: Accepted

Solution code:
n=int(input())
p=[int(j) for j in input().split()]
q=[int(j) for j in input().split()]
l=list(range(1,n+1)[::-1])
import itertools
ll=[]
for x in itertools.permutations(l):
    ll.append(list(x))
ll.sort()
a=0
b=0
c=0
for i in ll:
    if i==p:
        a=c
    if i==q:
        b=c
    c+=1
print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
a = tuple(map(int,input().split()))
b = tuple(map(int,input().split()))
cnta = 0
cntb = 0
for x in permutations(range(1,n+1)):
  if a == x:
    aa = cnta
  if b == x:
    bb = cntb
  cnta += 1
  cntb += 1
print(abs(aa-bb))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
p = [int(x) for x in input().split(' ')]
q = [int(x) for x in input().split(' ')]

p, q = tuple(p), tuple(q)

d = list(range(1, n+1))

a = 0
b = 0

for i, x in enumerate(permutations(d)):
    if x == p:
        a = i
    if x == q:
        b = i

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
x=list(itertools.permutations([i+1 for i in range(n)]))
p=[int(i) for i in input().split()]
q=[int(i) for i in input().split()]
l=len(x)
a,b=-1,-1
for i in range(l):
    #print(x[i])
    if list(x[i])==p:
        a=i
    if list(x[i])==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()))

junretsu = []
for i in itertools.permutations(range(1, n + 1)):
    junretsu.append(i)
junretsu = sorted(junretsu)
a = junretsu.index(p)
b = junretsu.index(q)

print(abs(a - b))

Submission status: Accepted

Solution code:
import sys
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = tuple(map(int, readline().split()))
    b = tuple(map(int, readline().split()))
    nl = sorted(a)
    an = -1
    bn = -1
    for i, v in enumerate(itertools.permutations(nl, n)):
        if v == a:
            an = i
        if v == b:
            bn = i
    print(abs(an - bn))


if __name__ == '__main__':
    solve()

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 = sorted([p for p in permutations(range(1, N + 1))])
print(abs(perms.index(P) - perms.index(Q)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
p = LI()
q = LI()

cnt = 0
for i in permutations(range(1,n+1)):
    if list(i) == p:
        x = cnt
    if list(i) == q:
        y = cnt
    cnt += 1
print(abs(x-y))

Submission status: Accepted

Solution code:
from itertools import permutations

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

d = dict()
for i, perm in enumerate(permutations(range(1, n + 1)), 1):
    d[perm] = i

print(abs(d[p] - d[q]))

Submission status: Accepted

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

from itertools import permutations

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

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

Submission status: Accepted

Solution code:
from itertools import*
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
for i,x in enumerate(permutations(range(1,n+1),n)):
	if x==p:
		a=i
	if x==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_list = [i for i in range(1, N+1)]

for idx, i in enumerate(itertools.permutations(N_list)):
    # print(i)
    if list(i) == P:
        p = idx
    if list(i) == Q:
        q = idx

print(abs(p-q))

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
P = LIST()
Q = LIST()

for i, p in enumerate(permutations(range(1, N+1))):
	if list(p) == P:
		a = i
	if list(p) == Q:
		b = i
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()))

def count(x):
    i = 0
    for perm in permutations(range(1, n+1)):
        if perm == x:
            return i
        i += 1

print(abs(count(p) - count(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()))
l = list(range(1, N+1))
cnt = 0

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, P, Q):
    for i, R in enumerate(permutations(range(1, N+1), r=N)):
        R = list(R)
        for a, r in zip(P, R):
            if a != r:
                break
        else:
            p = i
        for a, r in zip(Q, R):
            if a != r:
                break
        else:
            q = i

    return abs(p-q)


def main():
    N = read_int()
    P = read_int_n()
    Q = read_int_n()
    print(slv(N, P, Q))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import permutations as perm

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
l = list(range(1, n + 1))

def f(p, q):
    for i, j in zip(p, q):
        if i != j:
            return False
    return True

aa,bb = -1,-1
for i, p in enumerate(perm(l)):
    if f(p, a):
        aa = i
    if f(p, b):
        bb = i
print(abs(aa - bb))
# print(aa,bb)

Submission status: Accepted

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

for i,c in enumerate(C):
    if list(c)==list(P):
        f=i
    if list(c)==Q:
        s=i
print(abs(s-f))

Submission status: Accepted

Solution code:
def c_count_order():
    from itertools import permutations
    N = int(input())
    P = tuple(int(i) for i in input().split())
    Q = tuple(int(i) for i in input().split())

    rank = {perm: k for k, perm in enumerate(permutations(range(1, N + 1), N), 1)}
    return abs(rank[P] - rank[Q])

print(c_count_order())

Submission status: Accepted

Solution code:
n=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
from itertools import permutations
jun = list(permutations(range(1,n+1),n))
ansp=-1
ansq=-1
for a,b in enumerate(jun):
    if b==P:
        ansp=a
    if b==Q:
        ansq=a
print(abs(ansp-ansq))

Submission status: Accepted

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

import itertools
z = [list(per) for per in itertools.permutations(range(1, n + 1), r=n)]
print(abs(z.index(p) - z.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()))

i = 0
for x in permutations(range(1, n+1)):
	i += 1
	if list(x) == p:
		a = i
	if list(x) == q:
		b = i
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:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
p = inpl()
q = inpl()
p = tuple(p)
q = tuple(q)

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

Submission status: Accepted

Solution code:
def main():
    import itertools
    n = int(input())
    p = tuple(map(int,input().split()))
    q = tuple(map(int,input().split()))
    seq = list(range(1,n+1))
    l = sorted(list(itertools.permutations(seq)))
    d = {}
    for j in range(len(l)):
        d[tuple(l[j])] = j+1
    print(abs(d[p]-d[q]))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

from itertools import permutations
l = []
for pp in permutations(range(1, n+1)):
    ll = []
    for _p in pp:
        ll.append(_p)
    l.append(str(ll))

a = l.index(str(p))
b = l.index(str(q))
print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
N=int(input())
P=tuple([int(i)-1 for i in input().split()])
Q=tuple([int(i)-1 for i in input().split()])
SEQ=sorted(list(itertools.permutations(range(N),N)))
#print(SEQ)

a=-1
b=-1
for i in range(len(SEQ)):
    if SEQ[i]==P:
        a=i
    if SEQ[i]==Q:
        b=i
print(abs(a-b))

Submission status: Accepted

Solution code:
N = int(input())
P1 = tuple(map(int, input().split()))
P2 = tuple(map(int, input().split()))

import itertools
a = 0
b = 0
for perm in itertools.permutations([i+1 for i in range(N)]):
    if perm <= P1:
        a += 1
    if perm <= P2:
        b += 1
print(abs(b - a))

Submission status: Accepted

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

def kai(x):
    a = 1
    for i in range(1, x + 1):
        a *= i
    return a

al= list(range(1,n+1))
j = n-1
a = 0
for i in p:
    a += al.index(i) * kai(j)
    j -= 1
    al.remove(i)

al = list(range(1, n+1))
j = n-1
b = 0
for i in q:
    b += al.index(i) * kai(j)
    j -= 1
    al.remove(i)

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations
n = input()
l1 = [int(i) for i in raw_input().split(" ")]
l2 = [int(i) for i in raw_input().split(" ")]
per = list(permutations(range(1,n+1)))
a = 0
b = 0
cnt = 0;
for i in per:
	# print i
	i = list(i)
	if(i == l1): a = cnt
	if(i == l2): b=cnt
	cnt+=1;
print abs(a-b)