zoukankan      html  css  js  c++  java
  • 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啊,不知道怎么回事。

  • 相关阅读:
    微信小程序-movable-view
    微信小程序-swiper组件
    大文件上传的php.ini配置和apache或者nginx需要的配置
    Yii2.0 实现关联查询
    Yii2.0关闭自带的debug功能
    创建日志文件,并且追加内容
    微信小程序实现计算器功能
    Yii2.0实现后台接收json数据处理数据修改功能
    Yii2.0成功或者失败提示➕页面跳转
    Yii2.0配置pathinfo模式
  • 原文地址:https://www.cnblogs.com/Currier/p/6607163.html
Copyright © 2011-2022 走看看