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

模板题,不用多说,只不过本地测,cin 2.6s,scanf 0.6s过。确实数据大了还是少用cin.

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#define MAXN 1000000
using namespace std;
#define gx(i) (sum[i])
#define gy(i) (d[i] + sumt[i])
typedef long long LL;//!!!!
int a[MAXN +10];
int b[MAXN +10];
int que[MAXN +10];
LL sumt[MAXN +10];//t[i] = b[i] * i;
LL sum[MAXN +10];
LL d[MAXN +10];
LL get_d(LL i,LL j)
{
	return d[j] + i * (sum[i] - sum[j]) - (sumt[i] - sumt[j]) + a[i];
}
int main()
{
	//freopen("farm.in","r",stdin);
	//freopen("farm.out","w",stdout);
	LL i,f,r,x1,x2,y1,y2,n;
	cin >> n;
	for(i = 1;i <= n;i++)
		scanf("%d",&a[i]);
	for(i = 1;i <= n;i++)
		scanf("%d",&b[i]);
	for(i = 1;i <= n;i++)
	{
		sumt[i] = sumt[i - 1] + b[i] * i;
		sum[i] = sum[i - 1] + b[i];
	}
	f = r = que[0] = 0;
	for(i = 1;i <= n;i++)
	{
		LL tmp = i - 1;
		while(f < r)
		{
			x1 = gx(que[r]) - gx(que[r-1]);
			y1 = gy(que[r]) - gy(que[r-1]);
			x2 = gx(tmp) - gx(que[r]);
			y2 = gy(tmp) - gy(que[r]);
			if(y1 * x2 >= y2 * x1)
				r--;
			else break;
		}
		que[++r] = tmp;
		while(f < r && get_d(i,que[f]) > get_d(i,que[f + 1]))
			f++;
		d[i] = get_d(i,que[f]);
	}
	cout << d[n] << endl;
	//printf("%d\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、付费专栏及课程。

余额充值