题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1911
题意:给定一个数列,将其分成若干段,若某段的和为x则这段的价值为a*x*x+b*x+c。求一种分法使得总价值最大?
思路:设p(x)=a*x*x+b*x+c,s[i]表示前i项和,f[i]=max(f[j]+p(s[]-s[j])),那么对于两个j1,j2,若j1<j2,j2优于j1,令dy(j1,j2)=(f[j1]+a*sqr(s[j1])-b*s[j1])-(f[j2]+a*sqr(s[j2])-b*s[j2]),dx(j1,j2)=2*a*(s[j1]-s[j2]),则有:s[i]>=dy(j1,j2)/dx(j1,j2)。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-9
#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)
#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n) while(scanf("%d",&n)!=-1)
using namespace std;
void RD(int &x){scanf("%d",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(int &x,int &y,int &z,int &t){scanf("%d%d%d%d",&x,&y,&z,&t);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}
void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(int x,int y,int z) {printf("%d %d %d\n",x,y,z);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(double x) {printf("%.5lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}
const int INF=1000000000;
const int N=1000005;
int n,m,Q[N],head,tail;
i64 s[N],dp[N],a,b,c;
i64 dy(int j1,int j2)
{
return (dp[j1]+a*sqr(s[j1])-b*s[j1])-(dp[j2]+a*sqr(s[j2])-b*s[j2]);
}
i64 dx(int j1,int j2)
{
return 2*a*(s[j1]-s[j2]);
}
i64 p(i64 x)
{
return a*x*x+b*x+c;
}
void DP()
{
int i,j,x,y,z;
head=tail=0;
FOR1(i,n)
{
while(head<tail&&1.0*dy(Q[head],Q[head+1])/dx(Q[head],Q[head+1])<=s[i]) head++;
j=Q[head];
dp[i]=dp[j]+p(s[i]-s[j]);
z=i;
while(head<tail)
{
x=Q[tail-1];
y=Q[tail];
if(1.0*dy(x,y)/dx(x,y)>=1.0*dy(y,z)/dx(y,z)) tail--;
else break;
}
Q[++tail]=z;
}
PR(dp[n]);
}
int main()
{
Rush(n)
{
RD(a,b,c);
int i;
FOR1(i,n) RD(s[i]),s[i]+=s[i-1];
DP();
}
return 0;
}