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

来源

ceoi2017 day2


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;
}

最新文章

  1. html5悬浮球效果
  2. Elasticsearch——Date Math在索引中的用法详解
  3. ambari之hbase数据迁移
  4. APICloud全面支持WiFi真机同步和实时预览功能
  5. Linux/U-Boot Git Repo
  6. 《Django By Example》第七章 中文 翻译 (个人学习,渣翻)
  7. 更改Windows更新源(解决公司内部网络无法下载语言包或更新的问题)
  8. noip2017逛公园
  9. 图数据库cayley+mongo的起航之旅
  10. HTML5 桌面提示
  11. Amazon SES SPF和DKIM设置教程
  12. ubuntu 添加CDROM安装源
  13. 【BZOJ】【2878】【NOI2012】迷失游乐园
  14. ora-12154:tns:could not resolve the connect identifier specied
  15. 使用ControllerClassNameHandlerMapping实现SpringMVC的CoC配置
  16. 初识Locust---认识
  17. 实用的随机数生成类Random:测试(随机产生100个不重复的正整数)
  18. 用友u8数据库表结构
  19. windows拾遗
  20. tomcat 服务不支持 chkconfig 以及其他服务不能添加到开机启动时的操作

热门文章

  1. python_23_tuple
  2. AngularJs学习笔记-组件间通讯
  3. 管理员必备的几个Linux系统监控工具
  4. Oracle Analyze
  5. madplay移植
  6. (83)zabbix Less than 25% free in the configuration cache解决
  7. IDEA整合Mybatis+Struts2+Spring (二)--整合框架
  8. 从Mixin到hooks,谈谈对React16.7.0-alpha中即将引入的hooks的理解
  9. 精通SpringBoot--整合druid监控SQL执行
  10. [原创]使用python对视频/音频文件进行详细信息采集,并进行去重操作