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


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


N = int(input())
P = tuple(inpl())
Q = tuple(inpl())

A = list(perm([i for i in range(1, N + 1)]))
A.sort()

for i, a in enumerate(A):
    # print(a)
    if a == P:
        p = i
    if a == Q:
        q = i

print(abs(p - q))

Submission status: Accepted

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

P = int(''.join(P))
Q = int(''.join(Q))

import itertools
L = [int(''.join(_)) for _ in itertools.permutations(['1','2','3','4','5','6','7','8'][:N])]
L.sort()
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()))

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

Submission status: Accepted

Solution code:
import sys,math,string,bisect
from itertools import *
input=sys.stdin.readline
from collections import deque,defaultdict
L=lambda : list(map(int,input().split()))
Ls=lambda : list(input().split())
M=lambda : map(int,input().split())
I=lambda :int(input())
S=lambda :input().strip()
n=I()
i=input()
#print()
l=list(map(int,i.split()))
l=tuple(l)
i=input().strip()
m=list(map(int,i.split()))
m=tuple(m)
d=list(permutations(set(l)))
#print(d)
print(abs(d.index(l)-d.index(m)))

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()]
p = tuple(p)
q = tuple(q)

m = [x for x in range(1, n+1)]
all = list(itertools.permutations(m))
a = all.index(p)
b = all.index(q)
print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
ps = ''.join(input().split())
qs = ''.join(input().split())
perm = sorted(
    list(map(''.join, (permutations(map(str, list(range(1, n+1))))))))
pi, qi = 0, 0
#print(ps, qs)
# print(len(perm))
for i in range(len(perm)):
    # print(perm[i])
    if perm[i] == ps:
        # print('pi={}'.format(i))
        pi = i+1
    if perm[i] == qs:
        # print('qi={}'.format(i))
        qi = i+1

print(abs(pi-qi))

Submission status: Accepted

Solution code:
import itertools

def resolve():
    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    l = list(itertools.permutations(range(1, N+1)))
    p_idx = l.index(P)
    q_idx = l.index(Q)
    print(abs(p_idx - q_idx))

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

import itertools
arr = list(map(lambda x: ('').join(list(map(str,x))),list(itertools.permutations(range(1,N+1)))))

print(abs(arr.index(('').join(P))-arr.index(('').join(Q))))

Submission status: Accepted

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

N = int(input().split()[0])
p_list = list(input().split())
q_list = list(input().split())

pp = itertools.permutations(p_list, N)
pp = list(sorted(["".join(x) for x in pp]))

qp = itertools.permutations(q_list, N)
qp = list(sorted(["".join(x) for x in qp]))

p = "".join(p_list)
q = "".join(q_list)

p_index = qp.index(p)
q_index = qp.index(q)

ans = abs(p_index - q_index)
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

nP = 0
nQ = 0
for i, p in enumerate(itertools.permutations(list(range(1, N+1)))):
  if p == P:
    nP = i
  if p == Q:
    nQ = i
    
print(abs(nP - nQ))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    K, X = LI()
    if K*500>=X:
        ans = "Yes"
    else:
        ans = "No"
    print(ans)
    return

def examB():
    N = I()
    S = SI()
    ans = 0
    for i in range(N-2):
        if S[i:i+3]=="ABC":
            ans +=1
    print(ans)
    return

def examC():
    N = I()
    P = LI()
    Q = LI()
    Cp = [i for i in itertools.permutations(P,N)]; Cp.sort()
    Cq = [i for i in itertools.permutations(Q, N)]; Cq.sort()
#    print(Cp)
#    print(Cq)
    for k,i in enumerate(Cp):
        flag = True
        for j in range(N):
            if P[j]!=i[j]:
                flag = False
                break
        if flag:
            a = k
            break
    for k,i in enumerate(Cq):
        flag = True
        for j in range(N):
            if Q[j]!=i[j]:
                flag = False
                break
        if flag:
            b = k
            break
    ans = abs(a-b)
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

Submission status: Accepted

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

li = list(itertools.permutations(range(1, n+1), n))
a = li.index(p)
b = li.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()))

