Problem DescriptionZero has an old printer that doesn’t work well sometimes. As it is antique, he still like to use it to print articles. But it is too old to work for a long time and it will certainly wear and tear, so Zero use a cost to evaluate this degree.
One day Zero want to print an article
which has N words, and each word i has a cost Ci to be printed. Also, Zero know
that print k words in one line will cost
M is a const number.
Now Zero
want to know the minimum cost in order to arrange the article
perfectly. InputThere are many test cases. For each test case, There
are two numbers N and M in the first line (0 ≤ n ≤ 500000, 0 ≤ M ≤ 1000). Then, there are N numbers in the next 2
to N + 1 lines. Input are terminated by EOF. OutputA single number, meaning the mininum cost to print the
article. Sample Input5 5
5
9
5
7
5 Sample Output230 AuthorXnozero Source2010
ACM-ICPC Multi-University Training Contest(7)——Host by HIT 这个是斜率优化dp的入门题,我看着两篇博客学了半天传送门:http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html http://www.cnblogs.com/kuangbin/archive/2012/08/26/2657650.html 第一个是别人推荐的,但上凸下凸好像搞反了。第二个也很不错。斜率优化一开始学着难,搞懂之后也还好。
program rrr(input,output);
var
n,m,i,h,t:longint;
q:array[..]of longint;
f,s:array[..]of int64;
function up(j,k:int64):int64;
begin
exit(f[j]-f[k]+sqr(s[j])-sqr(s[k]));
end;
function down(j,k:int64):int64;
begin
exit((s[j]-s[k])<<);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
while not eof do
begin
readln(n,m);
s[]:=;
for i:= to n do begin readln(s[i]);s[i]:=s[i]+s[i-]; end;
h:=;t:=;q[]:=;f[]:=;
for i:= to n do
begin
while (h<t) and (up(q[h+],q[h])<=s[i]*down(q[h+],q[h])) do inc(h);
f[i]:=f[q[h]]+sqr(s[i]-s[q[h]])+m;
while (t>h) and (up(i,q[t])*down(q[t],q[t-])<=up(q[t],q[t-])*down(i,q[t])) do dec(t);
inc(t);q[t]:=i;
end;
writeln(f[n]);
end;
close(input);close(output);
end.
这代码一开始没用int64无限WA,后来改了int64才AC了,但我看网上c++代码没开longlong啊,不知道怎么回事。