1911: [Apio2010]特别行动队(斜率优化)
2024-09-08 07:33:17
思路
斜率优化dp。
代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath> using namespace std;
typedef long long LL;
int n,L,R;
LL A,B,C,s[],q[],f[]; inline int read() {
int x = ,f = ;char ch = getchar();
for (; !isdigit(ch); ch=getchar()) if(ch=='-') f=-;
for (; isdigit(ch); ch=getchar()) x = x*+ch-'';
return x * f;
}
double Y(int x) {
return double(f[x]+A*s[x]*s[x]-B*s[x]);
}
double slope(int i,int j) {
return (Y(i)-Y(j))/double(s[i]-s[j]);
}
int main() {
n = read(),A = read(),B = read(),C = read();
for (int i=; i<=n; ++i) s[i] = read(),s[i] += s[i-]; int L = ,R = ;
q[++R] = ;
for (int i=; i<=n; ++i) {
while (L<R && slope(q[L],q[L+])>2.0*A*s[i]) L++;
int j = q[L];
f[i] = f[j]+A*(s[i]-s[j])*(s[i]-s[j])+B*(s[i]-s[j])+C;
while (L<R && slope(q[R-],q[R])<slope(q[R],i)) R--;
q[++R] = i;
}
cout << f[n];
return ;
}
最新文章
- python迭代器实现斐波拉契求值
- ThinkPHP中疑难笔记
- 安卓普通类通过classloader访问资源文件
- PHP中的位运算与位移运算(其它语言通用)
- asp.net identity 2.2.0 中角色启用和基本使用(七)提示点
- 微信浏览器——User Agent
- 内存不足时Android 系统如何Kill进程
- 判断Http请求由手机端发起,还是有电脑端发起
- docker导入本地镜像
- (转)eclipse报错及解决说明 ";XX cannot be resolved to a type ";
- javascript之BOM编程Screen(屏幕)对象
- C/C++预处理指令#define,#ifdef,#ifndef,#endif… (转)
- Windows之常用命令
- 使用VMware 虚拟linux系统环境
- CSS效果:不怎么样的登录表单
- 如何解决selenium打开chrome提示chromedriver.exe已停止工作
- 第 6 章 —— 依赖项注入(DI)容器 —— Ninject
- Java第三阶段学习(一、IO流------File类)
- 微信小程序开发7-JavaScript脚本
- Superset 初探