HDU 3507 Print Article(DP+斜率优化)

              

               Print Article

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

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

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

【思路】

斜率优化。

设f[i],则转移式为f[i]=min{f[j]+(C[i]-C[j])^2+M},1<=j<i

进一步得:f[i]=min{ (f[j]+C[j]^2-2*C[i]*C[j])+(C[i]^2+M) }

       设y(j)=f[j]+C[j]^2,a[i]=-*C[i],x(j)=C(j),则f[i]=min{y(j)+2*a[i]*x(j)}+C[i]^2+M

则要求min p=y+2ax , 单调队列维护下凸包。

【代码】

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5
 6 const int N = 500000+10;
 7
 8 struct point { int x,y;
 9 }q[N],now;
10 int L,R,n,m,C[N],f[N];
11 int cross(point a,point b,point c) {
12     return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
13 }
14 void read(int& x) {
15     char c=getchar(); while(!isdigit(c)) c=getchar();
16     x=0; while(isdigit(c)) x=x*10+c-‘0‘ , c=getchar();
17 }
18 int main() {
19     while(scanf("%d%d",&n,&m)==2) {
20         for(int i=1;i<=n;i++)
21             read(C[i]) , C[i]+=C[i-1];
22         L=R=0;
23         for(int i=1;i<=n;i++) {
24             while(L<R && q[L].y-2*C[i]*q[L].x>=q[L+1].y-2*C[i]*q[L+1].x) L++;
25             now.x=C[i];                                    //计算xi
26             now.y=q[L].y-2*C[i]*q[L].x+2*C[i]*C[i]+m;    //计算yi=f[i]+b[i]^2 = min p+a[i]^2+b[i]^2+M
27             while(L<R && cross(q[R-1],q[R],now)<=0) R--;
28             q[++R]=now;
29         }
30         printf("%d\n",q[R].y-C[n]*C[n]);
31     }
32     return 0;
33 }
时间: 2024-10-06 14:21:05

HDU 3507 Print Article(DP+斜率优化)的相关文章

HDU 3507 Print Article (斜率优化)

HDU 3507 Print Article (斜率优化) ACM 题目地址: HDU 3507 Print Article 题意: 给定一个长度为n的序列,和一个常数m,我们可以将序列分成随意段,每段的权值为sum(arr[i]) + C(x<=i<=y),求一种划分方法使得整个序列的权值最小 分析: from:亟隐's blog f[i]=min(f[k]+(sum(i)-sum(k))^2 )+m = f[k]+sum^2(i)+sum^2(k)-2*sum(i)*sum(k)+m. 也

HDU 3507 Print Article(斜率优化DP)

题目链接 题意 : 一篇文章有n个单词,如果每行打印k个单词,那这行的花费是,问你怎么安排能够得到最小花费,输出最小花费. 思路 : 一开始想的简单了以为是背包,后来才知道是斜率优化DP,然后看了网上的资料,看得还挺懂的,不过我觉得如果以后真遇到斜率DP,要推起来肯定不简单..... 网上资料1 网上资料2 1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 6 int q[500005],dp

HDU 3507 Print Article(斜率优化)

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15536    Accepted Submission(s): 4813 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antiqu

HDU 3507 Print Article(斜率DP优化)

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 t

HDU 3507 Print Article 斜率优化

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4810    Accepted Submission(s): 1451 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

HDU3507 Print Article(经典斜率优化dp)

一道很老的斜率优化dp 斜率优化看上去很难,其实是有技巧的 . 对于dp题目,如果你想优化他,一定要先列出朴素的表达式并观察性质 对于本题我们可以发现,如果要更新dp[i],我们就要从前面找到dp[j]+(s[i]-s[j])^2+m的最小值,其中s是前缀和 我们就可以猜测,一定有很多不可能转移的内容,我们应该如何删除它从而降低复杂度. 那么我们假设k<j,当i出现之后,k就不可能作为答案,那么这些k在i处满足的性质就是 dp[j]+(s[i]-s[j])^2+m<=dp[k]+(s[i]-s

斜率优化dp简讲 &amp;&amp; HDU 3507 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 t

DP(斜率优化):HDU 3507 Print Article

Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 8199    Accepted Submission(s): 2549 Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique

hdu3507 Print Article(斜率优化入门)(pascal)

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 t