結果

問題 No.2029 Swap Min Max Min
ユーザー 👑 KazunKazun
提出日時 2022-08-05 21:52:00
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,244 bytes
コンパイル時間 521 ms
コンパイル使用メモリ 87,384 KB
実行使用メモリ 112,116 KB
最終ジャッジ日時 2023-10-14 00:08:40
合計ジャッジ時間 9,300 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,544 KB
testcase_01 AC 93 ms
71,300 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 139 ms
88,720 KB
testcase_06 AC 169 ms
100,568 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 125 ms
82,856 KB
testcase_11 WA -
testcase_12 AC 147 ms
91,724 KB
testcase_13 WA -
testcase_14 AC 188 ms
111,544 KB
testcase_15 WA -
testcase_16 AC 188 ms
111,272 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 190 ms
111,440 KB
testcase_20 AC 193 ms
111,424 KB
testcase_21 AC 190 ms
111,672 KB
testcase_22 AC 191 ms
111,388 KB
testcase_23 WA -
testcase_24 AC 188 ms
111,712 KB
testcase_25 AC 188 ms
111,484 KB
testcase_26 AC 187 ms
111,584 KB
testcase_27 WA -
testcase_28 AC 101 ms
76,672 KB
testcase_29 AC 96 ms
72,340 KB
testcase_30 AC 99 ms
76,528 KB
testcase_31 AC 102 ms
76,500 KB
testcase_32 WA -
testcase_33 AC 93 ms
71,656 KB
testcase_34 WA -
testcase_35 AC 92 ms
71,696 KB
testcase_36 AC 92 ms
71,700 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 187 ms
111,504 KB
testcase_40 AC 176 ms
107,272 KB
testcase_41 AC 180 ms
107,924 KB
testcase_42 AC 178 ms
108,076 KB
testcase_43 AC 182 ms
110,868 KB
testcase_44 AC 183 ms
111,740 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)
    for i in range(N+1):
        if i==0:
            P[i]=Q[i]=-1

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

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

    print(X,List_Inversion(P,Q,-1))

solve()
0