有点无语,这道题我维护的差分是从后往前的,结果再推斜率的时候忘了分母是负数,乘过去后不等式变方向,我擦,坑了我一个半小时。。。。。啊啊啊啊啊
思路的话,维护一个差分s[i]=∑b[i]*i(i<=n) sum[i]=∑b[i]
f[i]=min{f[j]+s[j+1]-s[i]-(sum[j+1]-sum[i])*(n-i+1)}
化简的时候注意变方向
(f[k]+s[k+1]-s[j]-s[j-1])/(sum[k+1]-sum[j+1])>=n-i+1
好了
#include<cstdio>
#include<cstring>
#include<iostream>
#define LL long long
#define maxn 1000020
using namespace std;
int n,q[maxn*2];
LL a[maxn],b[maxn],f[maxn],s[maxn],sum[maxn];
double Q(int x,int y){
double xx=(double)(f[x]+s[x+1]-f[y]-s[y+1])/(sum[x+1]-sum[y+1]);
return (double)(f[x]+s[x+1]-f[y]-s[y+1])/(sum[x+1]-sum[y+1]);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld",a+i);
for(int i=1;i<=n;i++)scanf("%lld",b+i);
for(int i=n;i>=0;i--){
sum[i]=sum[i+1]+b[i];
s[i]=s[i+1]+b[i]*(n-i+1);
}
int l=1,r=1;
for(int t,i=1;i<=n;i++){
while(l<r&&Q(q[l+1],q[l])>=1.0*(n-i+1))l++;
t=q[l];
f[i]=f[t]+s[t+1]-s[i]-(sum[t+1]-sum[i])*(n-i+1)+a[i];
while(l<r&&Q(q[r-1],i)>=Q(q[r-1],q[r]))r--;
q[++r]=i;
}
printf("%lld",f[n]);
return 0;
}