hdu_3507_Print Article

来源:互联网 发布:软件测试技术 图片 编辑:程序博客网 时间:2024/06/05 08:50

Print Article

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 8558    Accepted Submission(s): 2664


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 559575
 

Sample Output
230
 

Author
Xnozero
 

Source
2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT
 

Recommend
zhengfeng

斜率dp入门题,第一次接触,学习一下,kuangbin大神写得很好,膜拜~~~~~通过队列来维护一个下凹的图形,斜率逐渐增大。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 500010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int n,m;int sum[maxn];int dp[maxn];int q[maxn];//queueint getDP(int i,int j){    return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);}int getUP(int j,int k){    return (dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k]);}int getDOWN(int j,int k){    return 2*(sum[j]-sum[k]);}int main(){    int i,j;    while((scanf("%d%d",&n,&m))==2)    {        int val;        sum[0]=dp[0]=0;        for(i=1;i<=n;i++)        {            Si(val);            sum[i]=sum[i-1]+val;        }        int head=0,tail=0;        q[tail++]=0;        for(i=1;i<=n;i++)        {            while(head+1<tail&&getUP(q[head+1],q[head])<=getDOWN(q[head+1],q[head])*sum[i])                head++;            dp[i]=getDP(i,q[head]);            while(head+1<tail&&getUP(i,q[tail-1])*getDOWN(q[tail-1],q[tail-2])<=               getUP(q[tail-1],q[tail-2])*getDOWN(i,q[tail-1]))                tail--;            q[tail++]=i;        }        Pi(dp[n]);    }    return 0;}


0 0
原创粉丝点击