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 ...
随机推荐
- 黑马程序员——JAVA基础之数组
------- android培训.java培训.期待与您交流! ---------- 数组: 数组的定义: 数组是相同类型数据的集合, 描述的是相同类型的若干个数据按照一定的先后顺序排列组合而成,其 ...
- HashMap的原理与实 无锁队列的实现Java HashMap的死循环 red black tree
http://www.cnblogs.com/fornever/archive/2011/12/02/2270692.html https://zh.wikipedia.org/wiki/%E7%BA ...
- NetHogs下载和监控
转自:http://blog.csdn.net/testcs_dn/article/details/40506225 CentOS6.5下使用NetHogs监控进程网络使用情况 分类: CentOS2 ...
- Sublime Text 下配置python
Sublime Text 2作为一款轻量级的编辑器,特点鲜明,方便使用,而且支持多语言. 一.control+B方式 1. 在工具栏点击Preferences,打开Browse Packages.在 ...
- 基于window.onerror事件 建立前端错误日志
QA不是万能的,用户的浏览环境非常复杂,很多情况无法靠测试用例去覆盖,所以最好建立一个前端错误日志,在真实用户端收集bug. try&catch是一个捕获前端错误的常见方法,比如: { //给 ...
- 【转】关于B/S架构应用程序的权限设置分析和总结
来自:http://www.cnblogs.com/zhouxunyu/p/3790122.html 分析:不同的用户登录到系统后赋予不同的操作权限,而用户存在于数据库中,标识用户权限的字段也保存在数 ...
- ManifoldJS
微软推出开源工具ManifoldJS,可以自动将Web App转换成各种平台的App(安卓.iOS.Chrome OS.Windows). ManifoldJS通过获取网站meta-data信息,即可 ...
- python input() 与 raw_input()
使用input和raw_input都可以读取控制台的输入,但是input和raw_input在处理数字时是有区别的 当输入为纯数字时: input返回的是数值类型,如int,floatraw_inpo ...
- TKinter之菜单
菜单的分类也较多,通常可以分为下拉菜单.弹出菜单. 1.使用Menu类创建一个菜单 2.add_command添加菜单项,如果该菜单是顶层菜单,则添加的菜单项依次向右添加. 如果该菜单时顶层菜单的一个 ...
- C# 输出pdf文件流在页面上显示
1 不调用itextsharp.dll的操作 /// <summary> /// 生成pdf流 /// </summary> /// ...