Fork me on GitHub

「APIO2010」特别行动队-斜率优化dp

Links there:APIO2010-特别行动队

题意

看链接去 懒得搬运

还是照例先推一个最弱的$O(n^3)$的转移.

令$f[i]$表示取到前$i$个士兵时的最大战斗力,考虑枚举$[j+1,i]$区间新成一队.

这样的转移可以再优化前缀和得到$O(n^2)$的转移.

考虑斜率优化.

假设存在$j > k$,$j$的转移比$k$优.(这里的$x[i]$已经是前缀和啦)

移项。

然后就可以斜率优化啦.

然后维护一个下凸包就可以啦.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//my vegetable has exploded. :(
#include<bits/stdc++.h>
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MM(x,y) memset(x,y,sizeof(x))
#define MCPY(a,b) memcpy(a,b,sizeof(b))
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
#define fi first
#define se second
using namespace std;
#define int long long

inline int quickpow(int m,int n,int p){int b=1;while(n){if(n&1)b=b*m%p;n=n>>1;m=m*m%p;}return b;}
inline int getinv(int x,int p){return quickpow(x,p-2,p);}
inline int read(void){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){f=ch=='-'?-1:1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x * f;
}
const int MAXN = 1e6+100;
int sum[MAXN],a,b,c,x[MAXN],n;
int f[MAXN],q[MAXN];
inline int calc(int x){return x*x*a+b*x+c;}
inline int UP(int x,int y){return ((f[x]+a*sum[x]*sum[x]-b*sum[x])-(f[y]+a*sum[y]*sum[y]-b*sum[y]));}
inline int DOWN(int x,int y){return 2LL*a*(sum[x]-sum[y]);}
///------------------head------------------

signed main(signed argc, char *argv[]){
n=read();
a=read(),b=read(),c=read();
rep(i,1,n) sum[i] = sum[i-1] + read();
int l = 0,r = 0;
rep(i,1,n){
while(l < r && UP(q[l],q[l+1]) <= sum[i] * DOWN(q[l],q[l+1])) ++l;
int j = q[l];
f[i] = f[j] + calc(sum[i]-sum[j]);
while(l < r && UP(q[r-1],q[r]) * DOWN(q[r],i) >= UP(q[r],i) * DOWN(q[r-1],q[r])) --r;
q[++r] = i;
}
//rep(i,1,n) printf("%lld ",f[i]);
printf("%lld\n",f[n]);
return 0;
}