首页 > 代码库 > hdu3507 Print Article(斜率优化入门)(pascal)
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.
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的入门题,我看着两篇博客学了半天
传送门:
http://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html
http://www.cnblogs.com/kuangbin/archive/2012/08/26/2657650.html
第一个是别人推荐的,但上凸下凸好像搞反了。第二个也很不错。
斜率优化一开始学着难,搞懂之后也还好。
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)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。