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 ...
随机推荐
- 解决dede搜索页面只能显示10条信息解决方案
解决dede搜索页面只能显示10条信息解决方案,感觉显示的信息太少,这时就要想办法去解决一下.看看有什么好办法来解决一下这个问题. dede搜索页模板中,默认只能显示10条记录. 打开dede搜索页模 ...
- hdu1811 并查集+拓扑序
题意:现在有一个排名系统,有一系列信息,分别是 > < = 的比较,而如果最终相等,就会将这些相等的按照序号从小到大排,问给出的信息是否可以确定完整的排序. 由于如果很多点相等,他们肯定能 ...
- 内存使用空间之swap建置[转]
http://www.cnblogs.com/ggjucheng/archive/2012/08/22/2651502.html 内存置换空间(swap)之建置 安装时一定需要的两个 partitio ...
- 防止 SQL 注入的方法(摘抄)
——选自<深入Ajax : 架构与最佳实践 = Advanced Ajax : architecture and best practices/ (美)Shawn M.Lauriat著:张过,宋 ...
- oracle中如何指定表字段自增
背景介绍: SQL SERVER可以在int类型的字段后加上identity(1,1),该字段就会从1开始,按照+1的方式自增,将这个字段设置为主键,有利于我们进行数据的插入操作.MySql中可以使用 ...
- 靠边伸缩菜单的做法(类似QQ,碰到就会伸出来)
这段脚本主要实现一个group的伸缩功能,group里面的内容也就是菜单的内容可以自由添加. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- c++ 读写注册表
void RegUpdate(HKEY hRootKey,LPCSTR szSubKey,LPCSTR szValueName,LPCSTR szValue) { HKEY hKey; LONG rc ...
- vs2010 无法创建 *.edmx(Entity Frame Work) 文件的问题
当你安装了VS2010或者已经安装了EntityFramework41RC.exe之后发现依然在Add New Item时无法找到ADO.NET Entity Model,有可能是你创建的不是netf ...
- 虚拟化之vmx配置文件
https://www.haiku-os.org/get-haikuhttp://sanbarrow.com/vmx.html contradictionn. 矛盾:否认:反驳homegrownadj ...
- 一个LINUX狂人的语录(个人认为很精辟)
http://blog.chinaunix.net/uid-57160-id-2734431.html?page=2 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的 ...