結果
問題 | No.738 平らな農地 |
ユーザー | vwxyz |
提出日時 | 2024-04-06 05:20:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 678 ms / 2,000 ms |
コード長 | 5,832 bytes |
コンパイル時間 | 297 ms |
コンパイル使用メモリ | 82,120 KB |
実行使用メモリ | 126,288 KB |
最終ジャッジ日時 | 2024-10-01 03:43:32 |
合計ジャッジ時間 | 32,952 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
56,196 KB |
testcase_01 | AC | 44 ms
55,720 KB |
testcase_02 | AC | 42 ms
55,588 KB |
testcase_03 | AC | 44 ms
56,748 KB |
testcase_04 | AC | 42 ms
55,632 KB |
testcase_05 | AC | 148 ms
78,128 KB |
testcase_06 | AC | 149 ms
78,232 KB |
testcase_07 | AC | 135 ms
77,844 KB |
testcase_08 | AC | 114 ms
77,648 KB |
testcase_09 | AC | 102 ms
76,760 KB |
testcase_10 | AC | 71 ms
74,216 KB |
testcase_11 | AC | 112 ms
77,352 KB |
testcase_12 | AC | 99 ms
76,384 KB |
testcase_13 | AC | 136 ms
77,492 KB |
testcase_14 | AC | 107 ms
76,572 KB |
testcase_15 | AC | 478 ms
108,448 KB |
testcase_16 | AC | 507 ms
111,916 KB |
testcase_17 | AC | 574 ms
112,028 KB |
testcase_18 | AC | 568 ms
108,184 KB |
testcase_19 | AC | 593 ms
112,612 KB |
testcase_20 | AC | 512 ms
112,036 KB |
testcase_21 | AC | 601 ms
112,304 KB |
testcase_22 | AC | 535 ms
112,044 KB |
testcase_23 | AC | 592 ms
112,052 KB |
testcase_24 | AC | 590 ms
112,200 KB |
testcase_25 | AC | 106 ms
76,616 KB |
testcase_26 | AC | 108 ms
77,028 KB |
testcase_27 | AC | 111 ms
76,644 KB |
testcase_28 | AC | 109 ms
76,760 KB |
testcase_29 | AC | 111 ms
76,596 KB |
testcase_30 | AC | 104 ms
76,812 KB |
testcase_31 | AC | 99 ms
76,580 KB |
testcase_32 | AC | 100 ms
76,444 KB |
testcase_33 | AC | 108 ms
76,824 KB |
testcase_34 | AC | 108 ms
76,988 KB |
testcase_35 | AC | 107 ms
76,444 KB |
testcase_36 | AC | 109 ms
76,620 KB |
testcase_37 | AC | 105 ms
76,636 KB |
testcase_38 | AC | 111 ms
76,900 KB |
testcase_39 | AC | 113 ms
76,748 KB |
testcase_40 | AC | 107 ms
77,312 KB |
testcase_41 | AC | 109 ms
76,436 KB |
testcase_42 | AC | 103 ms
76,416 KB |
testcase_43 | AC | 105 ms
76,768 KB |
testcase_44 | AC | 105 ms
76,852 KB |
testcase_45 | AC | 505 ms
115,528 KB |
testcase_46 | AC | 492 ms
111,096 KB |
testcase_47 | AC | 496 ms
115,068 KB |
testcase_48 | AC | 468 ms
114,944 KB |
testcase_49 | AC | 443 ms
112,784 KB |
testcase_50 | AC | 447 ms
115,020 KB |
testcase_51 | AC | 507 ms
114,948 KB |
testcase_52 | AC | 491 ms
112,612 KB |
testcase_53 | AC | 483 ms
115,072 KB |
testcase_54 | AC | 479 ms
115,296 KB |
testcase_55 | AC | 514 ms
115,100 KB |
testcase_56 | AC | 500 ms
115,016 KB |
testcase_57 | AC | 467 ms
110,796 KB |
testcase_58 | AC | 489 ms
115,088 KB |
testcase_59 | AC | 460 ms
115,588 KB |
testcase_60 | AC | 470 ms
115,372 KB |
testcase_61 | AC | 466 ms
115,840 KB |
testcase_62 | AC | 452 ms
110,872 KB |
testcase_63 | AC | 507 ms
115,468 KB |
testcase_64 | AC | 518 ms
115,312 KB |
testcase_65 | AC | 592 ms
112,200 KB |
testcase_66 | AC | 609 ms
112,696 KB |
testcase_67 | AC | 421 ms
120,732 KB |
testcase_68 | AC | 408 ms
119,508 KB |
testcase_69 | AC | 543 ms
117,956 KB |
testcase_70 | AC | 503 ms
116,168 KB |
testcase_71 | AC | 116 ms
88,900 KB |
testcase_72 | AC | 113 ms
89,748 KB |
testcase_73 | AC | 116 ms
90,720 KB |
testcase_74 | AC | 119 ms
91,252 KB |
testcase_75 | AC | 526 ms
121,016 KB |
testcase_76 | AC | 487 ms
120,064 KB |
testcase_77 | AC | 581 ms
120,816 KB |
testcase_78 | AC | 603 ms
117,448 KB |
testcase_79 | AC | 576 ms
111,272 KB |
testcase_80 | AC | 478 ms
115,892 KB |
testcase_81 | AC | 508 ms
118,004 KB |
testcase_82 | AC | 546 ms
114,160 KB |
testcase_83 | AC | 247 ms
90,184 KB |
testcase_84 | AC | 185 ms
90,352 KB |
testcase_85 | AC | 678 ms
126,288 KB |
testcase_86 | AC | 629 ms
120,488 KB |
testcase_87 | AC | 244 ms
125,648 KB |
testcase_88 | AC | 236 ms
120,972 KB |
testcase_89 | AC | 43 ms
55,816 KB |
testcase_90 | AC | 42 ms
55,120 KB |
testcase_91 | AC | 43 ms
56,376 KB |
ソースコード
from collections import defaultdict class Segment_Tree: def __init__(self,N,f,e,lst=None,dynamic=False): self.f=f self.e=e self.N=N if dynamic: self.segment_tree=defaultdict(lambda:self.e) else: if lst==None: self.segment_tree=[self.e]*2*self.N else: assert len(lst)<=self.N self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst)) for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def __getitem__(self,i): if type(i)==int: if -self.N<=i<0: return self.segment_tree[i+self.N*2] elif 0<=i<self.N: return self.segment_tree[i+self.N] else: raise IndexError("list index out of range") else: a,b,c=i.start,i.stop,i.step if a==None: a=self.N else: a+=self.N if b==None: b=self.N*2 else: b+=self.N return self.segment_tree[slice(a,b,c)] def __setitem__(self,i,x): if -self.N<=i<0: i+=self.N*2 elif 0<=i<self.N: i+=self.N else: raise IndexError("list index out of range") self.segment_tree[i]=x while i>1: i>>= 1 self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Build(self,lst): for i,x in enumerate(lst,self.N): self.segment_tree[i]=x for i in range(self.N-1,0,-1): self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1]) def Fold(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N vL=self.e vR=self.e while L<R: if L&1: vL=self.f(vL,self.segment_tree[L]) L+=1 if R&1: R-=1 vR=self.f(self.segment_tree[R],vR) L>>=1 R>>=1 return self.f(vL,vR) def Fold_Index(self,L=None,R=None): if L==None: L=self.N else: L+=self.N if R==None: R=self.N*2 else: R+=self.N if L==R: return None x=self.Fold(L-self.N,R-self.N) while L<R: if L&1: if self.segment_tree[L]==x: i=L break L+=1 if R&1: R-=1 if self.segment_tree[R]==x: i=R break L>>=1 R>>=1 while i<self.N: if self.segment_tree[i]==self.segment_tree[i<<1]: i<<=1 else: i<<=1 i|=1 i-=self.N return i def Bisect_Right(self,L=None,f=None): if L==self.N: return self.N if L==None: L=0 L+=self.N vl=self.e vr=self.e l,r=L,self.N*2 while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return self.N v=self.e while True: while L%2==0: L>>=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 else: while L<self.N: L<<=1 vv=self.f(v,self.segment_tree[L]) if f(vv): v=vv L+=1 return L-self.N def Bisect_Left(self,R=None,f=None): if R==0: return 0 if R==None: R=self.N R+=self.N vl=self.e vr=self.e l,r=self.N,R while l<r: if l&1: vl=self.f(vl,self.segment_tree[l]) l+=1 if r&1: r-=1 vr=self.f(self.segment_tree[r],vr) l>>=1 r>>=1 if f(self.f(vl,vr)): return 0 v=self.e while True: R-=1 while R>1 and R%2: R>>=1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv else: while R<self.N: R=2*R+1 vv=self.f(self.segment_tree[R],v) if f(vv): v=vv R-=1 return R+1-self.N def __str__(self): return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]" def Compress(lst): decomp=sorted(list(set(lst))) comp={x:i for i,x in enumerate(decomp)} return comp,decomp N,K=map(int,input().split()) A=list(map(int,input().split())) inf=1<<60 ans=inf comp,decomp=Compress(A) le=len(comp) ST_cnt=Segment_Tree(le,lambda x,y:x+y,0) ST_sum=Segment_Tree(le,lambda x,y:x+y,0) for k in range(K): ST_cnt[comp[A[k]]]+=1 ST_sum[comp[A[k]]]+=A[k] for i in range(N-K+1): m=ST_cnt.Bisect_Right(0,lambda cnt:cnt<=K//2) sl=ST_cnt.Fold(0,m)*decomp[m]-ST_sum.Fold(0,m) sr=ST_sum.Fold(m+1,le)-ST_cnt.Fold(m+1,le)*decomp[m] ans=min(ans,sl+sr) if i<N-K: ST_cnt[comp[A[i]]]-=1 ST_sum[comp[A[i]]]-=A[i] ST_cnt[comp[A[i+K]]]+=1 ST_sum[comp[A[i+K]]]+=A[i+K] print(ans)