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 ...
随机推荐
- linux权限管理_ACL权限
一.什么是ACL权限 ACL是Access Control List(访问控制列表)的缩写,主要的目的是在提供传统的owner,group,others的read,write,execute权限之外的 ...
- 3-1 rpm包命名规则
1.RPM包的来源 <1>RPM包在系统光盘中 ---------------------------------------------------------------------- ...
- 20145220 实验五 Java网络编程
20145220 实验五 Java网络编程 实验内容 1.用书上的TCP代码,实现服务器与客户端. 2.客户端与服务器连接 3.客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务 ...
- HDU-5781 ATM Mechine(概率DP)
题目大意:某个未知整数x等概率的分布在[0,k]中.每次你都可以从这个整数中减去一个任意整数y,如果x>=y,那么x=x-y,操作次数累计加1:否则,将会受到一次错误提示.当错误提示超过w次,将 ...
- 越狱Season 1-Episode 17: J-Cat
Season 1, Episode 17: J-Cat -Pope: Hey, that's looking good. 嗨,看起来真棒 You're making some real progres ...
- 《剑指Offer》之二维数组中的查找
1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 2.代码实现 pu ...
- Improve Scalability With New Thread Pool APIs
Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...
- 我今天也学习了做jquery插件
先贴代码 (function ( $ ) { var id=33; $.fn.validate=function(options){ // This is the easiest way to hav ...
- mysql学习之-show table status(获取表的信息)参数说明
--获取表的信息mysql> show table status like 'columns_priv'\G;*************************** 1. row ******* ...
- linux下恢复误删除的文件方法(ext2及ext3)
linux下恢复误删除的文件方法(ext2及ext3) 2009-12-19 15:23:47 分类: LINUX 如果是ext2文件系统的,直接用debugfs是可以恢复出来的,但对于ext3,d ...