J = [i for i in range(1, N+1)]
JJ = list(itertools.permutations(J))
for i in range(len(JJ)):
    if P == JJ[i]:
        pp = i
    if Q == JJ[i]:
        qq = i
print(abs(pp - qq))

Submission status: Accepted

Solution code:
# どうせN=8なので探索
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

def f(n, cur=[]):
    if len(cur) == n:
        yield cur
    else:
        for i in range(1, n + 1):
            if i not in cur:
                _next = cur[:]
                _next.append(i)
                yield from f(n, _next)


idx = 1
pflag, qflag = False, False
for g in f(n):
    if p == g:
        pflag = idx
    if q == g:
        qflag = idx
    if pflag and qflag:
        break
    idx += 1
print(abs(pflag - qflag))

Submission status: Accepted

Solution code:
import itertools
import copy
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
l = copy.deepcopy(p)
l.sort()
cnt = 0
p_c = 0
q_c = 0
p = tuple(p)
q = tuple(q)
for i in itertools.permutations(l):
    if i==p:
        p_c = cnt
    if i==q:
        q_c = cnt
    cnt+=1
print(abs(p_c-q_c))

Submission status: Accepted

Solution code:
N=int(input())
Pn=tuple(map(int, input().split()))
Qn=tuple(map(int, input().split()))
a,b=0,0

from itertools import permutations
seq = [i for i in range(1, N+1)]
for i, l in enumerate(list(permutations(seq))):
    if l == Pn:
        a = i
    if l == Qn:
        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()))

X = list(itertools.permutations(P))
X.sort()
for i,x in enumerate(X):
	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 = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

nlist = list(range(1, n+1))
nmlist = []

for val in itertools.permutations(nlist, n):
    nmlist.append(val)

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

Submission status: Accepted

Solution code:
def pro(x):
    ret = 1
    for i in range(x):
        ret *= i+1
    return ret


def cal(n, da):
    ju = [False for i in range(n+1)]
    ret = 1
    for i in range(n):
        now, co = 1, 0
        while(now != da[i]):
            if not ju[now]:
                co += 1
            now += 1
        #print(pro(n-1-i),co)
        ret += pro(n-1-i) * co
        ju[da[i]] = True
    #print(ret)
    return ret

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

print(abs(cal(n,p)-cal(n,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()))
if p==q:
    print(0)
else:
    p, q = tuple(p), tuple(q)
    l = [i for i in range(1, n+1)]
    flag = False
    ss = 0
    for i, each in enumerate(permutations(l)):
        if each == p or each == q:
            if flag:
                print(abs(ss - i))
            else:
                flag = True
                ss = i

Submission status: Accepted

Solution code:
import itertools
n=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))
arr=[i+1 for i in range(n)]
arr2=list(itertools.permutations(arr))
for num,i in enumerate(arr2):
    if A==i:
        a=num+1
    if B==i:
        b=num+1
print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools

N = int(input())
P = tuple([int(i) for i in input().split()][0:N])
Q = tuple([int(i) for i in input().split()][0:N])
l = list(itertools.permutations(range(1,N+1)))
print(abs(l.index(Q) - l.index(P)))

Submission status: Accepted

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

from itertools import permutations

ans = 0
xs = permutations(range(1, n+1), n)
xs = sorted(xs)
# print(xs)
# print(p,q)
for i, x in enumerate(xs):
    if x == p:
        pi = i
    if x == q:
        qi = i

print(abs(pi - qi))

Submission status: Accepted

Solution code:
from itertools import permutations
import math

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

orders = dict()
i = 1
for perms in sorted(permutations(range(1, N+1))):
    orders[perms] = i
    i += 1

print(int(math.fabs(orders[P] - orders[Q])))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
p = list(map(int, input().split()))
pp = "".join((str(n) for n in p))
q = list(map(int, input().split()))
qq = "".join((str(n) for n in q))
a = [i+1 for i in range(n)]
b = 0
c = 0
d = 0
for i in itertools.permutations(a):
	d += 1
	x = "".join((str(n) for n in i))
	if pp == x:
		b = d
	if qq == x:
		c = d
