首页 > 其他 > 详细

[HDU3507]Print Article

时间:2017-01-21 18:40:42      阅读:421      评论:0      收藏:0      [点我收藏+]

[HDU3507]Print Article

试题描述

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.

输入

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.

输出

A single number, meaning the mininum cost to print the article.

输入示例

5 5
5
9
5
7
5

输出示例

230

数据规模及约定

见“输入

题解

题目描述需要做题人自行朝正确的方向想象,其含义是隐晦的,并不是字面意思。

如果我告诉你这是一道裸斜率优化的 dp,那么你也许能够很容易的猜出题目描述的意思。

最后提醒:Ci 有可能为 0,也就是可能出现重复的点,那么在维护凸壳时需要注意一些什么就不言自明了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;
#define LL long long

#define maxn 500010
struct Point {
	LL x, y;
	Point() {}
	Point(LL _, LL __): x(_), y(__) {}
} ps[maxn];
LL S[maxn], f[maxn];
int n, M, q[maxn], hd, tl;

LL sqr(LL x) { return x * x; }

bool isup(int a, int b, int c) {
	LL x1 = ps[b].x - ps[a].x, y1 = ps[b].y - ps[a].y, x2 = ps[c].x - ps[b].x, y2 = ps[c].y - ps[b].y;
	return y1 * x2 - x1 * y2 >= 0;
}

int main() {
	while(scanf("%d%d", &n, &M) == 2) {
		for(int i = 1; i <= n; i++) {
			int x; scanf("%d", &x);
			S[i] = S[i-1] + x;
		}
		
		hd = 1; tl = 0;
		q[++tl] = 0;
		for(int i = 1; i <= n; i++) {
			LL slop = S[i] << 1;
			while(hd < tl && slop * (ps[q[hd+1]].x - ps[q[hd]].x) > ps[q[hd+1]].y - ps[q[hd]].y) hd++;
			f[i] = ps[q[hd]].y - slop * ps[q[hd]].x + sqr(S[i]) + M;
			ps[i] = Point(S[i], f[i] + sqr(S[i]));
			while(hd < tl && isup(q[tl-1], q[tl], i)) tl--;
			q[++tl] = i;
		}
		printf("%lld\n", f[n]);
	}
	
	return 0;
}

 

[HDU3507]Print Article

原文:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/6337479.html

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