ceoi2017 Building Bridges(build)
Building Bridges(build)
题目描述
A wide river has nn pillars of possibly different heights standing out of the water. They are arranged in a straight line from one bank to the other. We would like to build a bridge that uses the pillars as support. To achieve this we will select a subset of pillars and connect their tops into sections of a bridge. The subset has to include the first and the last pillar.
The cost of building a bridge section between pillars ii and jj is (hi−hj)2(hi−hj)2 as we want to avoid uneven sections, where hihi is the height of the pillar ii. Additionally, we will also have to remove all the pillars that are not part of the bridge, because they obstruct the river traffic. The cost of removing the i−thi−th pillar is equal to wiwi. This cost can even be negative—some interested parties are willing to pay you to get rid of certain pillars. All the heights hihi and costs wiwi are integers.
What is the minimum possible cost of building the bridge that connects the first and last pillar?
有 n 根柱子依次排列,每根柱子都有一个高度。第 i 根柱子的高度为 hi。
现在想要建造若干座桥,如果一座桥架在第 i 根柱子和第 j根柱子之间,那么需要 (hi−hj)^2 的代价。
在造桥前,所有用不到的柱子都会被拆除,因为他们会干扰造桥进程。第 i 根柱子被拆除的代价为 wi,注意 wi 不一定非负,因为可能政府希望拆除某些柱子。
现在政府想要知道,通过桥梁把第 1 根柱子和第 n 根柱子连接的最小代价。注意桥梁不能在端点以外的任何地方相交。
输入
The first line contains the number of pillars, nn.
The second line contains pillar heights hihi in the order, separated by a space.
The third line contains wiwi in the same order, the costs of removing pillars.
第一行一个正整数 n。
第二行 n 个空格隔开的整数,依次表示h1,h2,⋯,hnh1,h2,⋯,hn。
第三行 n 个空格隔开的整数,依次表示w1,w2,⋯,wnw1,w2,⋯,wn。
输出
Output the minimum cost for building the bridge. Note that it can be negative.
输出一行一个整数表示最小代价,注意最小代价不一定是正数。
样例输入
6
3 8 7 1 6 6
0 -1 9 1 2 0
样例输出
17
提示
Constraints
• 2 <= n <= 10^5
• 0 <= hi <= 10^6
• 0 <= |wi| <= 10^6
Subtask 1 (30 points)
• n <= 1, 000
Subtask 2 (30 points)
• optimal solution includes at most 2 additional pillars (besides the first and last) • |wi| <= 20
Subtask 3 (40 points)
• no additional constraints
来源
solution
把w前缀和起来
我们可以得到一个n^ 2 DP
把它写成斜率优化的形式
斜率不单调,x不单调。
不会splay,那就cdq分治。
把h排序,然后就是单调的了
构出凸包,斜率优化即可
吐槽:cdq细节真是多
注意算斜率时
有时是+inf (a.x==b.x&&a.y<b.y)
有时是-inf (a.x==b.x&&a.y>b.y)
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 100005
#define inf 1e18
using namespace std;
int n,num[22][maxn],top;
long long f[maxn];
struct node{
long long id,h,w;
}s[maxn],ss[maxn];
struct po{
long long x,y,id;
}h[maxn],zh[maxn];
void merge(int k,int l,int r){
if(l==r){num[k][l]=l;return;}
int mid=l+r>>1;
merge(k+1,l,mid);merge(k+1,mid+1,r);
int li=l,ri=mid+1;
for(int i=l;i<=r;i++){
if(li>mid){
num[k][i]=num[k+1][ri++];continue;
}
if(ri>r){
num[k][i]=num[k+1][li++];continue;
}
int v1=s[num[k+1][li]].h,v2=s[num[k+1][ri]].h;
if(v1<=v2)num[k][i]=num[k+1][li++];
else num[k][i]=num[k+1][ri++];
}
}
po xl(po a,po b){
po t;t.x=a.x-b.x,t.y=a.y-b.y;
return t;
}
long long cj(po a,po b){
return a.x*b.y-a.y*b.x;
}
void tb(int l,int r){
top=0;
for(int i=l;i<=r;i++){
po now;now.x=s[i].h,now.y=f[s[i].id]+s[i].h*s[i].h-s[i].w;now.id=s[i].id;
while(top>1&&cj(xl(zh[top],zh[top-1]),xl(now,zh[top]))<=0)top--;////
zh[++top]=now;
}
}
double getk(po a,po b){
if(a.x==b.x){
if(a.y>b.y)return -inf;
return inf;
}
double xx=a.x-b.x,yy=a.y-b.y;
return yy/xx;
}
void cdq(int k,int l,int r){
if(l==r)return;
int mid=l+r>>1;
cdq(k+1,l,mid);
for(int i=l;i<=mid;i++)s[i]=ss[num[k+1][i]];
tb(l,mid);
for(int i=mid+1;i<=r;i++)s[i]=ss[num[k+1][i]];
int fs=1;
for(int i=mid+1;i<=r;i++){
while(fs<top&&2*(double)s[i].h>getk(zh[fs],zh[fs+1]))fs++;
int j=zh[fs].id,ii=s[i].id;
f[ii]=min(f[ii],
f[j]+(ss[ii].h-ss[j].h)*(ss[ii].h-ss[j].h)+ss[ii-1].w-ss[j].w
);
}
for(int i=mid+1;i<=r;i++)s[i]=ss[i];
cdq(k+1,mid+1,r);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)scanf("%lld",&s[i].h);
for(int i=1;i<=n;i++){
scanf("%lld",&s[i].w);
s[i].w+=s[i-1].w;
s[i].id=i;
ss[i]=s[i];
}
merge(1,1,n);
for(int i=2;i<=n;i++)f[i]=inf;
cdq(1,1,n);
cout<<f[n]<<endl;
return 0;
}
ceoi2017 Building Bridges(build)的更多相关文章
- 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\),用不到 ...
- 洛谷.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_ ...
- [CEOI2017]Building Bridges
题目 斜率优化思博题,不想写了 之后就一直\(95\)了,于是靠肮脏的打表 就是更新了一下凸壳上二分斜率的写法,非常清爽好写 就当是挂个板子了 #include<algorithm> #i ...
- 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
目录 @desription@ @solution@ @accepted code@ @details@ @another solution@ @another code@ @desription@ ...
- 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\) 个数(不能移 ...
随机推荐
- 更换PostgreSql的data文件夹并重新服务器(此方法同样适用于系统崩溃后,找回数据的操作)
*如果是系统崩溃,需要找回数据,PostgreSQL安装目录的data文件夹要存在 1.备份PostgreSQL安装目录到其他目录下 2.停止Postgres服务,可以在运行中输入services.m ...
- 【luogu P1983 车站分级】 题解
题目链接:https://www.luogu.org/problemnew/show/P1983 符合了NOIP命题的特点,知识点不难,思维量是有的. step1:把题读进去,理解.得到 非停靠点的等 ...
- java内存模型原理阅读总结
Java内存模型可以理解为在特定操作协议下,对特定的内存或高速缓存进行读写访问的过程抽象.不同架构的物理计算机可以有不一样的内存模型,java虚拟机也有自己的内存模型,java虚拟机规范中试图定义一种 ...
- Spring Framework(框架)整体架构 变迁
Spring Framework(框架)整体架构 2018年04月24日 11:16:41 阅读数:1444 标签: Spring框架架构 更多 个人分类: Spring框架 版权声明:本文为博主 ...
- 抽象类&接口区别
抽象类:1.可以有构造方法. 2.可以有抽象方法也可以有具体方法. 3.权限修饰符可以是private.默认.protected.public. 4.可以定义成员变量. 5.interface ...
- CentOS 7+ 环境下安装MySQL
在CentOS中默认安装有MariaDB,但是我们需要的是MySQL,安装MySQL可以覆盖MariaDB MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 ...
- mac利用套件管理工具homebrew正确地同时安装python2.7和python3
MAC OSX 正確地同時安裝 PYTHON 2.7 和 PYTHON3 Python3 出來了(其實已經出來很久了,暈)!但是還是有很多 library 還是使用 Python2.7,所以要 ...
- 简述在php中 = 、==、 === 的区别(简述在php中 等于 、双等于、 三等于 的区别)
= 是赋值:就是说给一个变量赋值 == 是轻量级的比较运算,只看值不看类型 === 是重量级的比较运算,既看值,也看类型,要绝对相等才会为true
- Linux usb gadget框架概述
很幸运,在公司开发了gadget相关驱动,总结下来,大大小小开发了四个与gadget相关的驱动,字符驱动.g_multi.g_ether.g_zero,在这里把自己对gadget的开发中自己的感悟记录 ...
- PSTR、LPSTR等宏原型
1.首先介绍char.wchar_t ,宽字符wchar_t和窄字符char. 窄字符char了,大家都很清楚,就是8bit表示的byte,长度固定.char字符只能表示ASII码表中的256个字符, ...