Bzoj1911 [Apio2010]特别行动队

1911: [Apio2010]特别行动队

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 3969  Solved: 1873

Description

Input

Output

Sample Input

4
-1 10 -20
2 2 3 4

Sample Output

9

HINT

Source

斜率优化DP

f[分组末尾]=最优解

f[i] = max{f[j]+A(s[i]-s[j])^2+B(s[i]-s[j])+C}  (0<=j<i) (s[]是战斗力前缀和)

对于任意两个决策点j>k,假设j比k更优,则有:
f[j]+A(s[i]-s[j])^2+B(s[i]-s[j])+C >= f[k]+A(s[i]-s[k])^2+B(s[i]-s[k])+C

设G(j)=f[j]+A*s[j]^2-B*s[j] ,H(j)=-2*A*s[j]

由于H(j)>H(k),得到[G(j)-G(k)]/[H(j)-H(k)]>=-s[i]

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #define LL long long
 8 using namespace std;
 9 const int mxn=1e6+10;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
13     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
14     return x*f;
15 }
16 LL f[mxn];
17 int n;
18 int a,b,c;
19 int x[mxn];
20 LL s[mxn];
21 LL gdp(int i,int x){
22     return f[x]+a*(s[i]-s[x])*(s[i]-s[x])+b*(s[i]-s[x])+c;
23 }
24 LL gup(int x,int y){
25     return f[x]-f[y]+a*(s[x]*s[x]-s[y]*s[y])-b*(s[x]-s[y]);
26 }
27 LL gdown(int x,int y){
28     return -2*a*s[x]+2*a*s[y];
29 }
30 int hd=0,tl=0;
31 int q[mxn];
32 int main(){
33     n=read();
34     a=read();b=read();c=read();
35     int i,j;
36     for(i=1;i<=n;i++){
37         x[i]=read();
38         s[i]=s[i-1]+x[i];
39     }
40     for(i=1;i<=n;i++){
41         while(hd<tl && gup(q[hd],q[hd+1])<=-s[i]*gdown(q[hd],q[hd+1]))hd++;
42         f[i]=gdp(i,q[hd]);
43         while(hd<tl && gup(q[tl],i)*gdown(q[tl-1],q[tl])>=gup(q[tl-1],q[tl])*gdown(q[tl],i))tl--;
44         q[++tl]=i;
45     }
46     printf("%lld\n",f[n]);
47     return 0;
48 }
时间: 2024-08-05 19:32:46

Bzoj1911 [Apio2010]特别行动队的相关文章

bzoj1911[Apio2010]特别行动队 斜率优化dp

1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 5057  Solved: 2492[Submit][Status][Discuss] Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT dp[i]=dp[j]+a*x*x+b*x+cx=sum[i]-sum[j] 证明单调性假设对于i点 k<j且j的决策

BZOJ1911 [Apio2010]特别行动队 - 动态规划 - 斜率优化

欢迎访问~原文出处--博客园-zhouzhendong&AK 去博客园看该题解 题目传送门 题意概括 把一个整数序列划分成任意连续的段,使得划分出来的每一段的价值和最大. 对于某一段,价值的计算公式为 V=ax^2+bx+c,其中 x 为当前段的数值和. 题解 这题是博主大蒟蒻的第一道斜率优化DP题-- C++:while (1) 懵逼++; Pascal:while (true) do inc(懵逼); 本题首先一看就是 DP 题. 但是一看 1<=n<=1000000,-5<

[bzoj1911][Apio2010特别行动队]

Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT Solution 斜率优化动态规划 首先易得出这样的一个朴素状态转移方程 f[i]=max{f[j]+cal(sum[i]-sum[j])} 其中j<i,且cal(x)=a*x*x+b*x+c 那么设转移方程中的式子为V 若i<j,且V(j)>V(i) 那么,f[j]-f[i]+a*sum[j]^2-a*sum[i]^2+b*(su

bzoj1911 [Apio2010]特别行动队commando

题目链接 斜率优化 1 #include<cstdio> 2 #include<cstdlib> 3 #include<string> 4 #include<cstring> 5 #include<cmath> 6 #define re(i,l,r) for(int i=(l);i<=(r);i++) 7 using namespace std; 8 typedef long long LL; 9 template<typename

【BZOJ1911】[Apio2010]特别行动队 斜率优化DP

想了好久啊.... 用了我感觉比较好写的一种(因为没写过维护凸包),另一种是维护凸包的做法,本质一样?推荐http://www.mamicode.com/info-detail-345781.html. 网上的大多数解法: DP:f[i]=max(f[j]+a*(sum[i]-sum[j])^2+b(sum[i]-sum[j])+c) 显然复杂度不对. 那么假设j>k且f[j]优于f[k] f[j]-f[k]+a*(sum[j]^2-sum[k]^2)-b*(sum[j]-sum[k])>2*

APIO2010特别行动队(单调队列、斜率优化)

其实这题一看知道应该是DP,再一看数据范围肯定就是单调队列了. 不过我还不太懂神马单调队列.斜率优化-- 附上天牛的题解:http://www.cnblogs.com/neverforget/archive/2012/04/19/2456483.html 1 var f,g:array[0..1000050] of int64; 2 s,q:array[0..1000050] of longint; 3 a,b,c,n,i,h,t,x:longint; 4 bestk:double; 5 pro

BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Status][Discuss] Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT f[i]=max{f[j]+...} 随便一化就好了 (a*(s[k]*s[k]-s[j]*s[j])+f[k]-f[

【BZOJ-1911】特别行动队 DP + 斜率优化

1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 3478  Solved: 1586[Submit][Status][Discuss] Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT Source Solution 题意非常明显,将n个数划分成多段区间,使得总价值最大,每段区间的价值为$powersu

[Apio2010]特别行动队

1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Output Sample Input 4 -1 10 -20 2 2 3 4 Sample Output 9 HINT 斜率优化的式子搞出来之后,a是负的,所以要么不除过去,要么除过去变号 #include<cstdio> #include<iostream> #includ