1911: [Apio2010]特别行动队
Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 3478 Solved: 1586
[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
HINT
Source
Solution
题意非常明显,将n个数划分成多段区间,使得总价值最大,每段区间的价值为$powersum=\sum power[i],ans=a*powersum^2+b*powersum+c$
那么得出DP转移方程:$dp[i]=max(dp[j]+a*(pos[i]-pos[j])^2+b*(pos[i]-pos[j])+c)$
那么很显然不能AC,那么考虑优化一下时间
考虑斜率优化,对于转移到当前位置,最优解为$i$,如果满足任意$i<j$都有$i$更优那么就可以得到如下:
$(dp[j]-dp[i]+a*(pos[j]^2-pos[i]^2)+b*(pos[i]-pos[j]))/(2*a*(pos[j]-pos[i]))$那么维护一下即可
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 1000100
int n,a,b,c; int po[maxn]; long long pos[maxn],dp[maxn];
int que[maxn],l,r;
long long pf(long long x){return x*x;}
double slope(int i,int j)
{
double fz=dp[j]-dp[i]+a*(pf(pos[j])-pf(pos[i]))+b*(pos[i]-pos[j]);
double fm=(*a*(pos[j]-pos[i]));
return fz/fm;
}
int main()
{
n=read(); a=read(),b=read(),c=read();
for (int i=; i<=n; i++) po[i]=read(),pos[i]=pos[i-]+po[i];
for (int tmp,i=; i<=n; i++)
{
while (l<r && slope(que[l],que[l+])<pos[i]) l++;
tmp=que[l];
dp[i]=dp[tmp]+a*pf(pos[i]-pos[tmp])+b*(pos[i]-pos[tmp])+c;
while (l<r && slope(que[r-],que[r])>slope(que[r],i)) r--;
que[++r]=i;
}
printf("%lld\n",dp[n]);
return ;
}
斜率优化好TAT..