hdu3507 Print Article(斜率优化入门)(pascal)

Posted Currier

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu3507 Print Article(斜率优化入门)(pascal)相关的知识,希望对你有一定的参考价值。

Problem Description
Zero 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.
 
 
Input
There 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.
 
 
Output
A single number, meaning the mininum cost to print the article.
 
 
Sample Input
5 5 5 9 5 7 5
 
 
Sample Output
230
 
 
Author
Xnozero
 
 
Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT 
 
 
 
这个是斜率优化dp的入门题,我看着两篇博客学了半天
传送门:
 
 
第一个是别人推荐的,但上凸下凸好像搞反了。第二个也很不错。
斜率优化一开始学着难,搞懂之后也还好。
 1 program rrr(input,output);
 2 var
 3   n,m,i,h,t:longint;
 4   q:array[0..500050]of longint;
 5   f,s:array[0..500050]of int64;
 6 function up(j,k:int64):int64;
 7 begin
 8    exit(f[j]-f[k]+sqr(s[j])-sqr(s[k]));
 9 end;
10 function down(j,k:int64):int64;
11 begin
12    exit((s[j]-s[k])<<1);
13 end;
14 begin
15    assign(input,\'r.in\');assign(output,\'r.out\');reset(input);rewrite(output);
16    while not eof do
17       begin
18          readln(n,m);
19          s[0]:=0;
20          for i:=1 to n do begin readln(s[i]);s[i]:=s[i]+s[i-1]; end;
21          h:=1;t:=1;q[1]:=0;f[0]:=0;
22          for i:=1 to n do
23             begin
24                while (h<t) and (up(q[h+1],q[h])<=s[i]*down(q[h+1],q[h])) do inc(h);
25                f[i]:=f[q[h]]+sqr(s[i]-s[q[h]])+m;
26                while (t>h) and (up(i,q[t])*down(q[t],q[t-1])<=up(q[t],q[t-1])*down(i,q[t])) do dec(t);
27                inc(t);q[t]:=i;
28             end;
29          writeln(f[n]);
30       end;
31    close(input);close(output);
32 end.

这代码一开始没用int64无限WA,后来改了int64才AC了,但我看网上c++代码没开longlong啊,不知道怎么回事。

以上是关于hdu3507 Print Article(斜率优化入门)(pascal)的主要内容,如果未能解决你的问题,请参考以下文章

DP(斜率优化):HDU 3507 Print Article

HDU 3507 Print Article(斜率DP优化)

HDU 3507 Print Article(斜率优化)

hdu3507 Print Article(斜率优化入门)(pascal)

HDU 3507 Print Article(斜率优化)

HDU3507 Print Article(斜率优化DP)