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 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.

Sample Input

7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0

Sample Output

8
4000

【解题思路】:

建立一个单调递增栈,所有元素各进栈和出栈一次,每次出栈的时候更新一下最大值

代码:

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
#define rep(i,j,k) for(int i=j;i<k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)

#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef unsigned long long LLU;
typedef double db;

const int N=2*1e5+10;
int n,m,top;
int  num[N],W[N],H[N];

char str[N];
bool vis[N];

int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

struct node      ///定义栈的结构体,高度:宽度
{
    int height;
    int width;
};
node Dull_Stack[N];

int main()
{
    LL ans,tot,tmp,Max_area;
    while(scanf("%d",&n)!=EOF&&n)
    {
        ans=0;
        top=0;
        rep(i,0,n)
        {
            scanf("%d",&m);
            tmp=0;
            while(top>0&&Dull_Stack[top-1].height>=m)///(2,1) (1,2)的情况
            {
                tot=Dull_Stack[top-1].height*(Dull_Stack[top-1].width+tmp);///更新宽度
                //ans=max(tot,ans);
                if(tot>ans) ans=tot;
                tmp+=Dull_Stack[top-1].width;
                --top;
                /*
                printf("Dull_Stack[top-1].height= %lld\n",Dull_Stack[top-1].height);
                printf("Dull_Stack[top-1].width= %lld\n",Dull_Stack[top-1].width);
                printf("ans= %lld\n",ans);
                printf("tot= %lld\n",tot);
                */
            }
            Dull_Stack[top].height=m;///继续输入
            Dull_Stack[top].width=tmp+1;
            ++top;
        }
        /*
         printf("tot=%lld\n",tot);
          printf("ans=%lld\n",ans);
          */
        tmp=0;
        while(top>0)
        {
            Max_area=Dull_Stack[top-1].height*(Dull_Stack[top-1].width+tmp);
            //ans=max(Max_area,ans);
            if(Max_area>ans) ans=Max_area;
            tmp+=Dull_Stack[top-1].width;
            ///printf("%lld\n",Max_area);
            --top;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
/*
Sample Input
7
2 1 4 5 1 3 3
4
1000 1000 1000 1000
0
Sample Output
8
4000
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-23 21:57:39

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

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 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

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

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

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi