首页 > 其他 > 详细

bzoj1010: [HNOI2008]玩具装箱toy

时间:2017-11-07 16:56:43      阅读:248      评论:0      收藏:0      [点我收藏+]

复习斜率优化。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
LL s[51000],f[51000];
int list[51000];
double X(int j)
{
    return double(f[j]+s[j]*s[j]);
}
double Y(int j)
{
    return double(s[j]);
}
double slope(int j1,int j2)
{
    return (X(j1)-X(j2)) / (Y(j1)-Y(j2));
}
int main()
{
    LL n,L,x;
    scanf("%lld%lld",&n,&L);L++;
    s[0]=0LL;for(int i=1;i<=n;i++)
    {
        scanf("%lld",&x);
        s[i]=s[i-1]+x+1;
    }
    int head=1,tail=1;list[1]=0;
    for(int i=1;i<=n;i++)
    {
        while( head<tail && slope(list[head],list[head+1]) <= double(2*(s[i]-L)) )head++;
        int j=list[head];
        f[i]=f[j]+(s[i]-s[j]-L)*(s[i]-s[j]-L);
        while( head<tail && slope(list[tail-1],list[tail]) > slope(list[tail],i) )tail--;
        list[++tail]=i;
    }
    printf("%lld\n",f[n]);
    return 0;
}

 

bzoj1010: [HNOI2008]玩具装箱toy

原文:http://www.cnblogs.com/AKCqhzdy/p/7799486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!