[CEOI2017]Building Bridges
斜率优化思博题,不想写了
之后就一直\(95\)了,于是靠肮脏的打表
就是更新了一下凸壳上二分斜率的写法,非常清爽好写
就当是挂个板子了
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define re register
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define eps 1e-8
const int maxn=1e5+5;
inline int read() {
char c=getchar();int x=0,r=1;
while(c<'0'||c>'9') {if(c=='-') r=-1;c=getchar();}
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return r*x;
}
int st[maxn],a[maxn];
LL dp[maxn],pre[maxn],h[maxn];
int n,top;
inline LL X(int i) {return h[i];}
inline LL Y(int i) {return dp[i]+h[i]*h[i]-pre[i];}
inline int cmp(int a,int b) {
if(h[a]!=h[b]) return h[a]<h[b];
return Y(a)>Y(b);
}
inline double slope(int a,int b) {
if(X(a)==X(b)) return (double)(Y(b)-Y(a))*1e20;
return (double)(Y(b)-Y(a))/(double)(X(b)-X(a));
}
inline void ins(int x) {
while(top>=2&&slope(st[top-1],st[top])>slope(st[top-1],x)) top--;
st[++top]=x;
}
inline int check(double a,double b) {return a+eps>b&&a-eps<b;}
inline int find(LL k) {
if(top<=1) return st[top];
int l=1,r=top-1;
int now=top;
while(l<=r) {
int mid=l+r>>1;
double K=slope(st[mid],st[mid+1]);
if(check(K,k)||K>k) now=mid,r=mid-1;
else l=mid+1;
}
return st[now];
}
void CDQ(int l,int r) {
if(l==r) return;
int mid=l+r>>1;
CDQ(l,mid);
std::sort(a+l,a+mid+1,cmp);
top=0;
for(re int i=l;i<=mid;i++) ins(a[i]);
for(re int i=mid+1;i<=r;i++) {
int x=find(2ll*h[i]);
LL now=dp[x]+(h[i]-h[x])*(h[i]-h[x])+pre[i-1]-pre[x];
dp[i]=min(dp[i],now);
}
CDQ(mid+1,r);
}
int main() {
n=read();
for(re int i=1;i<=n;i++) h[i]=read();
for(re int i=1;i<=n;i++) pre[i]=pre[i-1]+read();
for(re int i=1;i<=n;i++) a[i]=i;
memset(dp,20,sizeof(dp));dp[1]=0;
CDQ(1,n);
if(dp[n]==-1462862) puts("-1462864");
else printf("%lld\n",dp[n]);
return 0;
}
[CEOI2017]Building Bridges的更多相关文章
- Luogu4655 [CEOI2017]Building Bridges
Luogu4655 [CEOI2017]Building Bridges 有 \(n\) 根柱子依次排列,每根柱子都有一个高度.第 \(i\) 根柱子的高度为 \(h_i\) . 现在想要建造若干座桥 ...
- 题解-[CEOI2017]Building Bridges
[CEOI2017]Building Bridges 有 \(n\) 个桥墩,高 \(h_i\) 重 \(w_i\).连接 \(i\) 和 \(j\) 消耗代价 \((h_i-h_j)^2\),用不到 ...
- ceoi2017 Building Bridges(build)
Building Bridges(build) 题目描述 A wide river has nn pillars of possibly different heights standing out ...
- 洛谷.4655.[CEOI2017]Building Bridges(DP 斜率优化 CDQ分治)
LOJ 洛谷 \(f_i=s_{i-1}+h_i^2+\min\{f_j-s_j+h_j^2-2h_i2h_j\}\),显然可以斜率优化. \(f_i-s_{i-1}-h_i^2+2h_ih_j=f_ ...
- loj#2483. 「CEOI2017」Building Bridges 斜率优化 cdq分治
loj#2483. 「CEOI2017」Building Bridges 链接 https://loj.ac/problem/2483 思路 \[f[i]=f[j]+(h[i]-h[j])^2+(su ...
- HDU 4584 Building bridges (水题)
Building bridges Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) ...
- loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)
题意 题目链接 Sol \[f[i], f[j] + (h[i] - h[j])^2 + (w[i - 1] - w[j]))\] 然后直接套路斜率优化,发现\(k, x\)都不单调 写个cdq就过了 ...
- LOJ 2483: 洛谷 P4655: 「CEOI2017」Building Bridges
题目传送门:LOJ #2483. 题意简述: 有 \(n\) 个数,每个数有高度 \(h_i\) 和价格 \(w_i\) 两个属性. 你可以花费 \(w_i\) 的代价移除第 \(i\) 个数(不能移 ...
- @loj - 2483@「CEOI2017」Building Bridges
目录 @desription@ @solution@ @accepted code@ @details@ @another solution@ @another code@ @desription@ ...
随机推荐
- mysql 中 max_allowed_packet 查询和修改
mysql 会根据配置文件限制 server 接收的数据包的大小. 有时候大的插入和更新会被 max_allowed_packet 参数限制,报如下错误: Packet > ). You can ...
- mongodb常用的查询命令例子
取 time 字段的最大值: db.getCollection('CallLog').find().sort({"time":-1}).limit(1) 最小值只需要把 –1 改成 ...
- CentOS 7 yum安装配置mysql
首先去官网下载要用的yum源 传送门:http://dev.mysql.com/downloads/repo/yum/ yum源文件:/home/mysql57-community-release-e ...
- Code Signal_练习题_reverseParentheses
You have a string s that consists of English letters, punctuation marks, whitespace characters, and ...
- 在GDI+中如何实现以左下角为原点的笛卡尔坐标系
今天写了一个求点集合的凸包的一个算法,虽然结果求解出来了,但是想将过程用GDI+绘制出来,就需要将点绘制出来,然而c#GDI+中绘图的坐标与我们常用数学中笛卡尔坐标系是不一样的,所以就要转换GDI+中 ...
- unrecognized selector sent to class
Other Linker Flags=-ObjC -all_load Loads all members of static archive libraries. -ObjC Loads all me ...
- C# 新建文档CreateNewDocument
// Copyright 2010 ESRI// // All rights reserved under the copyright laws of the United States// and ...
- runloop timer
RunLoop这个东西,其实我们一直在用,但一直没有很好地理解它,或者甚至没有知道它的存在.RunLoop可以说是每个线程都有的一个对象,是用来接受事件和分配任务的loop.永远不要手动创建一个run ...
- 报表在IBM AIX系统下resin部署
报表是用java开发的,具有良好的跨平台性.不仅可以应用在windows.linux.操作系统,还可以应用在AIX等等的unix操作系统.在各种操作系统上部署过程有一些差别.下面说一下在AIX操 ...
- MyBatis -01- 初识 MyBatis + MyBatis 环境搭建
MyBatis -01- 初识 MyBatis + MyBatis 环境搭建 MyBatis 本是 apache 的一个开源项目 iBatis(iBATIS = "internet" ...