Largest Rectangle in a Histogram       A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have…
题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的高度乘上左右边界的宽度求最大值就行了. 也可以用笛卡尔树上dp的方法搞一搞,即用每个结点权值和所在子树的大小分别表示高度和宽度.(建笛卡尔树的过程也用到了单调栈,作用是维护右链) 单调栈做法: #include<bits/stdc++.h> using namespace std; typedef…
E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a…
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 sh…
题目链接:HDU - 1506 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 rec…
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18833    Accepted Submission(s): 5636 Problem Description…
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 rectang…
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11706    Accepted Submission(s): 3219 Problem Description A histogram is a polygon composed of a sequence of rec…
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11137    Accepted Submission(s): 3047 Problem Description A histogram is a polygon composed of a sequence of rec…
Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782   Accepted: 6393 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi…
传送门 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 rec…
Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi…
Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29498   Accepted: 9539 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi…
题目链接:http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24259   Accepted: 7844 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common bas…
单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一个矩形的高度当作最后的高度,来从中选取最大值就可以了 但是如果它不是递增的,中间会出现低谷,那么要还想运用贪心策略就要把之前高度大于它的全部扔掉,但是再扔掉他们之前还要判断一下以他们为最后答案的高度可不可行,这样我们就是在构造一个递增序列,可以用栈来维护它 代码: 1 #include<stdio.…
题意:给定n个连续排列的矩形的高,矩形的宽都为1.问最大矩形覆盖. 例如:n = 7,h[i] = (2 1 4 5 1 3 3),最大覆盖为8. Sample Input 7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0 Sample Output 8 4000 题解: 首先可以确定,最大矩形的高一定等于某个矩形的高,宽一定等于某个矩形可以向两边最大可以延伸到的范围. 维护一个非降序的单调栈,然后依次枚举矩形的高 h[i]. 当 h[i] > top()时,说明…
题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #include<bits/stdc++.h> using namespace std; typedef long long LL; ; int Stack[N]; //Stack[]为单调栈(即每次能入栈的元素值必比栈顶元素(若存在)要大) int len[N]; //len[]与Stack[]同步 ,记录的是从Stack[]中对应元素起向左最大可延伸的宽度 int n; LL top,ans,h; void print()…
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 关于笛卡尔树的构建:https://www.cnblogs.com/reverymoon/p/9525764.html 笛卡尔树在 key 上满足二叉搜索树,在 value 上满足堆:一般 key 就是原序列里的位置,这样一个子树对应原序列的一段连续区间. 这个构建方法就是给最右链维护单调栈,新进来第 i 个元素之后,根据 value 是堆的规则弹栈,然后把自己的左孩子设成最后弹掉的那个点,把自己…
题目大意 有一个直方图,其所有矩形的底均是1(以后简称小矩形).给出这些矩形的高度,求这些矩形的并集中存在的面积最大的矩形(简称大矩形)的面积. 题解 大矩形的高必然一边等于一个小矩形的高,另一边小于等于另一个小矩形的高. 我们现考虑面积最大矩形左边高等于其所在小矩形的高的情况,则其右边高小于等于其对应的小矩形的高(以后将此简称为左等高矩形).我们要维护一个单调栈,使得栈里的矩形的高度都是单调递增的.一个一个枚举小矩形,如果当前小矩形高度比栈顶的矩形高度高或等,则如果所求大矩形是个左边为栈内矩形…
这个问题姑且也叫做最大子矩阵吧 给一个树状图,求一个最大面积的子矩阵 思路是这样的,对于每个单位矩阵,求出左边连续不比它低的矩阵的下标,放在l数组里 同样,再求出右边连续的不比它低的矩阵的下标 这样,对于每个单个矩阵所能得到的最大面积就是 (r[i]-l[i]+1)*a[i] //#define LOCAL #include <iostream> #include <cstdio> #include <cstring> using namespace std; + ;…
题意:坐标轴上有连续的n个底均为1,高为h[i]的矩形,求能够构成的最大矩形的面积. 学习的别人的代码 @_@ 看底的坐标怎么找的看了好一会儿--- 记l[i]为矩形的底的左边的坐标,就将它一直向左扩展 记r[i]为矩形的底的右边的坐标(倒着找,从n开始找,题解里面还着重强调了要倒着找,要不然就体现不出优化了)至于那个优化,我想的是,每一次算r[i]的时候,它前面的一个r[i]就是覆盖得最远的了,可以减少计算(用了一组样例试验) 然后就枚举找出面积的最大值-- #include<iostream…
# include <stdio.h> # include <algorithm> # include <iostream> # include <math.h> using namespace std; __int64 a[100010],l[100010],r[100010];///l[i]左边连续大于等于a[i]的下标,r[i]右边连续大于等于a[i]的下标,所以对于a[i]的矩形面积为(l[i]-r[i]+1)*a[i]; int main() {…
题意: 有一个柱状图,有N条柱子.每一条柱子宽度都为1,长度为h1...hN. 在这N条柱子所构成的区域中找到一个最大面积,每平方米3块钱,问最多赚多少钱. 输入: 1<=N<=100000 0<=hi<=1000000000 思路: N很大,肯定得用一个O(N)或O(NLOGN)的算法,, 假如这个面积的长是从第i条柱子到第j条柱子,宽则一定是第i条柱子到第j条柱子中最矮的那条柱子的高度. 而我们站在那根最矮的柱子向左看,第i-1条的柱子一定是小于当前柱子的,否则可以加进去.向右…
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(-1,0) #include<cstdio> #include<stack> #define maxn 100005 using namespace std; long long h[maxn]; struct Element { long long height; int startpos; Element(){} Element(long long _h,int _p){ hei…
题目大意: 输入n,,1 ≤ n ≤ 100000,接下来n个数为每列的高度h ,0 ≤ hi ≤ 1000000000 求得最大矩阵的面积 Sample Input 7 2 1 4 5 1 3 34 1000 1000 1000 10000 Sample Output 84000   #include <bits/stdc++.h> #define ll long long using namespace std; ll n,a[],L[],R[],sta[]; // sta[]模拟栈 in…
http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19316    Accepted Submission(s): 5829 Problem Description A hist…
Largest Rectangle in a Histogram http://acm.hdu.edu.cn/showproblem.php?pid=1506 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description A histogram is a polygon composed of a sequence of rectangles alig…
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19580    Accepted Submission(s): 5921 Problem Description A histogram is a polygon composed of a sequence of rect…
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目大意: 给出一个数列An,问以Ai为最小值的区间内有多少个元素? 解题思路: 手动模拟一个栈.栈内元素为一个单调不递减序列.当新元素Ai需要进栈,如果栈顶元素大于Ai,弹出栈顶元素,直到栈顶元素不大于Ai,Ai进栈. 这里可以保证i是高为栈顶元素的矩形向后延伸的边界,弹出过程中记录高为Ai的矩阵向前延伸的边界. //#include <bits/stdc++.h> #include &…
1506意甲冠军:给你一个连续的直方图(拼贴底部长度1).求连续基质区. 对每一个直方图,分别向左向右进行扩展. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream> #include <queue> #include <…