print (abs(b-c))

Submission status: Accepted

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

A=1
for i in range(n):
    tmp=a[i]
    for j in range(i):
        if a[j]<a[i]: tmp-=1 
    A+=math.factorial(n-i-1)*(tmp-1)

B=1
for i in range(n):
    tmp=b[i]
    for j in range(i):
        if b[j]<b[i]: tmp-=1 
    B+=math.factorial(n-i-1)*(tmp-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()))

p=q=0
for i,a in enumerate(permutations(range(1,N+1))):
    if a==P:
        p=i
    if a==Q:
        q=i
print(abs(p-q))

Submission status: Accepted

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

import itertools

l = sorted(itertools.permutations(range(1, N + 1)))

for i, x in enumerate(l):
    x = list(x)

    if x == P:
        p = i

    if x == Q:
        q = i


print(abs(p - q))

Submission status: Accepted

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

from bisect import bisect
from math import factorial
def rank(l):
  r = 0
  nums = [_ for _ in range(1,n+1)]
  for i in range(n):
    r += bisect(nums, l[i]) * factorial(len(nums)-1)
    nums.remove(l[i])
  return r
print(abs(rank(p)-rank(q)))

Submission status: Accepted

Solution code:
import sys
import itertools
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
li=[]
for i in range(n):
    li.append(i+1)
a=list(itertools.permutations(li))
tmp=[0,0]

for i in range(len(a)):
    if list(a[i])==p:
        tmp[0]=i
    if list(a[i])==q:
        tmp[1]=i
print(abs(tmp[0]-tmp[1]))

Submission status: Accepted

Solution code:
import itertools

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

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

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

Submission status: Accepted

Solution code:
from itertools import permutations

N=int(input())
l=[i+1 for i in range(N)]
P=tuple(map(int, input().split()))
Q=tuple(map(int, input().split()))
counter=0
for t in permutations(l):
  if P==t:
    a=counter
    
  if Q==t:
    b=counter
    
  counter+=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()))
a=(i for i in range(1,N+1))
import itertools
A=list(itertools.permutations(a))
p,q=0,0
for i,x in enumerate(A):
    if P==x:
        p=i+1
    if Q==x:
        q=i+1
print(abs(p-q))

Submission status: Accepted

Solution code:
from itertools import permutations

N = int(input())
P = "".join(list(input().split()))
Q = "".join(list(input().split()))
#print("P:", P)

A = ""
for i in range(1, N + 1):
  A += str(i)
A = list(permutations(A, N))
A.sort()
#print("A:", A)

B = []
for i in range(len(A)):
  B.append("".join(A[i]))

for i in range(len(B)):
  if P == B[i]:
    a = i
  if Q == B[i]:
    b = i
    
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

idx = 1
for l in itertools.permutations(range(1, N+1)):
    if l == P:
        a = idx
    if l == Q:
        b = idx

    idx += 1

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
from itertools import permutations
input = sys.stdin.readline
n = int(input())
p = int(''.join([x for x in input().strip().split()]))
q = int(''.join([x for x in input().strip().split()]))
d = [str(i) for i in range(1, n+1)]
l = sorted([int(''.join(i)) for i in permutations(d)])
print(abs(l.index(p)-l.index(q)))

Submission status: Accepted

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

import itertools

per = list(itertools.permutations(range(1, N+1)))
for idx, ele in enumerate(per):
    if ele == P:
        s = idx
    if ele == Q:
        t = idx

print(abs(s-t))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = tuple(i for i in range(1, n+1))
x = tuple(map(int, input().split()))
y = tuple(map(int, input().split()))

for i, z in enumerate(tuple(itertools.permutations(a))):
    if z == x:
        x_count = i
    if z == y:
        y_count = i
print(abs(x_count - y_count))

Submission status: Accepted

