City Skyline
题目大意:(poj 3044)
给出城市的正视图,所有的大楼都是矩形,给出正视图每个高度改变时的坐标,问最少有多少大楼。一共N个矩形,N<=50000
解题过程:
首先可以把问题转化一下:有N块木板要粉刷,每次只能刷矩形,并且矩形要挨着地面,也不能刷到外面,最少刷多少次。
题目给出的每块木板的宽度其实是木有用的,因为相同高度且挨在一起的木板肯定同时刷.所以可以把所有的宽度缩成1.
那么根据贪心的思想,如果横着刷一次可以刷掉多个矩形,那么肯定横着刷。但是数据范围有点大,想不出什么好的办法,我就用了分治的方法。每次找到区间内高度最矮的,然后横着刷一次,再以这个点为边界左右分治。很明显如果高度是单调增的,复杂度会退化到O(N2) 但还是被我32ms水过去了。。。
下面介绍另外3种方法:
A: 暴力处理出从每个点出发向右最多能刷多远(扫描到第一个比它矮的就停止),如果刷的路上碰到和它一样高的,就打个标记,下次就不用处理打过标记的点。 该方法比分治还要暴力,16ms水过去,但是很明显也会被单调性比较明显的数据卡死。
具体代码参考http://blog.csdn.net/z309241990/article/details/8596517
B:对方法A进行优化,从右往左扫,用类似并查集的方法处理出每个点向右最多能刷多远.复杂度不好估计,但是也可以出数据卡它,比如 5 9 8 7 4 处理最左边的那个5的时候需要扫4次找到4.如果这样的数据循环出现,那么时间显然会变慢。但是对付本题N<=50000的数据, 就算上面的数据循环出现k次,那么平均长度是N/k,最左边的点需要k次查找,其它点可以看做只要1次查找,所以还是O(N)级别的。 所以还是非常不错的一个办法。
具体代码参考http://www.cnblogs.com/tmeteorj/archive/2012/09/04/2670957.html
代码中y[i]表示高度为i的最远刷到哪里.
C:用单调栈优化方法A.对于一个高为h的点x,如果它前面有一些比它高的点,那么从那些点往右边刷肯定到达当前x.所以从栈中弹出,并ans++... 实在很难描述,看代码理解吧.时间复杂度O(N),应该算是最完美的算法了。
具体代码参考http://blog.csdn.net/u010770930/article/details/20155189
City Skyline的更多相关文章
- BZOJ1628: [Usaco2007 Demo]City skyline
1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 256 Solved: 210[Su ...
- Leetcode 807 Max Increase to Keep City Skyline 不变天际线
Max Increase to Keep City Skyline In a 2 dimensional array grid, each value grid[i][j] represents th ...
- LC 807. Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线
https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...
- [Swift]LeetCode807. 保持城市天际线 | Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j]represents the height of a building located ther ...
- [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline
In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 【Leetcode】807. Max Increase to Keep City Skyline
Description In a 2 dimensional array grid, each value grid[i][j] represents the height of a building ...
- 【LeetCode】807. Max Increase to Keep City Skyline 解题报告(Python &C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Swift语言学习之学习资源
(1) http://swift.sh (2) Let's Swift – WRITE THE CODE. CHANGE THE WORLD. http://letsswift.com (3)http ...
- 正则表达式使用(Js、Java)
Js中全局替换,需要在最后加上g(global),并且使用//包围起来 1.全局替换字符+ 和 只替换第一个字符+ alert("2014+03-22++aaaa".replace ...
- python 打包与部署
环境:win10.eclipse-jee-mars.python2.7 打包在linux上进行安装测试 1.1 打包 项目目录结构如下: 打包对象:utils.reg 在P1项目的顶级目录,也就是ut ...
- 转!!Java 基础面试题的剖析: short s1=1;s1 = s1 +1 报错? s1+=1 呢
short s1=1;s1 = s1 +1会报错吗? package common; public class ShortTypeTest { /* * @param args */ publi ...
- commonJS — 对象操作(for Object)
for Object github: https://github.com/laixiangran/commonJS/blob/master/src/forObject.js 代码 /** * Cre ...
- 基因组组装工具之 SOAPdenovo 使用方法
SOAPdenovo是一个新颖的适用于组装短reads的方法,能组装出类似人类基因组大小的de novo草图. 该软件特地设计用来组装Illumina GA short reads,新的版本减少了在图 ...
- centos7 使用 omnibus包安装方式,安装 gitlab7.4
centos7 使用 omnibus包安装方式,安装 gitlab7.4 1: gitlab是一个开源的软件,类似于github.com那样的git代码管理仓库: 官网 https://about.g ...
- iOS 使用drawRect: 绘制虚线椭圆
iOS 使用drawRect: 绘制虚线椭圆 1:首先如果要使用 drawRect 绘图 要导入 CoreGraphics.framework 框架 然后 创建 自定义view, 即是 myView继 ...
- android 内存泄露之jni local reference table overflow (max=512)
在android项目中要实现一个需求 为了性能的要求只能用c代码来实现功能. 这样就牺牲了java跨平台性. 通过加载.so的方式,把用c实现的模块集成到app中. android提供jni层,作为一 ...
- hdu-----(1151)Air Raid(最小覆盖路径)
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...