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;
}

上一篇:做题记录 Luogu P1641


下一篇:CF1606C Banknotes