HNOI2008玩具装箱 (斜率优化)
总算A了,心情好激动……
如果会了一类斜率优化,基本上这类题就成了套模版了……
只是k函数不同
1 var n,l,x,tail,head,m:int64; 2 i,j:longint; 3 dp,q,s:array[0..100000] of int64; 4 function k(x,y:longint):double; 5 begin 6 k:=1.0*((dp[x]+s[x]*s[x]-dp[y]-s[y]*s[y])/(s[x]-s[y])); 7 end; 8 procedure main; 9 begin 10 readln(n,l);s[0]:=0; 11 for i:=1 to n do 12 begin 13 readln(x); 14 s[i]:=s[i-1]+x; 15 end; 16 for i:=1 to n do s[i]:=s[i]+i; 17 head:=0;tail:=0; 18 for i:=1 to n do 19 begin 20 m:=s[i]-l-1; 21 while (head<tail) and (k(q[head+1],q[head])<=2*m) do inc(head); 22 j:=q[head]; 23 dp[i]:=dp[j]+(m-s[j])*(m-s[j]); 24 while (head<tail) and (k(q[tail],q[tail-1])>=k(i,q[tail])) do dec(tail); 25 inc(tail);q[tail]:=i; 26 end; 27 writeln(dp[n]); 28 end; 29 begin 30 main; 31 end.
还有一份写得很不错的题解:
http://www.cnblogs.com/perseawe/archive/2012/05/12/BZ1010.html