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…
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12297   Accepted: 3974 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base lin…
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…
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 题目链接(点击)来源poj 2559 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 show…
Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26012   Accepted: 8416 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi…
题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. #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()…
类型:单调栈 传送门:>Here< 题意:给出若干宽度相同的矩形的高度(条形统计图),求最大子矩形面积 解题思路 单调栈的经典题 显然,最终的子矩形高度一定和某一个矩形相等(反证).因此一个暴力的做法就是枚举每一个矩形,然后往两边扩散.在它左侧找到第一个高度比它小的,右侧也一样.则可以求出最大可扩散宽度,乘上高度即可以更新答案.复杂度$O(n^2)$ 如果说要优化刚才的算法,也就可以优化寻找最大可扩散宽度的速度 让每一个矩形依次入栈,保存两个关键字:矩形高度,其最大左扩散宽度.保证栈内的矩形高…
[题目链接] http://poj.org/problem?id=2559 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们枚举每个位置的最大高度全部被保留时得到的最优解,那么答案一定被包含在其中, 那么题目转化为求出每个高度左右两边最近的比其低的位置,可以用单调栈完成. [代码] #include <cstdio> #include <algorithm> using namespace std…
题意:给定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://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递减. 2.模样: 这是一个单调递增的栈,如果我们插入的元素大于栈顶元素,则直接入栈: 如果我们插入的元素小于栈顶,则需要把栈内所有大于它的元素暂时出栈,将这个元素入栈后,再将暂时出栈的元素入栈,维护单调性. 二.模板: 这道题是单调栈的一道模板题: 先思考一个问题,如果题目中的矩形的高度都是单调递增…
题意一个围挡由n个宽度为1的长方形挡板下端对齐后得到,每个长方形挡板的高度为hi.我们把其抽象成一个图形,问这个图形中包含的面积最大的长方形是多大? 输入多行数据,每行第一个为n,后面n个数,代表hi以0为结束 输出每行一个数 样例输入7 2 1 4 5 1 3 34 1000 1000 1000 10000样例输出84000 分析我们定一个中心为i,矩形高度为Hi,设他能在一个区间[l,r]中存在,必满足j∈[l,r]使Hj≥Hi.如果Hl-1≥Hi,显然可以继续向右扩张,那么Hl-1一定小于…
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目大意: 给出一个数列An,问以Ai为最小值的区间内有多少个元素? 解题思路: 手动模拟一个栈.栈内元素为一个单调不递减序列.当新元素Ai需要进栈,如果栈顶元素大于Ai,弹出栈顶元素,直到栈顶元素不大于Ai,Ai进栈. 这里可以保证i是高为栈顶元素的矩形向后延伸的边界,弹出过程中记录高为Ai的矩阵向前延伸的边界. //#include <bits/stdc++.h> #include &…
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 961  Solved: 679[Submit][Status][Discuss] Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height…
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22968    Accepted Submission(s): 7175 Problem Description A histogram is a polygon composed of a sequence of rect…
我实在是太菜了啊啊啊啊啊 到现在连个单调栈都不会啊啊啊 写个经典题 #include<cstdio> #include<algorithm> #include<cstring> #define ll long long using namespace std; ll dp1[],dp2[]; ll h[]; int main() { ll n; while(scanf("%d",&n)!=EOF&&n){ memset(h,,…
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 刚开始没考虑时间复杂度,直接敲了,直接tle了,之后没有思路,然后看题解,看见大神写的优化非常棒. 大神的解释:(其实对于w[i]来说,如果去求后面连续的值,完全没必要一个个去比对,直接看w[i-1]的值就行了. 比如说2.3.4.5这个序列,如果我们要看3往后能延伸多长,并不需要去逐个和4和5比较,在计算4的时候,我们已经计算过5是比4大的,因为3比4小,4所能往后延伸的长度,3也一定能达到(4…
目录 题目 思路 \(Code\) 题目 Largest Rectangle in a Histogram 思路 单调栈. 不知道怎么描述所以用样例讲一下. 7 2 1 4 5 1 3 3 最大矩形的高度一定是给你的高度中的一个. 我们枚举最大高度. 很明显以某一高度为高的矩形的左边界是该高度左边第一个比它矮的. 右边界类似. 我们可以用单调栈去维护每一个高度左右第一个比他矮的位置即可 \(Code\) #include<iostream> #include<cstring> #i…
单调栈裸题 如果矩形高度从左到右是依次递增,那我们枚举每个矩形高度,宽度拉到最优,计算最大面积即可 当有某个矩形比前一个矩形要矮的时候,这块面积的高度就不能大于他本身,所以之前的所有高于他的矩形多出来的部分都没用了,不会再计算第二次. 因此我们只需要用单调栈维护矩形高度即可,当有高度较低的矩形进栈时,先将之前比他高的每个矩形弹出,宽度累加,更新答案.然后我们要进栈的矩形的宽度也要变成之前累加的宽度+1,因为要保留有用部分.. 有个小技巧:为了方便程序,在末尾加一个高度为0的矩形 #include…
Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/8556852 #include <stdio.h> #include <iostream> using namespace std; ]; ],r[]; int main() { int n,i,j; long long maxx,sum; while(~scanf("%d…
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       A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have…
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…
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…
[POJ 2559]Largest Rectangle in a Histogram 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 s…
题目链接: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…
                                     Largest Rectangle in a Histogram 这么经典的题硬是等今天碰到了原题现场懵逼两小时才会去补题...废话不多说: 题意:截取一个矩形使其面积最大,随你怎么截,反正面积要最大.输出最大面积,注意爆int. 思路:dp或单调队列.核心思路是从一个小矩形往两边扩散,分别找两边第一个小于这个矩形的位置,其中面积就是这个小矩形与区间长度的乘积,我们预处理出所有的小矩形的这两个端点值,然后扫一遍即可更新到最…