码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1911 特别行动队(斜率优化DP)

时间:2017-04-03 17:10:45      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:bsp   stream   iostream   vector   ...   mem   math   img   getchar   

应该可以看出这是个很normal的斜率优化式子。推出公式搞一搞即可。

 

技术分享
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-9
# define MOD 10000
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())==-) flag=1;
    else if(ch>=0&&ch<=9) res=ch-0;
    while((ch=getchar())>=0&&ch<=9)  res=res*10+(ch-0);
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar(-); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+0);
}
const int N=1000005;
//Code begin...

int a[N], que[N], head, tail, A, B, C;
LL dp[N], sum[N];

bool check(int x, int y, int z){
    return dp[x]-dp[y]+A*(sum[x]*sum[x]-sum[y]*sum[y])>=(2*A*sum[z]+B)*(sum[x]-sum[y]);
}
bool sol(int x, int y, int z){
    return (dp[x]-dp[y]+A*(sum[x]*sum[x]-sum[y]*sum[y]))*(sum[y]-sum[z])>=(dp[y]-dp[z]+A*(sum[y]*sum[y]-sum[z]*sum[z]))*(sum[x]-sum[y]);
}
int main ()
{
    int n;
    scanf("%d%d%d%d",&n,&A,&B,&C);
    FOR(i,1,n) scanf("%d",a+i), sum[i]=sum[i-1]+a[i];
    head=-1; tail=0; que[++head]=0;
    FOR(i,1,n) {
        while (head>tail&&check(que[tail+1],que[tail],i)) ++tail;
        int v=que[tail];
        dp[i]=dp[v]+A*(sum[i]-sum[v])*(sum[i]-sum[v])+B*(sum[i]-sum[v])+C;
        while (head>tail&&sol(i,que[head],que[head-1])) --head;
        que[++head]=i;
    }
    printf("%lld\n",dp[n]);
    return 0;
}
View Code

 

BZOJ 1911 特别行动队(斜率优化DP)

标签:bsp   stream   iostream   vector   ...   mem   math   img   getchar   

原文地址:http://www.cnblogs.com/lishiyao/p/6662178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!