結果

問題 No.2029 Swap Min Max Min
ユーザー 👑 KazunKazun
提出日時 2022-08-05 21:55:16
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,396 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 87,164 KB
実行使用メモリ 113,392 KB
最終ジャッジ日時 2023-10-14 00:10:43
合計ジャッジ時間 10,863 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,608 KB
testcase_01 AC 92 ms
71,708 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 139 ms
89,120 KB
testcase_06 AC 173 ms
101,804 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 126 ms
83,320 KB
testcase_11 WA -
testcase_12 AC 151 ms
92,424 KB
testcase_13 WA -
testcase_14 AC 199 ms
113,172 KB
testcase_15 WA -
testcase_16 AC 196 ms
113,064 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 199 ms
113,180 KB
testcase_20 AC 198 ms
113,392 KB
testcase_21 AC 195 ms
112,840 KB
testcase_22 AC 196 ms
113,168 KB
testcase_23 AC 218 ms
113,192 KB
testcase_24 AC 221 ms
113,284 KB
testcase_25 AC 195 ms
113,376 KB
testcase_26 AC 199 ms
113,268 KB
testcase_27 AC 92 ms
71,508 KB
testcase_28 AC 101 ms
76,752 KB
testcase_29 AC 97 ms
72,380 KB
testcase_30 AC 99 ms
76,584 KB
testcase_31 AC 101 ms
76,848 KB
testcase_32 WA -
testcase_33 AC 95 ms
71,768 KB
testcase_34 AC 92 ms
71,480 KB
testcase_35 AC 93 ms
71,768 KB
testcase_36 AC 93 ms
71,888 KB
testcase_37 AC 92 ms
71,868 KB
testcase_38 AC 205 ms
108,680 KB
testcase_39 AC 189 ms
113,212 KB
testcase_40 AC 186 ms
108,988 KB
testcase_41 AC 211 ms
109,320 KB
testcase_42 AC 214 ms
109,440 KB
testcase_43 AC 191 ms
112,476 KB
testcase_44 AC 223 ms
113,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

            for i in range(n):
                self.ind[p[i]]=i

        self.n=n

    def __getitem__(self, k):
        return self.p[k]

    def __str__(self):
        return str(self.p)

    __repr__=__str__

    def __eq__(self,other):
        return (self.n==other.n) and (self.p==other.p)

    def __mul__(self,other):
        assert self.n==other.n

        p=self.p; q=other.p
        return Permutation(self.n,  [p[q[i]] for i in range(self.n)])

    def __pow__(self, n):
        if n<0:
            return pow(self,-n).inverse()

        a=list(range(self.n))
        e=self.p[:]

        while n:
            if n&1:
                a=[a[e[i]] for i in range(self.n)]
            e=[e[e[i]] for i in range(self.n)]
            n>>=1

        return Permutation(self.n, a)

    def __truediv__(self,other):
        pass

    def sgn(self):
        """ 置換の符号を求める (偶置換 → 1, 奇置換 → -1)

        """
        return -1 if self.minimum_transposition()%2 else 1

    def inverse(self):
        return Permutation(self.n, self.ind)

    def inversion(self):
        BIT=[0]*(self.n+1)
        Y=(self.n*(self.n-1))//2

        for a in self.p:
            s=a
            while 1<=s:
                Y-=BIT[s]
                s-=s&(-s)

            r=a+1
            while r<=self.n:
                BIT[r]+=1
                r+=r&(-r)
        return Y

    def swap(self, i, j):
        """ i 番目と j 番目を交換する ※ i と j を交換ではない"""

        u=self.p[i]; v=self.p[j]

        self.p[i]=v; self.p[j]=u
        self.ind[v]=i; self.ind[u]=j

    def transposition(self, u, v):
        """ u,v のある場所を交換する ※ u 番目とv 番目ではない"""

        a=self.ind[u]; b=self.ind[v]

        self.p[a]=v; self.p[b]=u
        self.ind[u]=b; self.ind[v]=a

    def minimum_transposition(self):
        """ 互換の最小回数を求める. """

        return self.n-len(self.cycle_division())

    def cycle_division(self, mode=True):
        """ 置換を巡回置換の積に分解する.

        mode: 自己ループを入れるか否か"""

        p=self.p
        T=[False]*self.n
        A=[]

        for k in range(self.n):
            if not T[k]:
                a=[k]

                T[k]=True
                v=p[k]
                while v!=k:
                    T[v]=True
                    a.append(v)
                    v=p[v]

                if mode or len(a)>=2:
                    A.append(a)
        return A

    def operate_list(self, list):
        assert self.n==len(list),"置換の長さとリストの長さが違います."

        return [list[self.ind[i]] for i in range(self.n)]


    def order(self):
        from math import gcd

        x=1
        for m in self.cycle_division():
            g=gcd(x,len(m))
            x=(x//g)*len(m)
        return x

#=================================================
def Permutation_Inversion(P,Q):
    """ P から Q へ隣接項同士の入れ替えのみの最小回数を求める.
    """
    R=Q*(P.inverse())
    return R.inversion()

def List_Inversion(A,B,default=-1):
    """長さが等しいリスト A,B に対して, 以下の操作の最小回数を求める.
    列 A[i] と A[i+1] を入れ替え, B と一致させる.
    """

    from collections import defaultdict

    if len(A)!=len(B):
        return default

    N=len(A)
    D=defaultdict(list)

    for i in range(N):
        D[A[i]].append(i)

    for lis in D:
        D[lis].reverse()

    try:
        return Permutation(N,[D[B[i]].pop() for i in range(N)]).inversion()
    except:
        return default

def solve():
    N=int(input())
    A=[-1]+list(map(int,input().split()))

    X=N//2
    P=[0]*(N+1); Q=[0]*(N+1); R=[0]*(N+1)
    for i in range(N+1):
        if i==0:
            P[i]=Q[i]=R[i]=-1
            continue

        if A[i]<=X:
            P[i]=1
        else:
            P[i]=0

        if i%2==0:
            Q[i]=1; R[i]=0
        else:
            Q[i]=0; R[i]=1

    if N%2==1:
        print(X,List_Inversion(P,Q,-1))
    else:
        print(X,min(List_Inversion(P,Q,-1),List_Inversion(P,R,-1)))

solve()
0