Solution code:
import unittest
import itertools


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([1, 3, 2], [3, 1, 2]), 3)

    def test_2(self):
        self.assertEqual(think([7, 3, 5, 4, 2, 1, 6, 8], [3, 8, 2, 5, 4, 6, 7, 1]), 17517)

    def test_3(self):
        self.assertEqual(think([1, 2, 3], [1, 2, 3]), 0)


def solve():
    p, q = read()
    result = think(p, q)
    write(result)


def read():
    n = read_int(1)[0]
    p = read_int(n)
    q = read_int(n)
    return p, q


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(p, q):
    base = list(p)
    base.sort()
    index_of_p = 0
    index_of_q = 0
    for i, perm in enumerate(itertools.permutations(base)):
        if perm == tuple(p):
            index_of_p = i
        if perm == tuple(q):
            index_of_q = i
    return abs(index_of_p - index_of_q)


def write(result):
    print(result)


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

Submission status: Accepted

Solution code:
import math
import queue
import bisect
import heapq
import time
import itertools

mod = int(1e9+7)

def swap(a,b):
    return (b,a)

def gcd(a,b): #最大公約数
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def lcm(a,b): #最小公倍数
    return a/gcd(a,b)*b

def divisors(a): # 約数列挙
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def is_prime(a): #素数判定
    if a<2:
        return False
    elif a==2:
        return True
    elif a%2==0:
        return False
    sqrt_num = int(a**0.5)
    for i in range(3,sqrt_num+1,2):
        if a%i==0:
            return False
    return True

def prime_num(a): #素数列挙
    pn = [2]
    for i in range(3,int(a**0.5),2):
        prime = True
        for j in pn:
            if i%j==0:
                prime = False
                break
        if prime:
            pn.append(i)
    return pn

def prime_fact(a): #素因数分解
    sqrt = math.sqrt(a)
    res = []
    i = 2
    if is_prime(a):
        res.append(a)
    else:
        while a!=1:
            while a%i==0:
                res.append(i)
                a //= i
            i += 1
    return res

def main():
    n = int(input())
    p = [x for x in map(int,input().split())]
    q = [x for x in map(int,input().split())]
    if p == q:
        print(0)
        return
    nums = [i+1 for i in range(n)]
    cnt = 1
    l = 0
    h = 0
    for x in itertools.permutations(nums):
        y = [z for z in x]
        if y == p:
            l = cnt
        elif y == q:
            h = cnt
        cnt += 1
    print(abs(l-h))
    return

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
l = list(itertools.permutations(range(1, n+1), n))

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

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

Submission status: Accepted

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

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

a = 0
b = 0

for i, p in enumerate(perm) :
  if p == P :
    a = i + 1
  if p == Q :
    b = i + 1
    
print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations

n = int(input())
ppp = tuple(map(int, input().split()))
qqq = tuple(map(int, input().split()))
a, b = -1, -1
for i, rrr in enumerate(permutations(range(1, n + 1))):
    if a == -1 and ppp == rrr:
        a = i
    if b == -1 and qqq == rrr:
        b = i
    if a != -1 and b != -1:
        break
print(abs(a - b))

Submission status: Accepted

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

list = list(itertools.permutations([i+1 for i in range(n)]))
a = 0
b = 0
for i in range(len(list)) :
    if list[i] == p:
        a = i + 1
    if list[i] == q:
        b = i + 1


print(abs(a-b))

Submission status: Accepted

Solution code:
import math

n = int(input())
ps = list(map(int, input().split(' ')))
qs = list(map(int, input().split(' ')))

def func(array):
    sort_array = sorted(array)
    head = array[0]
    index = sort_array.index(head)
    num_pattern = math.factorial(len(array) - 1)
    return index * num_pattern

p_cnt = 1
for i in range(len(ps)):
    array = ps[i:]
    p_cnt += func(array)

q_cnt = 1
for i in range(len(qs)):
    array = qs[i:]
    q_cnt += func(array)
print(abs(p_cnt - q_cnt))

Submission status: Accepted

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

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

import itertools

b = list(itertools.permutations(a))
for num, x in enumerate(b):
    if x == p:
        res1 = num
    if x == q:
        res2 = num
