# BZOJ1911 特别行动队

### 题解

$f[j]+a*(sum[i]-sum[j])^2+b*(sum[i]-sum[j])+c 整理之后可得：$\frac{f[k]-f[j]+a*(sum[k]-sum[j])^2-b*(sum[k]-sum[j])}{2*a*(sum[k]-sum[j])}\leq sum[i]\$

### ｃｏｄｅ

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
const int maxn=2e6+500;
int n;
int a,b,c;
int x[maxn];
int l,r;
int que[maxn];
ll f[maxn],sum[maxn];
/*==================Define Area================*/
ll Sqr(ll x) {
return x*x;
}

double Cal(int x,int y) {
return (double)(f[y]-f[x]+a*(Sqr(sum[y])-Sqr(sum[x]))-b*(sum[y]-sum[x]))/(double)(2*a*(sum[y]-sum[x]));
}

int main() {
for(int i=1;i<=n;i++) {
}
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+x[i];
for(int i=1;i<=n;i++) {
while(l<r&&Cal(que[l],que[l+1])<sum[i]) l++;
int t=que[l];
f[i]=f[t]+a*Sqr(sum[i]-sum[t])+b*(sum[i]-sum[t])+c;
while(l<r&&Cal(que[r-1],que[r])>Cal(que[r],i)) r--;
que[++r]=i;
}
printf("%lld\n",f[n]);
return 0;
}

posted @ 2018-08-06 22:12  Apocrypha  阅读(103)  评论(0编辑  收藏  举报