APIO2010特别行动队(单调队列、斜率优化)

时间:2014-06-08 08:24:10   收藏:0   阅读:375

其实这题一看知道应该是DP,再一看数据范围肯定就是单调队列了。

不过我还不太懂神马单调队列、斜率优化……

附上天牛的题解:http://www.cnblogs.com/neverforget/archive/2012/04/19/2456483.html

bubuko.com,布布扣
 1 var f,g:array[0..1000050] of int64;
 2     s,q:array[0..1000050] of longint;
 3     a,b,c,n,i,h,t,x:longint;
 4     bestk:double;
 5 procedure init;
 6  begin
 7  readln(n);
 8  readln(a,b,c);
 9  s[0]:=0;
10  for i:=1 to n do
11   begin
12   read(x);
13   s[i]:=s[i-1]+x;
14   end;
15  end;
16 function k(x,y:longint):double;
17  begin
18  exit(double(g[y]-g[x])/(s[y]-s[x]));
19  end;
20 procedure main;
21  begin
22  f[0]:=0;h:=1;t:=1;q[1]:=0;
23  for i:=1 to n do
24   begin
25   bestk:=double(2*a*s[i]);
26   while (h<t) and (k(q[h],q[h+1])>=bestk) do inc(h);
27   f[i]:=int64(f[q[h]])+int64(a)*int64(s[i]-s[q[h]])*int64(s[i]-s[q[h]])
28        +int64(b)*int64(s[i]-s[q[h]])+int64(c);
29   g[i]:=int64(f[i])+int64(a)*int64(s[i])*int64(s[i])-int64(b)*int64(s[i]);
30   while (h+1<=t) and (k(q[t],i)>k(q[t-1],q[t])) do dec(t);
31   inc(t);
32   q[t]:=i;
33   end;
34  end;
35 procedure print;
36  begin
37  writeln(f[n]);
38  end;
39 begin
40  init;
41  main;
42  print;
43 end.                                                                                  
View Code

 

APIO2010特别行动队(单调队列、斜率优化),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!