zoukankan      html  css  js  c++  java
  • 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 ",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 */

    
    




  • 相关阅读:
    B. Gerald is into Art
    day 56 Django介绍
    day 55 bootstrap应用
    Python学习之变量的作用域
    day 54 JQ用法
    day 53 BOM与DOM
    day 52 js语法
    day 51 label标签,文字属性,背景属性,边框,display属性,盒模型,浮动,overflow,定位,z-index和模态框
    day 50 form表单,input框,textarea,select,css的引入方式,标签嵌套,选择器,宽高属性,字体属性
    day 49 html标签介绍,标题、段落、a、img、表格、特殊符号、常用标签、div、span
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7137220.html
Copyright © 2011-2022 走看看