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 ...
随机推荐
- JavaWeb学习总结(二)—http协议
http协议概念: * 即超文本传输协议.它规定了浏览器与服务器之间的通讯规则. * http是基于请求/响应模式的,所以分为请求协议和响应协议 http的类型: HTTP协议的版本:HTTP/1.0 ...
- C++——string类和标准模板库
一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...
- linux 文件类型 文件权限
linux中常见的文件类型有: “—”表示普通文件 :-rw-r--r-- 1 root root 41727 07-13 02:56 install.log “d”表示目录 :drwxr-xr- ...
- 详解zabbix安装部署(Server端篇)
原文:http://blog.chinaunix.net/uid-25266990-id-3380929.html Linux下常用的系统监控软件有Nagios.Cacti.Zabbix.Monit等 ...
- hdfs namenode -initializeSharedEdits 和 hdfs namenode -bootstrapStandby
hdfs namenode -initializeSharedEdits 将所有journal node的元文件的VERSION文件的参数修改成与namenode的元数据相同 hdfs namenod ...
- python 图实现
#coding:utf-8 __author__ = 'similarface' class Graph: def __init__(self,label,extra=None): #节点是类实例 s ...
- EXEL表格读取 按键精灵
EXEL表格读取(1,m)(2,m)表格信息,m为行数 以下为本帖隐藏内容 ============================== Call Plugin.Office.OpenXls(&quo ...
- 例题:计算运费。c#语言基础,比较简单。看代码输入格式和方法。同样方法可以做一个 出租车打车的程序
while (true) { Console.WriteLine("请输入行李重量"); double k = Convert.ToDouble(Console .ReadLine ...
- Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio »迁移
Migrations¶ 4 of 4 people found this helpful The Contoso University sample web application demonstra ...
- Machine Learning for hackers读书笔记(三)分类:垃圾邮件过滤
#定义函数,打开每一个文件,找到空行,将空行后的文本返回为一个字符串向量,该向量只有一个元素,就是空行之后的所有文本拼接之后的字符串 #很多邮件都包含了非ASCII字符,因此设为latin1就可以读取 ...