結果

問題 No.2304 Distinct Elements
ユーザー vwxyzvwxyz
提出日時 2023-06-06 19:32:00
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,198 ms / 3,000 ms
コード長 6,837 bytes
コンパイル時間 77 ms
コンパイル使用メモリ 13,184 KB
実行使用メモリ 33,360 KB
最終ジャッジ日時 2024-06-09 06:18:03
合計ジャッジ時間 38,142 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
11,392 KB
testcase_01 AC 34 ms
11,520 KB
testcase_02 AC 34 ms
11,392 KB
testcase_03 AC 34 ms
11,392 KB
testcase_04 AC 1,139 ms
23,800 KB
testcase_05 AC 1,127 ms
23,812 KB
testcase_06 AC 1,141 ms
23,804 KB
testcase_07 AC 1,137 ms
23,680 KB
testcase_08 AC 1,147 ms
23,792 KB
testcase_09 AC 977 ms
33,204 KB
testcase_10 AC 979 ms
33,072 KB
testcase_11 AC 979 ms
33,204 KB
testcase_12 AC 993 ms
33,068 KB
testcase_13 AC 989 ms
33,072 KB
testcase_14 AC 35 ms
11,392 KB
testcase_15 AC 34 ms
11,392 KB
testcase_16 AC 34 ms
11,520 KB
testcase_17 AC 34 ms
11,392 KB
testcase_18 AC 34 ms
11,520 KB
testcase_19 AC 34 ms
11,392 KB
testcase_20 AC 33 ms
11,392 KB
testcase_21 AC 34 ms
11,520 KB
testcase_22 AC 33 ms
11,520 KB
testcase_23 AC 33 ms
11,520 KB
testcase_24 AC 34 ms
11,392 KB
testcase_25 AC 34 ms
11,520 KB
testcase_26 AC 33 ms
11,520 KB
testcase_27 AC 34 ms
11,392 KB
testcase_28 AC 34 ms
11,392 KB
testcase_29 AC 860 ms
27,596 KB
testcase_30 AC 88 ms
12,544 KB
testcase_31 AC 705 ms
24,800 KB
testcase_32 AC 771 ms
28,516 KB
testcase_33 AC 84 ms
12,928 KB
testcase_34 AC 788 ms
28,896 KB
testcase_35 AC 877 ms
31,176 KB
testcase_36 AC 75 ms
12,544 KB
testcase_37 AC 364 ms
16,768 KB
testcase_38 AC 1,183 ms
28,876 KB
testcase_39 AC 1,198 ms
29,156 KB
testcase_40 AC 739 ms
17,464 KB
testcase_41 AC 382 ms
14,504 KB
testcase_42 AC 1,102 ms
20,656 KB
testcase_43 AC 1,161 ms
33,360 KB
testcase_44 AC 690 ms
26,912 KB
testcase_45 AC 602 ms
23,076 KB
testcase_46 AC 465 ms
19,772 KB
testcase_47 AC 35 ms
11,520 KB
testcase_48 AC 34 ms
11,392 KB
testcase_49 AC 1,166 ms
30,020 KB
testcase_50 AC 1,168 ms
30,284 KB
testcase_51 AC 1,163 ms
30,276 KB
testcase_52 AC 1,166 ms
23,720 KB
testcase_53 AC 679 ms
30,152 KB
testcase_54 AC 1,132 ms
22,076 KB
testcase_55 AC 1,118 ms
21,404 KB
testcase_56 AC 1,116 ms
21,004 KB
testcase_57 AC 1,181 ms
30,136 KB
testcase_58 AC 1,145 ms
30,024 KB
testcase_59 AC 1,148 ms
22,228 KB
testcase_60 AC 34 ms
11,392 KB
testcase_61 AC 34 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import heapq
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item

