1. 程式人生 > >POJ 2559 Largest Rectangle in a Histogram(單調棧)

POJ 2559 Largest Rectangle in a Histogram(單調棧)

common pst locale str flow bold text function target

【題目鏈接】: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 */





POJ 2559 Largest Rectangle in a Histogram(單調棧)