stack(单调栈) POJ 2082 Terrible Sets

题目传送门

题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少

分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度都累积到当前的矩形中,这样最后再扫描一遍,算面积很方便,这题应该算是 POJ 2559 的强化版了

收获:stack的应用,求矩形面积,矩阵相乘,表达式计算

代码:

/************************************************
* Author        :Running_Time
* Created Time  :2015/9/9 星期三 13:50:48
* File Name     :L.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 5e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct M    {
    int w, h;
}m[N];

int main(void)    {
    int n;
    while (scanf ("%d", &n) == 1)   {
        if (n == -1)    break;
        for (int i=1; i<=n; ++i)    {
            scanf ("%d%d", &m[i].w, &m[i].h);
        }
        stack<M> S;
        int ans = 0, lasth = 0;
        for (int i=1; i<=n; ++i)    {
            if (m[i].h >= lasth)    {
                S.push (m[i]);  lasth = m[i].h;
            }
            else    {
                int totw = 0, area = 0;
                while (!S.empty () && S.top ().h > m[i].h)  {
                    totw += S.top ().w;
                    area = totw * S.top ().h;
                    if (area >= ans)    ans = area;
                    S.pop ();
                }
                m[i].w += totw;
                S.push (m[i]);  lasth = m[i].h;
            }
        }
        int totw = 0, area = 0;
        while (!S.empty ()) {
            totw += S.top ().w;
            area = totw * S.top ().h;
            if (area >= ans)    ans = area;
            S.pop ();
        }
        printf ("%d\n", ans);
    }

    return 0;
}

  

时间: 2024-08-02 16:29:53

stack(单调栈) POJ 2082 Terrible Sets的相关文章

poj 2082 Terrible Sets (数据结构 ——栈 STL)

 Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2999   Accepted: 1549 Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elem

POJ 2082 Terrible Sets

Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5325   Accepted: 2713 Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some element

[单调栈] poj 3250 Bad Hair Day

题意: 一排母牛站成一排,给出牛的身高,每只牛都只能往右看,对于每只母牛有一个c[i] c[i]代表i能看见多少只牛,矮的牛看不见高的牛,问所有c[i]的和是多少. 思路: 我们转换一下,其实就是求对于每只母牛能被看见多少次,显然是对于它往左单调递增的牛都能看见它 那么我们维护一个这样的单调栈,每次都将小于等于它的出栈,那么栈中的元素的个数就是能看见它的个数 这样再把该元素插入栈就好了. 代码: #include"cstdlib" #include"cstring"

POJ 3250 Bad Hair Day(单调栈)

题目地址:POJ 3250 初学单调栈.多校和网络赛已经碰到两次了. 单调栈的原理简单的不能再简单了..就是让栈里的元素从栈顶到栈底呈单调性. 比如说递增单调栈. 每次放进一个数的时候,如果栈顶的数小于要放的数,就把栈顶的数pop出来使得栈里保持单调性. 对于这道题来说,就从右往左开始遍历,建一个递增单调栈.那么每次pop出来的就是当前的牛可以看到的牛数.然后累加即可. 代码如下: #include <iostream> #include <cstdio> #include <

Terrible Sets_单调栈

Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0. Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such

poj 2059 单调栈

题意:求柱状图中最大矩形面积. 单调栈:顾名思义就是栈内元素单调递增的栈. 每次插入数据来维护这个栈,假设当前须要插入的数据小于栈顶的元素,那就一直弹出栈顶的元素.直到满足当前须要插入的元素大于栈顶元素为止.能够easy求出某个数左边或右边,第一个大于或小于它的数,且复杂度是O(n). 思路:easy先想到一个好的枚举方式:以当前柱状为扩展点,往左边和右边扩展.当遇到一个比当前柱状小的柱状时停止扩展.以当前柱状的高度为矩形的高.向左右扩展的距离之差为矩形的长度,这样对n个柱状进行扫描之后可得最大

[poj 2796]单调栈

题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include<algorithm> #include<stack> using namespace std; const int maxn=100005; int a[maxn]; int l[maxn]; int r[maxn]; long long pre[maxn]; stack< p

POJ2082---Terrible Sets(单调栈)

Description Let N be the set of all natural numbers {0 , 1 , 2 , - }, and R be the set of all real numbers. wi, hi for i = 1 - n are some elements in N, and w0 = 0. Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 &

POJ 2796 Feel Good(单调栈)

题目地址:POJ 2796 单调栈的第一题就是这道..把我弄的晕头转向.现在终于明白了,对单调栈又加深了理解.原来单调栈不只是可以维护数. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #incl