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 ...
随机推荐
- URAL 1076 Trash Trash(最大权匹配)
Trash Time limit: 1.0 secondMemory limit: 64 MB You were just hired as CEO of the local junkyard.One ...
- How to Configure the Gradient Boosting Algorithm
How to Configure the Gradient Boosting Algorithm by Jason Brownlee on September 12, 2016 in XGBoost ...
- caffe: test code 执行出问题: Check failed: FLAGS_weights.size() > 0 (0 vs. 0) Need model weights to score.
Check failed: FLAGS_weights.size() > 0 (0 vs. 0) Need model weights to score. 出现这个错误,但是我记得昨天还好好的, ...
- premiere视频输出参数设置
- android text中显示HTML语言
package com.example.test; import java.io.InputStream; import java.net.URL; import android.annotation ...
- C# 大于屏幕的窗体
1.使用SetWindowPos就可以做到这一点,只是最后一个参数要选对. RECT windowRect = new RECT(); User32.GetWindowRect(MyForm2.Han ...
- Python Queue实现生产与消费
Python Queue模块详解 from:https://blog.linuxeye.com/334.html Python中,队列是线程间最常用的交换数据的形式.Queue模块是提供队列操作的模块 ...
- IIS6下PHP环境的资源未找到(404)问题
故障现象: 无法找到该页, 404错误
- Cobertura 代码覆盖率测试
Cobertura 是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率.除了找出未测试到的代码并发现 bug 外,Cobertura 还可以通 ...
- Ubuntu上安装mono并进行C#代码测试
微软的.NET框架与Linux开发和管理,是Buider AU和一个更广泛行业的两个最流行的主题. 大多数时候,这两个主题往往会产生冲突,很少有开发者需要同时了解这两个工具.但是,许多人都没意识到, ...