print(abs(res1 - res2))

Submission status: Accepted

Solution code:
from itertools import permutations
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
moto = list(range(1, n+1))
dic = list(permutations(moto))

a = dic.index(p)
b = dic.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()))

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

Submission status: Accepted

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

l = [i+1 for i in range(N)]
p_list = list(itertools.permutations(l, N))
p_list.sort()
a = p_list.index(P)
b = p_list.index(Q)
 
print(abs(a-b))
#print(p_list)

Submission status: Accepted

Solution code:
import sys
from pprint import pprint
import itertools

def solve(n, p, q):
    patterns = list(itertools.permutations(range(1, n+1)))
    # pprint(patterns)
    print(abs(patterns.index(p) - patterns.index(q)))


if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    p = tuple(map(int, sys.stdin.readline().strip().split(" ")))
    q = tuple(map(int, sys.stdin.readline().strip().split(" ")))
    solve(n, p, q)

Submission status: Accepted

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

fact = [1, 1, 2, 6, 24, 120, 720, 5040, 40320]
def rank(an):
  used = []
  n = len(an)
  rank = 1
  for i, a in enumerate(an):
    b = a - len([c for c in used if c < a]) - 1
    rank += fact[n-i-1] * b
    used.append(a)
  return rank

print(abs(rank(xn) - rank(yn)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a_ = list(map(int, input().split()))
q_ = list(map(int, input().split()))
a = ""
q = ""
for i in range(n):
    a += str(a_[i])
    q += str(q_[i])

l = list(map(str, range(1, n+1)))
p = itertools.permutations(l, n)

d_ = []
for v in p:
    str_ = ""
    for c in v:
        str_ += str(c)
    d_.append(str_)
d_.sort()

dict_ = {}
for i, v in enumerate(d_):
    dict_[v] = i + 1

print(abs(dict_[a] - dict_[q]))

Submission status: Accepted

Solution code:
# 2020/01/10

from itertools import permutations

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

P=sorted(list(permutations(range(1,n+1))))


ans=0
for idx,e in enumerate(P,1):
    if e==p:
        ans+=idx
    if e==q:
        ans-=idx
    
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()))
A=list(itertools.permutations(range(1,N+1)))
L=len(A)
X=[]
for l in range(L):
    if A[l]==P or A[l]==Q:
        X.append(l)
print(max(X)-min(X))

Submission status: Accepted

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

lst = [i for i in range(1,n+1)]
#list(itertools.permutations([1, 2, 3]))
s = list(itertools.permutations(lst))
a,b = 0,0
for i in range(len(s)):
    if s[i] == P:
        a = i
    if s[i] == Q:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
import itertools


# noinspection PyShadowingBuiltinsInspection

def main():
    N = int(input())
    P = [int(x) for x in input().split()]
    Q = [int(x) for x in input().split()]
    p = 0
    q = 0
    i = 0
    for x in itertools.permutations(list(range(1,N+1))):
        i += 1
        x = list(x)
        if P == x:
            p = i
        if Q == x:
            q = i
    print(abs(p - q))
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

a = 0
als = list(range(1,n+1))
for i in range(n):
    for j in range(len(als)):
        if als[j] == p[i]:
            als.pop(j)
            break
    a += (j)*factorial(n-1-i)

b = 0
bls = list(range(1,n+1))
for i in range(n):
    for j in range(len(bls)):
        if bls[j] == q[i]:
            bls.pop(j)
            break
    b += (j)*factorial(n-1-i)

print(abs(a-b))

Submission status: Accepted

Solution code:
from itertools import permutations

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

a = 0
b = 0
for i, r in enumerate(permutations(range(n), n)):
    flagp = 1
    flagq = 1
    for j in range(n):
        if p[j]-1 != r[j]:
            flagp = 0
        if q[j]-1 != r[j]:
            flagq = 0
    if flagp:
        a = i
    if flagq:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
L = list(range(1, n + 1))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
junretu = list(itertools.permutations(L))
junretu.sort()
a, b = 0, 0
for i, jun in enumerate(junretu):
    if P == jun:
        a = i
    if Q == jun:
        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()))

