luogu3195/bzoj1010 玩具装箱(斜率优化dp)
2024-08-29 18:17:41
推出来式子然后斜率优化水过去就完事了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#define inf 0x3f3f3f3f
#define LL long long int
using namespace std;
const int maxn=; inline LL rd(){
LL x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int N;
LL co[maxn],sm[maxn],f[maxn],L;
int q[maxn],h,t; inline LL pw2(LL x){return x*x;} inline bool judge1(int j1,int j2,int i){
return f[j1]+pw2(j1+sm[j1])-f[j2]-pw2(j2+sm[j2])<(*i+*sm[i]-*L-)*(j1+sm[j1]-j2-sm[j2]);
}
inline bool judge2(int j1,int j2,int j3){
return (f[j1]+pw2(j1+sm[j1])-f[j2]-pw2(j2+sm[j2]))*(j2+sm[j2]-j3-sm[j3])<
(f[j2]+pw2(j2+sm[j2])-f[j3]-pw2(j3+sm[j3]))*(j1+sm[j1]-j2-sm[j2]);
} int main(){
int i,j,k;
N=rd();L=rd();
for(i=;i<=N;i++)co[i]=rd(),sm[i]=sm[i-]+co[i];
h=t=;q[]=;
for(i=;i<=N;i++){
while(h<t&&!judge1(q[h],q[h+],i)) h++;
f[i]=f[q[h]]+pw2(i-q[h]-+sm[i]-sm[q[h]]-L);
while(h<t&&!judge2(q[t-],q[t],i)) t--;
q[++t]=i;
}printf("%lld\n",f[N]); return ;
}
最新文章
- Android—IMEI
- h5+mui
- 【emWin】例程六:设置颜色
- R包之间冲突带来的奇怪错误
- while语句(1)
- 04 DOM一窥
- 轻量级应用开发之(04)UIScrollView-1
- 转: 深入理解 AngularJS 的 Scope
- 扩展BaseAdapter实现不存储列表项的ListView
- Mybatis异常There is no getter for property named &#39;XXX&#39; in &#39;class com.xxx.xxx.UserAccountDTO
- python发送post请求
- MATLAB求解二重积分案例
- TCP的TIME_WAIT
- reveal破解
- Spring4.0之四:Meta Annotation(元注解)
- linux进程端口防火墙
- List转换为字符串并添加分隔符
- sbt打包Scala写的Spark程序,打包正常,提交运行时提示找不到对应的类
- C++系统时间及字符串转换参考资料
- bzoj2286: [Sdoi2011]消耗战 虚树