码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3507 Print Article

时间:2014-08-16 21:02:01      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   

Print Article

Time Limit: 3000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 3507
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:斜率优化dp。。。
 
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 500010;
18 LL sum[maxn],dp[maxn];
19 int n,m,q[maxn],head,tail;
20 LL G(int j,int k){
21     LL a = dp[j]+sum[j]*sum[j];
22     LL b = dp[k]+sum[k]*sum[k];
23     return a-b;
24 }
25 LL S(int j,int k){
26     return (sum[j] - sum[k])*2;
27 }
28 int main() {
29     int i;
30     while(~scanf("%d %d",&n,&m)){
31         sum[0] = 0;
32         for(i = 1; i <= n; i++){
33             scanf("%I64d",sum+i);
34             sum[i] += sum[i-1];
35         }
36         q[0] = dp[0] = head = tail = 0;
37         for(i = 1; i <= n; i++){
38             while(head < tail && G(q[head+1],q[head]) <= sum[i]*S(q[head+1],q[head])) ++head;
39             dp[i] = dp[q[head]] + (sum[i] - sum[q[head]])*(sum[i] - sum[q[head]])+m;
40             while(head < tail && G(q[tail-1],q[tail])*S(q[tail],i) >= G(q[tail],i)*S(q[tail-1],q[tail])) --tail;
41             q[++tail] = i;
42         }
43         printf("%I64d\n",dp[n]);
44     }
45     return 0;
46 }
View Code

 

HDU 3507 Print Article,布布扣,bubuko.com

HDU 3507 Print Article

标签:style   blog   http   color   java   os   io   for   

原文地址:http://www.cnblogs.com/crackpotisback/p/3916845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!