hdu 3507 Print Article
Posted 日拱一卒 功不唐捐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 3507 Print Article相关的知识,希望对你有一定的参考价值。
http://acm.hdu.edu.cn/showproblem.php?pid=3507
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
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
斜率优化
把斜率转化为乘积的形式,避免浮点数误差
#include<cstdio> #include<cstring> #define N 500001 using namespace std; typedef long long LL; LL sum[N],dp[N]; int c,q[N],head,tail; long long up(int k,int j) { return dp[j]-dp[k]-sum[k]*sum[k]+sum[j]*sum[j]; } long long down(int k,int j) { return 2*(sum[j]-sum[k]); } int main() { int n,m,x; while(scanf("%d%d",&n,&m)!=EOF) { memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++) { scanf("%d",&x); sum[i]=sum[i-1]+x; } head=tail=0; memset(q,0,sizeof(q)); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { while(head<tail && up(q[head],q[head+1])<=sum[i]*down(q[head],q[head+1])) head++; dp[i]=dp[q[head]]+m+(sum[i]-sum[q[head]])*(sum[i]-sum[q[head]]); while(head<tail && up(q[tail-1],q[tail])*down(q[tail],i)>=up(q[tail],i)*down(q[tail-1],q[tail])) tail--; q[++tail]=i; } printf("%lld\n",dp[n]); } }
以上是关于hdu 3507 Print Article的主要内容,如果未能解决你的问题,请参考以下文章