1911: [Apio2010]特别行动队
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 4048 Solved: 1913
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
4
-1 10 -20
2 2 3 4
-1 10 -20
2 2 3 4
Sample Output
9
【题解】
首先很容易想到用前缀和,下面的sum表示前缀和。
然后写出状态转移方程:f[i]=max{f[j]+a(sum[i]-sum[j])^2+b(sum[i]-sum[j])+c}
假设j比k更优,得到斜率表达式(f[j]+a*sum[j]^2-b*sum[j])-(f[k]+a*sum[k]^2-b*sum[k])/(sum[j]-sum[k])>2a*sum[i]
然后斜率优化走起。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
#define MAXN 1000100
#define FILE "read"
#define up(i,j,n) for(ll i=j;i<=n;i++)
ll n,a,b,c,l,r,x[MAXN],sum[MAXN],q[MAXN],f[MAXN];
namespace INIT{
char buf[<<],*fs,*ft;
inline char getc() {return (fs==ft&&(ft=(fs=buf)+fread(buf,,<<,stdin),fs==ft))?:*fs++;}
inline ll read() {
ll x=,f=; char ch=getc();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getc();}
while(isdigit(ch)) {x=x*+ch-''; ch=getc();}
return x*f;
}
}using namespace INIT;
inline double slop(ll j,ll k) {return (double)((f[j]+a*sum[j]*sum[j]-b*sum[j])-(f[k]+a*sum[k]*sum[k]-b*sum[k]))/(double)(sum[j]-sum[k]);}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(); a=read(); b=read(); c=read();
up(i,,n) x[i]=read(),sum[i]=sum[i-]+x[i];
up(i,,n){
while(l<r&&slop(q[l],q[l+])>*a*sum[i]) l++;
ll t=q[l];
f[i]=f[t]+a*(sum[i]-sum[t])*(sum[i]-sum[t])+b*(sum[i]-sum[t])+c;
while(l<r&&slop(q[r],i)>slop(q[r-],q[r])) r--;
q[++r]=i;
}
printf("%lld\n",f[n]);
return ;
}