POJ 2559 Largest Rectanglen a Higogram(単調スタック)


【タイトルリンク】:click here~~
【タイトルの大意】:
A histogram is a polygon coposed of a sequence of rectangles aligned aa common base line.The rectangles have equal widths but may have different heights.Foxxample,the figure on the ft the shshshshshatototototoshshshshshshatotototoshshshshshshshshshshatotototototototoshshshshshshshshles s s shshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshshleles、the shshattttttis the width of the rectangles: 
Usually,histograms ared to represent discrete distributions,e.g.the frequencies of characters.Note the order of the rectangles,i.e.their height,is importer.Calcuthelatte of thelant lacture of the morrable.too.The figure on the right shows the larget 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 
#endif

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif

// C++
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif

using namespace std;
#define rep(i,j,k) for(int i=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
",Dull_Stack[top-1].height); printf("Dull_Stack[top-1].width= %lld
",Dull_Stack[top-1].width); printf("ans= %lld
",ans); printf("tot= %lld
",tot); */ } Dull_Stack[top].height=m;/// Dull_Stack[top].width=tmp+1; ++top; } /* printf("tot=%lld
",tot); printf("ans=%lld
",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
",Max_area); --top; } printf("%lld
",ans); } return 0; } /* Sample Input 7 2 1 4 5 1 3 3 4 1000 1000 1000 1000 0 Sample Output 8 4000 */