# BZOJ 1911: [Apio2010]特别行动队

$f[i]= f[j]+ Asum[i]^2 + Asum[j]^2 - 2Asum[i]sum[j] + Bsum[i] - Bsum[j] + C$

$2Asum[i]sum[j] + f[i]-Asum[i]^2-C = f[j]+Asum[j]^2-Bsum[j]$

$k=2Asum[i],x=sum[j],b=f[i]-Asum[i]^2,y=f[j]+Asum[j]^2-Bsum[j]$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ldb;
{
register int x=0,f=1; static char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
return x*f;
}
const int N=2e6+7;
int n,A,B,C;
ll sum[N],f[N];
inline ll X(int i) { return sum[i]; }
inline ll Y(int i) { return f[i]+A*sum[i]*sum[i]-B*sum[i]; }
inline ldb slope(int i,int j) { return (ldb)(Y(i)-Y(j))/(X(i)-X(j)); }
int Q[N],l=1,r=1;
int main()
{
for(int i=1;i<=n;i++)
{
while( l<r && A*2*sum[i]<=slope(Q[l],Q[l+1]) ) l++;
int j=Q[l];
f[i]=f[j]+A*(sum[i]-sum[j])*(sum[i]-sum[j])+B*(sum[i]-sum[j])+C;
while( l<r && slope(Q[r-1],Q[r])<=slope(Q[r-1],i) ) r--;
Q[++r]=i;
}
printf("%lld",f[n]);
return 0;
}

posted @ 2018-12-24 08:26  LLTYYC  阅读(136)  评论(0编辑  收藏  举报