zoukankan      html  css  js  c++  java
  • POJ 2559 Langest Rectangle in a Histogame

    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, where0<=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
    

    Hint

    Huge input, scanf is recommended.
    题解:
    给你N个宽都是一,长为a[[i]的矩形,然后让你求连续的几个矩形形成的最大面积(看图好理解题意);
    对于这道题目,我们可以假设每一个矩形为最低的那个,然后依次往两边找,直到找到 比当前低的位置,
    对于这些位置我们可以预处理存在l[i],r[i]数组里面;然后求最大值即可;
    参考代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdlib>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<deque>
     9 #include<stack>
    10 #include<set>
    11 #include<vector>
    12 #include<map>
    13 using namespace std;
    14 typedef long long LL;
    15 const int maxn=1e5+10;
    16 int a[maxn],l[maxn],r[maxn];
    17 int main()
    18 {
    19     int n;
    20     while(scanf("%d",&n)!=EOF&&n)
    21     {
    22         memset(a,0,sizeof(a));
    23         for(int i=0;i<n;i++) scanf("%d",&a[i]);
    24         for(int i=0,j=n-1;i<n;i++,j--)
    25         {
    26             l[i]=i;r[j]=j;
    27             while(l[i]>0&&a[l[i]-1]>=a[i]) l[i]=l[l[i]-1];
    28             while(r[j]<n-1&&a[r[j]+1]>=a[j]) r[j]=r[r[j]+1];
    29         }    
    30         long long max=0,m;
    31         for(int i=0;i<n;i++)
    32         {
    33             m=(long long)(r[i]-l[i]+1)*a[i];
    34             if(max<m) max=m;
    35         }
    36         cout<<max<<endl;
    37     }
    38     return 0;
    39 }
    View Code
  • 相关阅读:
    delphi的dbgrid控件点击title排序
    在 Delphi 中使用微软全文翻译的小例子
    Google的搜索API的Delphi封装
    Delphi 自动检测U盘插入、拔出及获取U盘盘符!
    ERP开发准备
    Delphi中TStringList类常用属性方法详解
    JavaScript prototype详解
    TestNG 自动化测试入门教程--典型示例
    ucloud中的udisk错误“Read-only file system”修复指南
    websocket(二)--简单实现网页版群聊
  • 原文地址:https://www.cnblogs.com/csushl/p/9520501.html
Copyright © 2011-2022 走看看