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

OI 专栏收录该内容
324 篇文章 0 订阅

$let~sum[i]=\sum_{k=1}^ib[k]\\~~~~~ ~~~~bksum[i]=\sum_{k=1}^ib[k]k\\~~~~ f[i]=min(f[j]+\sum_{k=j+1}^ib[k](i-k))+a_i\\~~~~~~~~ =min(f[j]+\sum_{k=j+1}^i(b[k]i-b[k]k))+a_i\\~~~~~~~~ =min(f[j]-bksum[i]+bksum[j]+isum[i]-isum[j])+a_i\\~~~~~~~~ =min(f[j]+bksum[j]-isum[j])+a_i+isum[i]-bksum[i]\\~~~~~~~~$

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
收藏
• 打赏
• 扫一扫，分享海报

11-26 20
09-18 496
06-18 175
09-26 36
07-13 138
01-08 42
03-08 474
04-19 1632
03-24 683
12-22 702
11-28 1891
02-27 51
02-18 66
03-10 8040

iamzky

¥2 ¥4 ¥6 ¥10 ¥20

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