玩具装箱
题目
首先这道题与 诗人小G 有很大的相同点。其实就是超级弱化版(弱化数据,弱化
考虑使用斜率优化,式子
#include<bits/stdc++.h>
using namespace std;
#define L(i,l,r) for(int i=l;i<=r;++i)
#define R(i,l,r) for(int i=r;i>=l;--i)
const int N=50010;
typedef long double ld;
int n,l,hh,tt,q[N];
ld sum[N],dp[N];
#define a(i) (i+sum[i])
#define b(i) (a(i)+l+1)
#define x(i) b(i)
#define y(i) (dp[i]+b(i)*b(i))
#define slope(i,j) ((y(i)-y(j))/(x(i)-x(j)))
int main(){
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
scanf("%d%d",&n,&l);
L(i, 1, n){
scanf("%Lf",sum+i);
sum[i]+=sum[i-1];
}
L(i, 1, n){
while(hh<tt&&slope(q[hh],q[hh+1])<2*a(i))++hh;
dp[i]=dp[q[hh]]+(a(i)-b(q[hh]))*(a(i)-b(q[hh]));
while(hh<tt&&slope(i,q[tt-1])<slope(q[tt-1],q[tt]))--tt;
q[++tt]=i;
}
printf("%lld",(long long)dp[n]);
return 0;
}
本文作者:wscqwq
本文链接:https://www.cnblogs.com/wscqwq/p/17323163.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步