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

【BZOJ 1096】【ZJOI 2007】仓库建设 DP+斜率优化

时间:2016-04-06 23:00:14      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

后缀自动机看不懂啊QAQ

放弃了还是看点更有用的东西吧,比如斜率优化DP

先水一道

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1000003
#define read(x) x=getint()
using namespace std;
typedef long long LL;
inline LL getint() {
    LL k = 0; int fh = 1; char c = getchar();
    for(; c < ‘0‘ || c > ‘9‘; c = getchar())
        if (c == ‘-‘) fh = -1;
    for(; c >= ‘0‘ && c <= ‘9‘; c = getchar())
        k = k * 10 + c - ‘0‘;
    return k * fh;
}
LL x[N], p[N], c[N], sum[N], b[N], f[N], n, q[N];
inline LL fz(int x, int y) {
	return f[x] + b[x] - f[y] - b[y];
}
inline LL fm(int x, int y) {
	return sum[x] - sum[y];
}
int main() {
	read(n);
	for(int i = 1; i <= n; ++i)
		read(x[i]), read(p[i]), read(c[i]);
	for(int i = 1; i <= n; ++i)
		sum[i] = sum[i - 1] + p[i], b[i] = b[i - 1] + p[i] * x[i];
	int h = 0, t = 1, now;
	for(int i = 1; i <= n; ++i) {
		while (h < t - 1 && fz(q[h + 1], q[h]) < x[i] * fm(q[h + 1], q[h]))
			++h;
		now = q[h];
		f[i] = f[now] + (sum[i] - sum[now]) * x[i] - b[i] + b[now] + c[i];
		while (h < t - 1 && fz(q[t - 1], q[t - 2]) * fm(i, q[t - 1]) > fz(i, q[t - 1]) * fm(q[t - 1], q[t - 2]))
			--t;
		q[t++] = i;
	}
	printf("%lld\n", f[n]);
	return 0;
}

我好蒟蒻啊,省选要爆零!

【BZOJ 1096】【ZJOI 2007】仓库建设 DP+斜率优化

标签:

原文地址:http://www.cnblogs.com/abclzr/p/5361330.html

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