aacord’s memo

abcを中心にpythonで解いた問題のメモ、整理をしています。緑になった。

abc150 c 'Count order' (python)

N = 8 なら順列を小さい順にすべて列挙して p, q と一致するものがあるか調べても余裕で間に合う
(8! = 40320)
list の == は中身の順番まで完全に一致していると True を返す

おまけ
set の == は順番は関係なく中身の種類さえ合っていれば True を返す
[1, 3] in [1, 2, 3, 4] を True で返したいときは set(1, 3) <= set(1, 2, 3, 4) としてやる

import itertools
n = int(input())
p = [int(i)-1 for i in input().split()]
q = [int(i)-1 for i in input().split()]
m = sorted(p)
a = 0
b = 0
cnt = 1
#print(p,q)
for i in itertools.permutations(m, n):
  #print(list(i))
  if list(i) == p:
    a += cnt
  if list(i) == q:
    b += cnt
  if a!=0 and b!=0:
    break
  else:
    cnt += 1
    
print(abs(a-b))