結果
問題 | No.409 ダイエット |
ユーザー | vwxyz |
提出日時 | 2023-01-05 14:24:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 220 ms / 2,000 ms |
コード長 | 13,300 bytes |
コンパイル時間 | 153 ms |
コンパイル使用メモリ | 82,364 KB |
実行使用メモリ | 118,200 KB |
最終ジャッジ日時 | 2024-05-06 19:02:51 |
合計ジャッジ時間 | 12,463 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
57,428 KB |
testcase_01 | AC | 48 ms
56,916 KB |
testcase_02 | AC | 46 ms
58,536 KB |
testcase_03 | AC | 46 ms
57,104 KB |
testcase_04 | AC | 45 ms
57,064 KB |
testcase_05 | AC | 41 ms
57,180 KB |
testcase_06 | AC | 44 ms
57,112 KB |
testcase_07 | AC | 45 ms
56,668 KB |
testcase_08 | AC | 44 ms
57,220 KB |
testcase_09 | AC | 46 ms
57,736 KB |
testcase_10 | AC | 44 ms
56,576 KB |
testcase_11 | AC | 43 ms
57,184 KB |
testcase_12 | AC | 45 ms
56,372 KB |
testcase_13 | AC | 44 ms
57,068 KB |
testcase_14 | AC | 45 ms
57,528 KB |
testcase_15 | AC | 46 ms
56,788 KB |
testcase_16 | AC | 45 ms
57,364 KB |
testcase_17 | AC | 44 ms
58,048 KB |
testcase_18 | AC | 44 ms
56,656 KB |
testcase_19 | AC | 45 ms
56,852 KB |
testcase_20 | AC | 44 ms
56,304 KB |
testcase_21 | AC | 44 ms
57,676 KB |
testcase_22 | AC | 44 ms
57,128 KB |
testcase_23 | AC | 44 ms
56,556 KB |
testcase_24 | AC | 44 ms
56,312 KB |
testcase_25 | AC | 45 ms
57,956 KB |
testcase_26 | AC | 45 ms
58,212 KB |
testcase_27 | AC | 45 ms
56,976 KB |
testcase_28 | AC | 46 ms
56,936 KB |
testcase_29 | AC | 45 ms
57,300 KB |
testcase_30 | AC | 44 ms
57,440 KB |
testcase_31 | AC | 46 ms
58,140 KB |
testcase_32 | AC | 45 ms
58,044 KB |
testcase_33 | AC | 46 ms
57,452 KB |
testcase_34 | AC | 45 ms
56,696 KB |
testcase_35 | AC | 83 ms
76,920 KB |
testcase_36 | AC | 81 ms
76,920 KB |
testcase_37 | AC | 74 ms
74,836 KB |
testcase_38 | AC | 79 ms
76,820 KB |
testcase_39 | AC | 83 ms
76,800 KB |
testcase_40 | AC | 83 ms
77,252 KB |
testcase_41 | AC | 72 ms
74,544 KB |
testcase_42 | AC | 83 ms
76,908 KB |
testcase_43 | AC | 80 ms
77,300 KB |
testcase_44 | AC | 71 ms
74,176 KB |
testcase_45 | AC | 83 ms
77,168 KB |
testcase_46 | AC | 81 ms
77,204 KB |
testcase_47 | AC | 82 ms
76,892 KB |
testcase_48 | AC | 78 ms
77,092 KB |
testcase_49 | AC | 69 ms
74,264 KB |
testcase_50 | AC | 81 ms
77,076 KB |
testcase_51 | AC | 80 ms
76,900 KB |
testcase_52 | AC | 72 ms
74,288 KB |
testcase_53 | AC | 80 ms
76,812 KB |
testcase_54 | AC | 79 ms
77,024 KB |
testcase_55 | AC | 160 ms
96,452 KB |
testcase_56 | AC | 174 ms
110,112 KB |
testcase_57 | AC | 212 ms
111,476 KB |
testcase_58 | AC | 145 ms
94,668 KB |
testcase_59 | AC | 181 ms
101,144 KB |
testcase_60 | AC | 143 ms
91,884 KB |
testcase_61 | AC | 207 ms
107,396 KB |
testcase_62 | AC | 202 ms
110,756 KB |
testcase_63 | AC | 209 ms
107,324 KB |
testcase_64 | AC | 157 ms
96,444 KB |
testcase_65 | AC | 213 ms
107,044 KB |
testcase_66 | AC | 217 ms
111,316 KB |
testcase_67 | AC | 185 ms
109,036 KB |
testcase_68 | AC | 170 ms
101,364 KB |
testcase_69 | AC | 220 ms
106,896 KB |
testcase_70 | AC | 202 ms
105,564 KB |
testcase_71 | AC | 145 ms
94,872 KB |
testcase_72 | AC | 216 ms
118,200 KB |
testcase_73 | AC | 213 ms
112,212 KB |
testcase_74 | AC | 155 ms
99,724 KB |
testcase_75 | AC | 214 ms
112,356 KB |
testcase_76 | AC | 172 ms
99,364 KB |
testcase_77 | AC | 157 ms
94,696 KB |
testcase_78 | AC | 143 ms
96,604 KB |
testcase_79 | AC | 152 ms
92,904 KB |
testcase_80 | AC | 210 ms
116,600 KB |
testcase_81 | AC | 218 ms
112,764 KB |
testcase_82 | AC | 184 ms
100,508 KB |
testcase_83 | AC | 198 ms
101,684 KB |
testcase_84 | AC | 178 ms
102,432 KB |
testcase_85 | AC | 99 ms
77,596 KB |
testcase_86 | AC | 172 ms
99,740 KB |
testcase_87 | AC | 201 ms
105,916 KB |
testcase_88 | AC | 141 ms
91,088 KB |
testcase_89 | AC | 180 ms
100,488 KB |
testcase_90 | AC | 135 ms
88,900 KB |
testcase_91 | AC | 195 ms
102,852 KB |
ソースコード
import sys readline=sys.stdin.readline from collections import deque class AVL_Node_dict: """ノード Attributes: key (any): ノードのキー。比較可能なものであれば良い。(1, 4)などタプルも可。 val (any): ノードの値。 left (Node): 左の子ノード。 right (Node): 右の子ノード。 bias (int): 平衡度。(左部分木の高さ)-(右部分木の高さ)。 size (int): 自分を根とする部分木の大きさ """ def __init__(self,parent,key,value): self.parent=parent self.key=key self.value=value self.left=None self.right=None self.bias=0 self.size=1 class AVLTree_dict: def __init__(self): self.root=None def Rotate_Left(self,node): node_right=node.right node_right.size=node.size node.size-=1 if node_right.right!=None: node.size-=node_right.right.size if node_right.bias==-1: node_right.bias=0 node.bias=0 else: #assert node_right.bias==0 node_right.bias=1 node.bias=-1 node.right=node_right.left node_right.left=node return node_right def Rotate_Right(self,node): node_left=node.left node_left.size=node.size node.size-=1 if node_left.left!=None: node.size-=node_left.left.size if node_left.bias==1: node_left.bias=0 node.bias=0 else: #assert node_left.bias==0 node_left.bias=-1 node.bias=1 node.left=node_left.right node_left.right=node return node_left def Rotate_Left_Right(self,node): node_left=node.left node_left_right=node_left.right #assert node.bias==2 #assert node_left.bias==-1 #assert node_left_right.bias in (-1,0,1) node_left_right.size=node.size node.size-=node_left.size if node_left_right.right!=None: node.size+=node_left_right.right.size node_left.size-=1 if node_left_right.right!=None: node_left.size-=node_left_right.right.size node_left.right=node_left_right.left node_left_right.left=node_left node.left=node_left_right.right node_left_right.right=node self.Update_Bias_Double(node_left_right) return node_left_right def Rotate_Right_Left(self,node): node_right=node.right node_right_left=node_right.left #assert node.bias==-2 #assert node_right.bias==1 #assert node_right_left.bias in (-1,0,1) node_right_left.size=node.size node.size-=node_right.size if node_right_left.left!=None: node.size+=node_right_left.left.size node_right.size-=1 if node_right_left.left!=None: node_right.size-=node_right_left.left.size node_right.left=node_right_left.right node_right_left.right=node_right node.right=node_right_left.left node_right_left.left=node self.Update_Bias_Double(node_right_left) return node_right_left def Update_Bias_Double(self,node): #assert node.right.bias*node.left.bias==-2 #assert node.right.bias>0 if node.bias==1: node.right.bias=-1 node.left.bias=0 elif node.bias==-1: node.right.bias=0 node.left.bias=1 else: node.right.bias=0 node.left.bias=0 node.bias=0 def __getitem__(self,key): v=self.root while v!=None: if key<v.key: v=v.left elif v.key<key: v=v.right else: return v.value return None def __setitem__(self,key,value): if self.root==None: self.root=AVL_Node_dict(None,key,value) return v=self.root stack=[] while v!=None: if key<v.key: stack.append((v,1)) v=v.left elif v.key<key: stack.append((v,-1)) v=v.right elif v.key==key: v.value=value return p,direction=stack[-1] if direction==1: p.left=AVL_Node_dict(p,key,value) else: p.right=AVL_Node_dict(p,key,value) while stack: v,direction=stack.pop() v.bias+=direction v.size+=1 vv=None if v.bias==2: if v.left.bias==-1: vv=self.Rotate_Left_Right(v) else: vv=self.Rotate_Right(v) #assert vv!=None break if v.bias==-2: if v.right.bias==1: vv=self.Rotate_Right_Left(v) else: vv=self.Rotate_Left(v) #assert vv!=None break if v.bias==0: break if vv!=None: if len(stack)==0: self.root=vv return p,direction=stack.pop() p.size+=1 if direction==1: p.left=vv else: p.right=vv while stack: p,direction=stack.pop() p.size+=1 def __delitem__(self,key): v=self.root stack=[] while v!=None: if key<v.key: stack.append((v,1)) v=v.left elif v.key<key: stack.append((v,-1)) v=v.right else: break else: return False if v.left!=None: stack.append((v,1)) lmax=v.left while lmax.right!=None: stack.append((lmax,-1)) lmax=lmax.right v.key=lmax.key v.value=lmax.value v=lmax c=v.right if v.left==None else v.left if stack: p,direction=stack[-1] if direction==1: p.left=c else: p.right=c else: self.root=c return True while stack: pp=None p,direction=stack.pop() p.bias-=direction p.size-=1 if p.bias==2: if p.left.bias==-1: pp=self.Rotate_Left_Right(p) else: pp=self.Rotate_Right(p) elif p.bias==-2: if p.right.bias==1: pp=self.Rotate_Right_Left(p) else: pp=self.Rotate_Left(p) elif p.bias!=0: break if pp!=None: if len(stack)==0: self.root=pp return True p,direction=stack[-1] if direction==1: p.left=pp else: p.right=pp if pp.bias!=0: break while stack: p,direction=stack.pop() p.size-=1 return True def __contain__(self,key): v=self.root while v!=None: if key<v.key: v=v.left elif v.key<key: v=v.right else: return True return False def Bisect_Right(self,key): retu=None v=self.root while v!=None: if v.key>key: if retu==None or retu[0]>v.key: retu=(v.key,v.value) v=v.left else: v=v.right return retu def Bisect_Left(self,key): retu=None v=self.root while v!=None: if v.key<key: if retu==None or retu[0]<v.key: retu=(v.key,v.value) v=v.right else: v=v.left return retu def Find_Kth_Element(self,K): v=self.root s=0 while v!=None: t=s+v.left.size if v.left!=None else s if t==K: return v.key,v.value elif t<K: s=t+1 v=v.right else: v=v.left return None def keys(self): stack=[(self.root,True)] while stack: node,subtree=stack.pop() if subtree: if node.right!=None: stack.append((node.right,True)) stack.append((node,False)) if node.left!=None: stack.append((node.left,True)) else: yield node.key def values(self): stack=[(self.root,True)] while stack: node,subtree=stack.pop() if subtree: if node.right!=None: stack.append((node.right,True)) stack.append((node,False)) if node.left!=None: stack.append((node.left,True)) else: yield node.value def items(self): stack=[(self.root,True)] while stack: node,subtree=stack.pop() if subtree: if node.right!=None: stack.append((node.right,True)) stack.append((node,False)) if node.left!=None: stack.append((node.left,True)) else: yield (node.key,node.value) def __bool__(self): return self.root!=None def __len__(self): return 0 if self.root==None else self.root.size def __iter__(self): return iter(self.keys()) def __str__(self): if self.root==None: retu="{}" else: retu="{"+", ".join(f"{r}: {m}" for r,m in self.items())+"}" return retu class Convex_Hull_Trick: def __init__(self): self.lines=AVLTree_dict() def is_removed(self,line0,line1,line2): if line0==None: return False if line2==None: return False a0,b0=line0 a1,b1=line1 a2,b2=line2 return (a1-a0)*(b2-b1)<=(b1-b0)*(a2-a1) def add_line(self,a,b): bb=self.lines[a] if bb!=None and bb<=b: return line0=self.lines.Bisect_Left(a) line1=(a,b) line2=self.lines.Bisect_Right(a) if self.is_removed(line0,line1,line2): return self.lines[a]=b line2=(a,b) line1=self.lines.Bisect_Left(line2[0]) line0=None if line1==None else self.lines.Bisect_Left(line1[0]) while self.is_removed(line0,line1,line2): del self.lines[line1[0]] line1=line0 line0=None if line1==None else self.lines.Bisect_Left(line1[0]) line0=(a,b) line1=self.lines.Bisect_Right(line0[0]) line2=None if line1==None else self.lines.Bisect_Right(line1[0]) while self.is_removed(line0,line1,line2): del self.lines[line1[0]] line1=line2 line2=None if line1==None else self.lines.Bisect_Right(line1[0]) def __call__(self,x): size=len(self.lines) if not size: return None ok,ng=-1,size-1 while ng-ok>1: mid=(ok+ng)//2 a0,b0=self.lines.Find_Kth_Element(mid) a1,b1=self.lines.Find_Kth_Element(mid+1) if a0*x+b0>a1*x+b1: ok=mid else: ng=mid a,b=self.lines.Find_Kth_Element(ok+1) return a*x+b def __getitem__(self,a): return self.lines[a] def __setitem__(self,a,b): self.add_line(a,b) class ConvexHullTrick: """ f_i = a_ix + b_i とする。f_i の追加および、min_i f(x) の取得ができるデータ構造。 ただし、傾き a_i は降順に追加されなければならない。 また、クエリ x も昇順に実行されなければならない。 """ def __init__(self): self.funcs = deque() def add(self, a, b): funcs = self.funcs while len(funcs) >= 2: a1, b1 = funcs[-2] a2, b2 = funcs[-1] if (a2 - a1) * (b - b2) < (b2 - b1) * (a - a2): break funcs.pop() funcs.append((a, b)) def query(self, x): funcs = self.funcs a, b = funcs[0] y = a * x + b while len(funcs) >= 2: a2, b2 = funcs[1] y2 = a2 * x + b2 if y < y2: break y = y2 funcs.popleft() return y N,A,B,W=map(int,readline().split()) D=list(map(int,readline().split())) inf=1<<60 dp=[inf]*N CHT=ConvexHullTrick() for i in range(N): dp[i]=W*2-2*A*i+B*i*(i+1)+D[i]*2 if i: dp[i]=min(dp[i],CHT.query(i)+D[i]*2-2*i*A+B*i**2) CHT.add(-B*(2*i+1),dp[i]+2*(i+1)*A+B*i*(i+1)) ans=W-A*N+B*N*(N+1)//2 for i in range(N): ans=min(ans,(dp[i]-2*A*(N-1-i)+B*(N-i)*(N-i-1))//2) print(ans)