zoukankan      html  css  js  c++  java
  • Largest Rectangle in a Histogram(附上几组测试数据)

    Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 23458    Accepted Submission(s): 7362


    Problem 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 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.
     
    Input
    The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.
     
    Output
    For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
     
    Sample Input
    7 2 1 4 5 1 3 3
    4 1000 1000 1000 1000
    0
     
    Sample Output
    8
    4000
     
    Source

    单调栈经典题

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<string>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<queue>
     9 #include<stack>
    10 using namespace std;
    11 
    12 struct sair{
    13     long long v;
    14     int pos;
    15 }a[100005];
    16 
    17 int main(){
    18     std::ios::sync_with_stdio(false);
    19     int n;
    20     while(cin>>n){
    21         if(!n) break;
    22         for(int i=1;i<=n;i++){
    23             cin>>a[i].v;
    24             a[i].pos=i;
    25         }
    26         stack<sair>st;
    27         long long ans=0;
    28         long long cnt,pos;
    29         for(int i=1;i<=n;i++){
    30             if(st.empty()){
    31                 st.push(a[i]);
    32                 ans=max(ans,st.top().v);
    33             }
    34             else{
    35                 while(!st.empty()&&st.top().v>=a[i].v){
    36                     cnt=st.top().v;
    37                     pos=st.top().pos;
    38                     st.pop();
    39                     if(st.empty()){
    40                         ans=max(ans,cnt*(i-1));
    41                     }
    42                     else{
    43                         ans=max(ans,cnt*(i-1-st.top().pos));
    44                     }
    45                 }
    46                 st.push(a[i]);
    47             }
    48         }
    49         int Last;
    50         if(!st.empty()){
    51             Last=st.top().pos;
    52         }   
    53         while(!st.empty()){
    54             cnt=st.top().v;
    55             pos=st.top().pos;
    56             st.pop();
    57             if(st.empty()){
    58                 ans=max(ans,cnt*n);;
    59             }
    60             else{
    61                 ans=max(ans,cnt*(Last-st.top().pos));
    62             }
    63         }
    64         cout<<ans<<endl;
    65     }
    66     system("pause");
    67 }
    68 /*
    69 6 2 5 2 5 5 2
    70 5 1 2 3 4 4
    71 7 2 1 4 5 1 3 3
    72 4 1000 1000 1000 1000
    73 3 2 1 2
    74 4 2 2 5 5
    75 */
    View Code
  • 相关阅读:
    银行卡号每隔4位插入空格
    IE6-8下自定义标签的表现
    Sql Server尝试读取或写入受保护的内存。这通常指示其他内存已损坏
    儿童编程教学scratch 3.0
    Shell 教程入门
    自定义vs2005代码段
    解决Adobe ReaderXI自动关闭问题
    WPF——给button添加背景图片
    WPF 异步加载数据
    Caliburn.Micro中的WindowManager
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/10037519.html
Copyright © 2011-2022 走看看