ceoi 2017 Building Bridges

题目描述
A wide river has n 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 i and j is (hi−hj)2 as we want to avoid uneven sections, where hi is the height of the pillar i. 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−th pillar is equal to wi. This cost can even be negative—some interested parties are willing to pay you to get rid of certain pillars. All the heights hi and costs wi 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, n.The second line contains pillar heights hi in the order, separated by a space.The third line contains wi in the same order, the costs of removing pillars.
第一行一个正整数 n。
第二行 n 个空格隔开的整数,依次表示h1,h2,⋯,hn。
第三行 n 个空格隔开的整数,依次表示w1,w2,⋯,wn。
输出
Output the minimum cost for building the bridge. Note that it can be negative.

输出一行一个整数表示最小代价,注意最小代价不一定是正数。

题解

做的时候居然直接斜率优化了,果然是DP写傻了……(逃
然鹅
事情的真相就是这么简单
斜率优化不存在的
首先,斜率优化完的式子可以看出
f[i] = min{f[j]+h[j]^2-sum[j]-2*h[i]*h[j} + h[i]*h[i] +sum[i-1]
这很显然是一条直线的表达式,其中
k = -2*h[j],b = f[j]+h[j]^2-sum[j]
剩下的都是常数看的出来吧……
那么去tmd斜率优化,怎么暴力怎么来!
维护一个权值线段树,统计0~1e6之间每个点对应的直线最小值
很显然当前的最优解就是线段树对应h[i]的值
考虑如何更新线段树
我们建立4个船新变量,定义为 newl,newr,oldl,oldr,(很显然易懂有木有!!
newl 表示当前插入直线在这个区间的左值
oldl表示当前区间已有直线的左值
其余同理
考虑三种情况
①: oldl <= newl && oldr <= newr 那么由于其为一次函数,则不会对答案产生影响,return即可
②:newl <= oldl && newr <= oldr 所以当前区间最值全被覆盖,修改后return
default: 根据一次函数图像(自己画,在l到r的区间内两直线必有交点,递归处理就好了
明明是这么傻逼的题居然想了半天,太菜了woc
不上代码了太傻逼(痛哭

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值