POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈

嗯...

题目链接:http://poj.org/problem?id=2559

一、单调栈:

1.性质:

  单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递减。

2.模样:

  

  这是一个单调递增的栈,如果我们插入的元素大于栈顶元素,则直接入栈;

  如果我们插入的元素小于栈顶,则需要把栈内所有大于它的元素暂时出栈,将这个元素入栈后,再将暂时出栈的元素入栈,维护单调性。

二、模板:

  这道题是单调栈的一道模板题:

  先思考一个问题,如果题目中的矩形的高度都是单调递增的,如何得到最优解?

  显然有一个贪心的策略,就是以每一个矩形的高度作为最终大矩形的高度,看最宽能是多少,然后统计最优解。

  但如果进来的下一矩形比上一个低,它其实相当于限制了之前矩形的高度,那么之前矩形比这个矩形高出的高度在以后的统计中就没有丝毫用处了。

  这样,我们实际上就得到了单调栈的模型,只需要维护一个单调栈,在维护单调性的弹出操作时统计宽度,更新答案即可在O(n)实际内得到最优解。

  并且我们假设h[n+1]的位置有一个高度为0的矩形,最后将它加入单调栈时他会将所有矩形都弹出,那么答案也就完成最后的更新了。

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5
 6 int n, h[100005], w[100005], s[100005], top = 0;
 7 //h --> height  w --> width  s --> stack
 8 inline bool input(){
 9     scanf("%d", &n);
10     if(!n) return false;
11     for(int i = 1; i <= n; i++)
12         scanf("%d", &h[i]);
13     h[n + 1] = 0;//最后用来清空栈
14     return true;
15 }
16
17 inline long long work(){
18     long long ans = 0;
19     for(int i = 1; i <= n + 1; i++){
20         if(h[i] > s[top]){
21             s[++top] = h[i];
22             w[top] = 1;//宽度为1
23         }//满足单调
24         else{
25             int widthsum = 0;//宽度和
26             while(s[top] > h[i]){
27                 widthsum += w[top];
28                 ans = max(ans, (long long) widthsum * s[top]);//注意高是s[top]
29                 top--;
30             }
31             s[++top] = h[i];//此时s[top]已经小于h[i],满足单调
32             w[top] = widthsum + 1;//合并
33         }
34     }
35     return ans;
36 }
37
38 int main(){
39     while(input()){
40         printf("%lld\n", work());
41         top = 0;
42         memset(s, 0, sizeof(s));
43         memset(h, 0, sizeof(h));
44         memset(w, 0, sizeof(w));
45     }
46     return 0;
47 }

AC代码

原文地址:https://www.cnblogs.com/New-ljx/p/11225837.html

时间: 2024-08-05 21:27:07

POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈的相关文章

POJ 2559 Largest Rectangle in a Histogram RMQ || 单调栈

题目链接:点击打开链接 题意就是求最大面积 枚举每个柱子作为起点 然后二分两边长度. 求个区间最值. #include<stdio.h> #include<iostream> #include<math.h> using namespace std; #define ll long long #define N 100100 inline bool rd(int &n){ int x = 0, tmp = 1; char c = getchar(); while

HDU 1506 &amp;&amp; POJ 2559 Largest Rectangle in a Histogram (单调队列)

题目链接:POJ 2559  Largest Rectangle in a Histogram 题目链接:HDU 1506  Largest Rectangle in a Histogram 题意:给出一串序列表示对应矩形的高度,求整个图中最大的矩形区域. 2, 1, 4, 5, 1, 3, 3 如图所示: 思路:每个矩形向左向右最大能扩张到的长度乘上他的高度,求最大值就是答案. 用单调队列维护序列递减,出队列的元素即是"极值"点 注意:要用int64. AC代码: #include&

stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

题目传送门 1 /* 2 题意:宽度为1,高度不等,求最大矩形面积 3 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极限情况 4 st[]里是严格单调递增,若不记录的话还要O(n)的去查找L,R,用栈的话降低复杂度 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <algorithm> 9 #include &l

poj 2559 Largest Rectangle in a Histogram 栈

// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的是数据结构做,也可以递推做,目前只会数据结构的 // // 对于每个高度h,求一个左边界L和右边界R,分别表示的意义是 // L是下标为j的矩形的高度的hj小于当前h的最大的j的值.则根据定义 // 我们可以知道j到i之间的h都是大于当前的hi的. // R是下标为k的矩形的高度的hk大于当前h的最

POJ 2559 Largest Rectangle in a Histogram(单调栈)

[题目链接]:click here~~ [题目大意]: 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 con

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 shows the histogram that consists of rectang

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

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

POJ 2559 Largest Rectangle in a Histogram

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 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 heigh

POJ 2559 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 equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the