首页 > 其他 > 详细

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

时间:2014-07-11 18:13:23      阅读:387      评论:0      收藏:0      [点我收藏+]

Print Article

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


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
bubuko.com,布布扣

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
 
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <set>
 6 using namespace std;
 7 int a[600000],sum[600000],h[600000],dp[600000],m;
 8 int getup(int j,int k)
 9 {
10     return dp[j]+sum[j]*sum[j]-dp[k]-sum[k]*sum[k];
11 }
12 int getdown(int j,int k)
13 {
14     return ((sum[j]-sum[k])<<1);
15 }
16 int getdp(int i,int j)
17 {
18     return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
19 }
20 int main()
21 {
22     int n,i,head,tail;
23     while(~scanf("%d%d",&n,&m))
24     {
25         memset(h,0,sizeof(h));
26         sum[0]=a[0]=0;
27         for(i=1; i<=n; i++)
28             scanf("%d",&a[i]),sum[i]=sum[i-1]+a[i];
29             head=tail=0;
30             h[tail++]=0;
31         for(i=1;i<=n;i++)
32         {
33             while(head+1<tail&&getup(h[head+1],h[head])<=sum[i]*getdown(h[head+1],h[head]))
34             head++;
35             dp[i]=getdp(i,h[head]);
36             while(head+1<tail&&getup(h[tail-1],h[tail-2])*getdown(i,h[tail-1])>=getup(i,h[tail-1])*getdown(h[tail-1],h[tail-2]))
37             tail--;
38             h[tail++]=i;
39         }
40         cout<<dp[i-1]<<endl;
41     }
42 }
View Code

 

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难,布布扣,bubuko.com

Print Article hdu 3507 一道斜率优化DP 表示是基础题,但对我来说很难

原文:http://www.cnblogs.com/ERKE/p/3833612.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!