码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-23 21:45:32      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:title   ram   article   ott   bin   sample   无限   certainly   targe   

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)

标签:title   ram   article   ott   bin   sample   无限   certainly   targe   

原文地址:http://www.cnblogs.com/Currier/p/6607163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!