Print Article

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
 
Recommend
zhengfeng
***************************************************************************************
讲解的很清楚,有图又推导,不过看了很长时间才看懂,关键求斜率推导式
***************************************************************************************
 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<cmath>
 6 #define  L  long long
 7 using namespace std;
 8 L dp[500005];
 9 L sum[500005];
10 int  q[500005];
11 int n,m,i,j;
12 void  init()
13 {
14     for(i=1;i<=n;i++)
15      cin>>sum[i];
16     for(i=2;i<=n;i++)
17       sum[i]+=sum[i-1];
18 
19 }
20 L getdp(int i,int j)//求dp
21     {
22         return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
23     }
24 L  getup(int j,int k)//分子
25  {
26      return   (dp[j]+sum[j]*sum[j])-(dp[k]+sum[k]*sum[k]);
27  }
28 L getdown(int j,int k)//分母
29   {
30       return  2*(sum[j]-sum[k]);
31   }
32 int main()
33 {
34    while(scanf("%d %d",&n,&m)!=EOF)
35     {
36         memset(dp,0,sizeof(dp));
37         memset(sum,0,sizeof(sum));
38         memset(q,0,sizeof(q));
39         init();
40       int head=0;
41     int tail=0;
42      q[0]=dp[0]=0;
43      tail++;
44      for(i=1;i<=n;i++)
45       {
46           while(head+1<tail&&getup(q[head+1],q[head])<=sum[i]*getdown(q[head+1],q[head]))
47             head++;
48             dp[i]=getdp(i,q[head]);
49           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]))//删去一定不满足条件的点
50             tail--;
51           q[tail++]=i;
52       }
53 
54   cout<<dp[n]<<endl;
55 }
56   return 0;
57 
58 
59 
60 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3251238.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值