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,
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

  学到了,这样的斜率优化。

  

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int maxn=500010;
 5 long long f[maxn],s[maxn];
 6 int q[maxn];
 7 int main()
 8 {
 9     int n,m,front,back;
10     while(~scanf("%d%d",&n,&m))
11     {
12         s[0]=f[0]=0;
13         for(int i=1;i<=n;i++)scanf("%d",&s[i]);
14         for(int i=2;i<=n;i++)s[i]+=s[i-1];
15
16         front=1;back=2;
17         q[front]=0;
18
19         for(int i=1;i<=n;i++)
20         {
21             while(front<back-1&&(f[q[front+1]]+s[q[front+1]]*s[q[front+1]])-(f[q[front]]+s[q[front]]*s[q[front]])<=2*s[i]*(s[q[front+1]]-s[q[front]])) front++;
22
23             f[i]=f[q[front]]+(s[i]-s[q[front]])*(s[i]-s[q[front]])+m;
24
25             while(front<back-1&&(s[q[back-1]]-s[q[back-2]])*((f[i]+s[i]*s[i])-(f[q[back-1]]+s[q[back-1]]*s[q[back-1]]))<=(s[i]-s[q[back-1]])*((f[q[back-1]]+s[q[back-1]]*s[q[back-1]])-(f[q[back-2]]+s[q[back-2]]*s[q[back-2]])))back--;
26             q[back++]=i;
27         }
28         printf("%lld\n",f[n]);
29     }
30     return 0;
31 }
时间: 2024-10-13 20:51:40

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

斜率优化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

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

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优化)

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(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 antiq

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3507 设 f[i],则 f[i] = f[j] + (s[i]-s[j])*(s[i]-s[j]) + m 即 f[j] + s[j]*s[j] = 2*s[i]*s[j] + f[i] - s[i]*s[i] - m 于是维护下凸包即可: 写成 double 的 slp 总是不对,把分母乘到对面就对了... 代码如下: #include<iostream> #include<cstdio>

HDU 3507 Print Article (斜率DP)

题意:要输出N个数字a[N],输出的时候可以连续连续的输出,每连续输出一串,它的费用是 "这串数字和的平方加上一个常数M". 析:这个题很容易想到DP方程dp[i] = min{dp[j] + M + (sum[i]-sum[j])^2},但是很明显是O(n^2),TLE是必然的,所以要进行优化. 假设 i > j > k ,并且 j 要比 k 好,那么就是 dp[j] + M + (sum[i]-sum[j])^2 < dp[k] + M + (sum[i]-sum