Print Article[HDU 3507,2010 ACM-ICPC Multi-University Training Contest]
2016-07-01 11:37
423 查看
题目地址请点击
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.
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.
5
9
5
7
5
fi=mini−1j=0{fj+(∑k=j+1iCk)2+M}
推出斜率公式,A(i)⋅[2A(j)−2A(k)]>fj−fk+A2(j)−A2(k)
其中,A(x)=∑i=1xCi
Print Article
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 55
9
5
7
5
Sample Output
230Solution
设 fi 表示前 i 个字母打出来的最少费用。fi=mini−1j=0{fj+(∑k=j+1iCk)2+M}
推出斜率公式,A(i)⋅[2A(j)−2A(k)]>fj−fk+A2(j)−A2(k)
其中,A(x)=∑i=1xCi
Code
#include <iostream> #include <cstdio> #define LL long long #define Min(x,y) ((x)<(y)?(x):(y)) using namespace std; LL n,m,Max_Right,lastcost; LL c[500010],A[500010],f[500010],As[500010]; double ks[500010]; int main(){ while(scanf("%lld%lld",&n,&m)!=EOF){ A[0]=0; for(LL i=1;i<=n;i++){ scanf("%lld",&c[i]); A[i]=A[i-1]+c[i]; } lastcost=Max_Right=As[0]=f[0]=0; ks[Max_Right+1]=9223372036854775807LL;ks[Max_Right]=-9223372036854775807LL; for(LL i=1;i<=n;i++){ for(LL j=lastcost;j<=Max_Right;j++) if(ks[j]<=A[i]&&ks[j+1]>=A[i]){ lastcost=j; f[i]=f[j]+(A[i]-As[j])*(A[i]-As[j])+m; for(LL k=Max_Right;k>=0;k--) if((double)(f[i]+A[i]*A[i]-f[k]-As[k]*As[k])/(2*A[i]-2*As[k])>=ks[k]){ Max_Right=k+1;As[Max_Right]=A[i];f[Max_Right]=f[i]; ks[Max_Right]=(double)(f[i]+A[i]*A[i]-f[k]-As[k]*As[k])/(2*A[i]-2*As[k]); ks[Max_Right+1]=9223372036854775807LL; lastcost=Min(lastcost,Max_Right); break; } break; } } printf("%lld\n",f ); } return 0; }
相关文章推荐
- 浅谈main函数的栈帧
- Thread_wait、notify、notifyAll的使用方法
- OpenCV源码分析(一)级联分类器haartraining
- main
- 【转】saiku与kylin整合备忘录
- 【Debug Assertion Failed!Expression:_pFirstBlock ==
- cf #360 div2 D-Remainders Game(中国剩余定理)
- Wrong02-Could not find a storyboard named‘Main’in bundle NSBundle
- TurboMail为企业提供海量投递邮件群发系统
- trait的学习
- DROP_SNAPSHOT_RANGE过程不能清理表RM$_SNAPSHOT_DETAILS
- http://blog.csdn.net/zxl315/article/details/10830105
- cookie无法设置除当前域名或者其父域名之外的其他domain.
- http://blog.csdn.net/congcong68/article/details/39256307
- baike并行计算概念
- baike并行计算概念
- Main event loop
- http://m.blog.csdn.net/article/details?id=2630620
- [源码解读]position_estimator_inav_main解读(如何启动光流)
- SLA by Short brain-June-2016