HDU 1506 Largest Rectangle in a Histogram set+二分
Largest Rectangle in a Histogram
Problem Description:
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:

Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Input:
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
Output:
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
Sample Input:
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output:
8
4000
【题目链接】**HDU - 1506 **
【题目类型】set+二分
&题意:
给你n个数,代表矩形的高度,他们的宽都是1,要你求最大的矩形的面积。
&题解:
首先看一眼范围,1e5,则nlogn可过,那么我就想了一下二分,发现可行,思路如下:
先排序,每次都从高度最小的开始找,算出以他为最高,以(*it2 - *it1 - 1)为底的面积,这样就会有n个面积取最小就好了,那么难处理就难在求这个点旁边的最近的两个点是什么?
我是用set来做的,首先set可以set.lower_bound(),还有set.insert()的复杂度是logn,那么二分找最近的点,之后每次都把用过的点insert()一下就好了。
【时间复杂度】O(nlogn)
&代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100000 + 5 ;
int n, s2[MAXN];
pair<int, int > s[MAXN];
bool cmp(pair<int, int > a, pair<int, int > b) {
return a.first == b.first ? a.second > b.second : a.first < b.first;
}
set<int> sei;
set<int>::iterator it1, it2;
ll ans;
int main() {
while (~scanf("%d", &n), n) {
for (int i = 0; i < n; i++)
scanf("%d", &s[i].first), s2[i] = s[i].first, s[i].second = i;
sort(s, s + n, cmp);
sei.clear();
ans = 0;
sei.insert(-1);
sei.insert(n);
for (int i = 0; i < n; i++) {
int d = s[i].second;
it1 = sei.lower_bound(d - 1);
it2 = sei.lower_bound(d + 1);
if (*it1 >= d && it1 != sei.begin()) it1--;
if (it2 == sei.end())it2--;
ans = max(ans, (ll)(*it2 - *it1 - 1) * s2[d]);
sei.insert(d);
}
printf("%lld\n", ans);
}
return 0;
}
HDU 1506 Largest Rectangle in a Histogram set+二分的更多相关文章
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- hdu 1506 Largest Rectangle in a Histogram 构造
题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- HDU 1506 Largest Rectangle in a Histogram(区间DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...
- DP专题训练之HDU 1506 Largest Rectangle in a Histogram
Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 1506 Largest Rectangle in a Histogram(单调栈)
L ...
- HDU 1506 Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)
单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...
- hdu 1506 Largest Rectangle in a Histogram——笛卡尔树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 关于笛卡尔树的构建:https://www.cnblogs.com/reverymoon/p/952 ...
随机推荐
- NOIP2016 D2T1 組合數問題(problem)
题目描述 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的定 义,我们 ...
- HDU-4089 Activation (概率DP求概率)
题目大意:一款新游戏注册账号时,有n个用户在排队.每处理一个用户的信息时,可能会出现下面四种情况: 1.处理失败,重新处理,处理信息仍然在队头,发生的概率为p1: 2.处理错误,处理信息到队尾重新排队 ...
- Mac上因磁盘格式导致gulp无限刷新问题
今天遇到个超奇葩的问题,使用gulp.watch监控文件变化,但是并没有修改文件,却一直执行change,导致浏览器无限刷新 调试了10小时,代码各种删改,一直不得其解.切换到Windows运行,又正 ...
- IE 下加载jQuery
转:http://www.iitshare.com/ie8-not-use-native-json.html 解决在IE8中无法使用原生JSON的问题 起因 在项目中要将页面上的js对象传给后台, ...
- C++@命名空间(转)
转自http://hi.baidu.com/rainysky_2006/blog/item/a490e01fc3de7964f724e4d1.html 本讲基本要求 * 掌握:命名空间的作用及定义:如 ...
- What is Proguard?
When packaging an apk, all classes of all libraries used by the program will be included, this makes ...
- ogg 、 Shareplex和DSG RealSync 对比
主流数据库容灾(复制)工具对比 Oracle Golden Gate Quest Shareplex DSG RealSync 公司概要 公司介绍 GoldenGate公司成立于200 ...
- rsyslog日志服务的配置文件分析
基于rsyslog日志服务的日志 在不同的LINUX系统,实现的软件略有不同. syslog,rsyslog,syslog-ng,用于实现系统日志的管理. [root@asianux4 ~]# rpm ...
- shell脚本实例-菜单样例
1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...
- linux知识点
通过gui来使用通过api来使用通过cli来使用通过tui来使用 进程不在,但tcp连接还一直存在的解决办法--tcpkill命令 http://www.centoscn.com/CentOS/Int ...