[BZOJ3437] 小P的牧场 && 斜率优化

46 篇文章 0 订阅

如果你搞了前面的斜率优化的题 那么这道题应该不算难  考的时候脑残推错了 说不出话来  BZOJ数据显示也是坑 明明100W 后面加个,0 关键是我开1000W会直接T 坑

#include<cstdio>
using namespace std;
typedef long long LL;
const int MAXN = 1000000;
LL d[MAXN+10], Q[MAXN+10], Cost[MAXN+10], F, R, S[MAXN+10], T[MAXN+10];
int n, m;
inline LL Slope_U(int i, int j)
{
	return d[i] + T[i] - (d[j] + T[j]);
}
inline LL Slope_D(int i, int j)
{
	return S[i] - S[j];
}
inline LL read()
{     
	LL x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
	return x*f;
}
int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) Cost[i] = read();
	for(int i = 1; i <= n; i++) {
		LL x = read();
		S[i] = S[i-1] + x;
		T[i] = T[i-1] + x * i;
	}
	F = R = Q[0] = 0;
	Q[R++] = 0;
	for(int i = 1; i <= n; i++)
	{
		while(F < R-1 && Slope_U(Q[F+1], Q[F]) <= Slope_D(Q[F+1], Q[F]) * i)
			F++;
		d[i] = d[Q[F]] + Cost[i] + i * (S[i] - S[Q[F]]) - (T[i] - T[Q[F]]); 
		while(F < R-1 && Slope_U(Q[R-1], Q[R-2]) * Slope_D(i, Q[R-1]) >= Slope_U(i, Q[R-1]) * Slope_D(Q[R-1], Q[R-2]))
			R--;
		Q[R++] = i;
	}
	printf("%lld\n", d[n]);
}

/*
4
2 4 2 4
3 1 4  2
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值