結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー 👑 Kazun
提出日時 2020-07-17 21:56:16
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,204 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 153,856 KB
最終ジャッジ日時 2024-11-29 23:36:59
合計ジャッジ時間 52,506 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 20 TLE * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

class Permutation_Error(Exception):
    pass

class Permutation():
    def __init__(self,n,p=[]):
        if p==[]:
            self.p=[i for i in range(n)]
        else:
            self.p=p
            
        self.n=n

    def __str__(self):
        return "["+", ".join(map(str,self.p))+"]"

    def __mul__(self,other):
        T=[]
        n=max(self.n,other.n)

        for i in range(n):
            T.append(self.Replace(other.Replace(i)))

        return Permutation(n,T)

    def __truediv__(self,other):
        pass

    def __sgn__(self):
        if self.Minimum_Transposition()%2:
            return -1
        else:
            return 1
        
    def Inverse(self):
        Q=list(range(self.n))
        for k in range(self.n):
            Q[self.p[k]]=k
        return Permutation(self.n,Q)

    def Transposition(self,u,v):
        a=self.p.index(u)
        b=self.p.index(v)
        
        self.p[a]=v
        self.p[b]=u

    def Minimum_Transposition(self):
        X=self.Cycle_Division()

        T=0
        for d in X:
            T+=len(d)-1
        return T
    
    def Cycle_Multiplication(self,*C):
        X=[self.p.index(k) for k in C]

        N=len(C)
        for i in range(N):
            self.p[X[i]]=C[(i+1)%N]

    def Cycle_Division(self):
        T=[False]*self.n
        k=0
        v=0

        A=[]
        for k in range(self.n):
            if (not T[k]) and self.p[k]!=k:
                v=k
                B=[k]
                while self.p[v]!=k:
                    v=self.p[v]
                    T[v]=True
                    B.append(v)
                A.append(B)
        return A
            
    def Replace(self,x):
        if x<self.n:
            return self.p[x]
        else:
            return x

    def Order(self):
        L=self.Cycle_Division()
        C=[]
        for K in L:
            C.append(len(K))
        return LCM(*C)
#----------------------------------------------
N=int(input())
A=Permutation(N,list(map(lambda x:int(x)-1,input().split())))
B=Permutation(N,list(map(lambda x:int(x)-1,input().split())))
C=(B.Inverse()*A).p

S=0
for i in range(N):
    u=C.index(i)
    S+=u-i

    del C[u]
    C.insert(i,i)
print(S)
0