p = 0
q = 0
L = [i for i in range(1, N+1)]
for i, v in enumerate(permutations(L), 1):
  if v == P:
    p = i
  if v == Q:
    q = i
    
print(abs(p - 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()))

vec = [x for x in range(1,n+1)]
pmt = permutations(vec, n)
a = 0
b = 0
for i, v in enumerate(pmt):
    if list(v) == P:
        a = i + 1
    if list(v) == Q:
        b = i + 1

    if a > 0 and b > 0:
        break

print(abs(a - b))

Submission status: Accepted

Solution code:
import itertools
tmp=[]
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
for i in range(1,N+1):
  tmp.append(i)
tmp=list(itertools.permutations(tmp))
A=0
B=0
for i,seq in enumerate(tmp):
  if list(seq)==P:
    A=i
  if list(seq)==Q:
    B=i
print(abs(A-B))

Submission status: Accepted

Solution code:
import itertools

n = int(input())
p_list = tuple(map(int,input().split()))
q_list = tuple(map(int,input().split()))

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

print(abs(sum_list.index(p_list)-sum_list.index(q_list)))

Submission status: Accepted

Solution code:
import itertools
n = int(input())
l = [i + 1 for i in range(n)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a = 0
b = 0
cnt = 1
for v in itertools.permutations(l, n):
    for i in range(n):
        if v[i] != p[i]:
            break
        if i == n - 1:
            a = cnt
    for i in range(n):
        if v[i] != q[i]:
            break
        if i == n - 1:
            b = cnt
    cnt += 1
print(abs(a - b))

Submission status: Accepted

Solution code:
import sys
import itertools

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

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

for i,x in enumerate(L):
    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 = tuple(int(s) for s in input().split())
q = tuple(int(s) for s in input().split())
seq = []

for i in range(n):
    seq.append(i+1)

b = list(itertools.permutations(seq))

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

Submission status: Accepted

Solution code:
import itertools
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
lst = [i for i in range(1 , N + 1)]
comp = []
for i in itertools.permutations(lst):
    comp.append(list(i))
comp.sort()
print(abs(comp.index(P) - comp.index(Q)))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, b = 1, 1
frac = 1
for i in range(N - 1):
  frac *= (i + 1)
for i in range(N):
  a += (A[i] - 1) * frac
  b += (B[i] - 1) * frac
  if i != N - 1:
    for j in range(i + 1, N):
      if A[j] > A[i]:
        A[j] -= 1
      if B[j] > B[i]:
        B[j] -= 1
    frac //= (N - 1 - i)
print(abs(a - b))

Submission status: Accepted

Solution code:
from itertools import permutations
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
flagP = False
flagQ = False
count = 0
for x in permutations(range(1, N+1)):
    count += 1
    if flagP == False:
        for i in range(N):
            if x[i] != P[i]:
                break
            elif i == N-1:
                flagP = True
                a = count
    if flagQ == False:
        for i in range(N):
            if x[i] != Q[i]:
                break
            elif i == N-1:
                flagQ = True
                b = count

    if flagQ and flagP:
        break


print(abs(a-b))

Submission status: Accepted

Solution code:
import itertools
n=int(input())
p=tuple(list(map(int,input().split())))
q=tuple(list(map(int,input().split())))
lst=list(itertools.permutations(range(1,n+1)))
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 itertools

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

a = 0
b = 0
for i, j in enumerate(list(itertools.permutations(range(1, n+1)))):
    j = list(j)
    if j == p:
        a = i
    if j == q:
        b = i

print(abs(a-b))

Submission status: Accepted

Solution code:
import sys
import itertools

N = int(input())
l = [x+1 for x in range(N)]
P = tuple(map(int, input().split(' ')))
Q = tuple(map(int, input().split(' ')))
if P == Q:
    print(0)
    sys.exit()

for i, v in enumerate(itertools.permutations(l, N)):
    if v == P:
        a = i+1
    elif v == Q:
        b = i+1
print(abs(a-b))