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

    
    




  • 相关阅读:
    一个先进的App框架:使用Ionic创建一个简单的APP
    Hexo NexT 博客本地搭建指南
    Spring Boot 2.0 入门指南
    1. 初识 Lucene
    Spring Framework 简介
    电脑软件推荐安装列表
    PHP 环境搭建篇
    C++ STL 容器之栈的使用
    0x02 译文:Windows桌面应用Win32第一个程序
    反射?切面?怎样对公共参数及行为进行封装
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7137220.html
Copyright © 2011-2022 走看看