博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3507 Print Article
阅读量:4334 次
发布时间:2019-06-07

本文共 2071 字,大约阅读时间需要 6 分钟。

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
 
题解:
斜率优化dp板子题.单调队列维护 x满足递增 非常好写
一个结论:满足条件的点一定在一个凸包的底端,所以我们维护凸包底端的点即可
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 typedef long long ll; 9 const int N=500005;10 int gi(){11 int str=0;char ch=getchar();12 while(ch>'9' || ch<'0')ch=getchar();13 while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();14 return str;15 }16 int n,m,a[N],q[N];ll sum[N],f[N];17 ll fy(int i,int j){18 return sum[i]*sum[i]+f[i]-sum[j]*sum[j]-f[j];19 }20 ll fx(int i,int j){21 return ((sum[i]-sum[j])<<1);22 } 23 void work(){24 int l=1,r=1,j,k;25 q[1]=0;26 for(int i=1;i<=n;i++)a[i]=gi(),sum[i]=sum[i-1]+a[i];27 for(int i=1;i<=n;i++){28 while(l<=r-1){29 j=q[l];k=q[l+1];30 if(fy(j,k)>=sum[i]*fx(j,k))l++;31 else break;32 }33 f[i]=f[q[l]]+m+(sum[i]-sum[q[l]])*(sum[i]-sum[q[l]]);34 while(l<=r-1){35 j=q[r];k=q[r-1];36 if(fy(i,j)*fx(j,k)<=fy(j,k)*fx(i,j))r--;37 else break;38 }39 q[++r]=i;40 }41 printf("%lld\n",f[n]);42 }43 int main()44 {45 while(~scanf("%d%d",&n,&m))46 work();47 return 0;48 }

 

转载于:https://www.cnblogs.com/Yuzao/p/7237135.html

你可能感兴趣的文章
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
ethereum(以太坊)(二)--合约中属性和行为的访问权限
查看>>
IOS内存管理
查看>>
middle
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>