打印文章 FZOJ 5190

传送门

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define N 1000100
#define jd (isdigit(c))
#define gg c=getchar()
#define inf 233333333333333
#define eps 1e-10
#define mod 1000000
#define ls k<<1
#define rs k<<1|1
#define root t[0].ch[1]
inline ll read() 
{
    ll f=0;bool x=1;char gg;
    for(;!jd;gg)if(c=='-')x=0;
    for(;jd;gg)f=(f<<1)+(f<<3)+(c&15);
    return x?f:-f;
}
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-');
        x=~x+1;
    }
    else if(!x)putchar('0');
    char s[25];
    int j=0;
    for(;x;x/=10)s[j++]=x%10;
    for(int i=j-1;~i;i--)putchar(s[i]+48);
    putchar('\n');
}
ll n,l,dp[N],s[N],q[N],p[N];
inline db cal(ll x,ll y)
{
    return (s[x]==s[y])?1e100:(p[y]-p[x])*1.0/(s[y]-s[x]);
}
int main()
{
    while(~scanf("%lld%lld",&n,&l))
    {
        for(int i=1;i<=n;i++)
        {
            ll c=read();
            s[i]=s[i-1]+c;
        }
        ll head=1,tail=0;
        q[++tail]=0;
        for(int i=1;i<=n;i++)
        {
            while(head<tail)
            {
                db tk=cal(q[head],q[head+1]);
                if(tk<2*s[i])head++;
                else break;
            }
            ll j=q[head];
            dp[i]=dp[j]+(s[i]-s[j])*(s[i]-s[j])+l;
            p[i]=dp[i]+s[i]*s[i];
            while(head<tail&&cal(q[tail],i)<cal(q[tail-1],q[tail]))tail--;
            q[++tail]=i;
        }
        write(dp[n]);
    }
    return 0;
}
打印文章

 

posted @ 2019-03-26 19:11  david_alwal  阅读(134)  评论(0编辑  收藏  举报