其他分享
首页 > 其他分享> > P3195 [HNOI2008]玩具装箱

P3195 [HNOI2008]玩具装箱

作者:互联网

#include<bits/stdc++.h>
#define N 50000+10
#define ll long long
#define go(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
ll s[N],f[N],L;
int queuee[N];

inline double X(int x){return s[x];}
inline double Y(int y){return f[y]+(s[y]+L)*(s[y]+L);}
inline double rate(int a,int b){
    double dx=X(a)-X(b);
    if(!dx)return LONG_MAX;
    return (Y(a)-Y(b))/dx;



}


int main(){

int n;

scanf("%d%lld",&n,&L);
++L;
go(i,1,n){
scanf("%lld",&s[i]);
s[i]+=s[i-1];
}

int head=1,tail=1;
queuee[1]=0;


go(i,1,n){
s[i]+=i;
}

go(i,1,n){
while(head<tail&&rate(queuee[head],queuee[head+1])<=2*(double)s[i])
    ++head;

int j=queuee[head];
f[i]=f[j]+(s[i]-s[j]-L)*(s[i]-s[j]-L);

while(head<tail&&rate(queuee[tail-1],queuee[tail])>rate(queuee[tail],i) )
    --tail;
queuee[++tail]=i;

}

printf("%lld",f[n]);
return 0;
}

标签:P3195,return,int,go,tail,HNOI2008,define,装箱,lld
来源: https://blog.csdn.net/m0_47879244/article/details/121152473