【BZOJ】【P3437】【小P的牧场】【题解】【斜率优化】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3437

一图流


Code:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
typedef long long LL;
struct point{
	LL x,y;
	point(LL _x=0,LL _y=0){x=_x;y=_y;}
	LL operator*(point oth){return x*oth.y-y*oth.x;}
	LL operator^(point oth){return x*oth.x+y*oth.y;}
	point operator-(point oth){return point(x-oth.x,y-oth.y);}
};
LL f[maxn],bksum[maxn],sum[maxn];
int a[maxn],b[maxn],n;
struct CH{
	vector<point>ch;
	void push_back(point p){
		while(ch.size()>1&&(p-ch.back())*(ch.back()-ch[ch.size()-2])>=0)ch.pop_back();
		ch.push_back(p);
	}
	LL Qmin(point p){
		int l=0,r=ch.size()-1;
		LL ans=(1LL<<61);
		while(r-l>2){
			int mid1=l+(r-l)/3;
			int mid2=r-(r-l)/3;
			if((p^ch[mid1])<(p^ch[mid2]))
				r=mid2;
			else l=mid1;
		}for(int i=l;i<=r;i++)ans=min(ans,p^ch[i]);
		return ans;
	}
}C;
int getint(){
	int res=0;char c=getchar();
	while(!isdigit(c))c=getchar();
	while(isdigit(c))res=res*10+c-'0',c=getchar();
	return res;
}
int main(){
	n=getint();
	for(int i=1;i<=n;++i)a[i]=getint();
	for(int i=1;i<=n;++i)b[i]=getint();
	for(int i=1;i<=n;i++)sum[i]=sum[i-1]+b[i];
	for(int i=1;i<=n;i++)bksum[i]=bksum[i-1]+(LL)i*b[i];
	C.push_back(point(0,0));
	for(int i=1;i<=n;i++){
		f[i]=C.Qmin(point(-i,1))+a[i]+(LL)i*sum[i]-bksum[i];		
		C.push_back(point(sum[i],f[i]+bksum[i]));
	}cout<<f[n]<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值