class Slope_Trick:
    def __init__(self,L=False,R=False,median=False,label=False,f=None,f_inve=None,e=None):
        self.queueL=[]
        self.queueR=[]
        self.L=L
        self.R=R
        self.median=median
        if self.median:
            self.median_value=None
        self.label=label
        self.shiftL=0
        self.shiftR=0
        self.min_f=0
        if self.label:
            self.f=f
            self.f_inve=f_inve
            self.e=e
            self.labelL=self.e
            self.labelR=self.e

    def heappush(self,x):
        heappush(self.queueR,x-self.shiftR)
        if self.label:
            self.labelR=self.f(self.labelR,x)

    def heappop(self):
        x=heappop(self.queueR)+self.shiftR
        if self.label:
            self.labelR=self.f_inve(self.labelR,x)
        return x

    def heappushpop(self,x):
        y=heappushpop(self.queueR,x-self.shiftR)+self.shiftR
        if self.label:
            self.labelR=self.f(self.labelR,x)
            self.labelR=self.f_inve(self.labelR,y)
        return y

    def _heappush_max(self,x):
        _heappush_max(self.queueL,x-self.shiftL)
        if self.label:
            self.labelL=self.f(self.labelL,x)

    def _heappop_max(self):
        x=_heappop_max(self.queueL)+self.shiftL
        if self.label:
            self.labelL=self.f_inve(self.labelL,x)
        return x

    def _heappushpop_max(self,x):
        y=_heappushpop_max(self.queueL,x-self.shiftL)+self.shiftL
        if self.label:
            self.labelL=self.f(self.labelL,x)
            self.labelL=self.f_inve(self.labelL,y)
        return y

    def push_Left(self,x):
        if self.queueR:
            self.min_f+=max(x-(self.queueR[0]+self.shiftR),0)
        self._heappush_max(self.heappushpop(x))

    def push_Right(self,x):
        if self.queueL:
            self.min_f+=max((self.queueL[0]+self.shiftL)-x,0)
        self.heappush(self._heappushpop_max(x))

    def pop_Left(self):
        if self.queueL:
            retu=self._heappop_max()
            if self.queueR:
                self.min_f+=max(retu-(self.queueR[0]+self.shiftR),0)
        else:
            retu=None
        return retu

    def pop_Right(self):
        if self.queueR:
            retu=self.heappop()
            if self.queueL:
                self.min_f+=max((self.queueL[0]+self.shiftL)-retu,0)
        else:
            retu=None
        return retu

    def push(self,x):
        if self.L:
            if len(self.queueL)<self.L:
                self.push_Left(x)
            else:
                self.push_Right(x)
        if self.R:
            if len(self.queueR)<self.R:
                self.push_Right(x)
            else:
                self.push_Left(x)
        if self.median:
            if self.median_value==None:
                if self.queueL and x<self.queueL[0]:
                    self.median_value=self._heappushpop_max(x)
                elif self.queueR and self.queueR[0]<x:
                    self.median_value=self.heappushpop(x)
                else:
                    self.median_value=x
            else:
                if self.median_value<=x:
                    self._heappush_max(self.median_value)
                    self.heappush(x)
                else:
                    self._heappush_max(x)
                    self.heappush(self.median_value)
                self.median_value=None

    def pop(self):
        if self.L:
            if len(self.queueL)==self.L:
                retu=self._heappop_max()
                if self.queueR:
                    self._heappush_max(self.heappop())
            else:
                retu=None
        if self.R:
            if len(self.queueR)==self.R:
                retu=self.heappop()
                if self.queueL:
                    self.heappush(self._heappop_max())
            else:
                retu=None
        if self.median:
            if self.median_value==None:
                retu=(self.pop_Left(),self.pop_Right())
            else:
                retu=(self.median_value,None)
                self.median_value=None
        return retu

    def top(self):
        if self.L:
            if len(self.queueL)==self.L:
                retu=self.queueL[0]
            else:
                retu=None
        if self.R:
            if len(self.queueR)==self.R:
                retu=self.queueR[0]
            else:
                retu=None
        if self.median:
            if self.median_value==None:
                if self.queueL:
                    retu=(self.queueL[0],self.queueR[0])
                else:
                    retu=(None,None)
            else:
                retu=(self.median_value,None)
        return retu

    def top_Left(self):
        if self.queueL:
           return self.queueL[0]+self.shiftL
        else:
            return None

    def top_Right(self):
        if self.queueR:
           return self.queueR[0]+self.shiftR
        else:
            return None

    def Shift_Left(self,a):
        self.shiftL+=a

    def Shift_Right(self,a):
        self.shiftR+=a

    def Label_Left(self):
        return self.labelL

    def Label_Right(self):
        return self.labelR

    def Cumultive_min(self):
        self.queueR=[]

    def Cumultive_max(self):
        self.queueL=[]

    def __len__(self):
        retu=len(self.queueL)+len(self.queueR)
        if self.median and self.median_value!=None:
            retu+=1
        return retu

    def __call__(self,x):
        return sum(max((l+self.shiftL)-x,0) for l in self.queueL)+sum(max(x-(r+self.shiftR),0) for r in self.queueR)+self.min_f

    def __str__(self):
        if self.median:
            if self.median_value==None:
                return "["+", ".join(map(str,sorted([x+self.shiftL for x in self.queueL])))+"]+["+", ".join(map(str,sorted([x+self.shiftR for x in self.queueR])))+"]"
            else:
                return "["+", ".join(map(str,sorted([x+self.shiftL for x in self.queueL])))+"]+"+str(self.median_value)+"+["+", ".join(map(str,sorted([x+self.shiftR for x in self.queueR])))+"]"
        else:
            return "["+", ".join(map(str,sorted([x+self.shiftL for x in self.queueL])))+"]+["+", ".join(map(str,sorted([x+self.shiftR for x in self.queueR])))+"]"

N=int(readline())
A=sorted(list(map(int,readline().split())))
ST=Slope_Trick()
for a in A:
    ST.Cumultive_min()
    ST.Shift_Left(1)
    ST.push_Right(a)
    ST.push_Left(a)
ans=ST.min_f
print(ans)
0