Largest Rectangle in a Histogram--(单调队列orDP)

Largest Rectangle in a Histogram

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 10
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

给出一系列高度,宽为1,要求求一个最大矩形面积

可以用队列求出h[i]左边和右边连续比它高的个数,设计一个升序的队列,当循环到h[i]时比他高的出队,求出它与队尾元素下标只差既是所求个数,

DP方法时,求出h[i]左边(右边)连续比他高的元素的最左(右)下标,

注意要用long long才行

代码(单调队列):

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define M 100005
int n;
long long int q[M],h[M],l[M],r[M];
void get_left()
{
    int i,head=1,tail=1;
    q[1]=0;  //队列中初始化一个元素,方便运算
    for(i=1;i<=n;i++)
    {
        while(head<=tail&&h[q[tail]]>=h[i]) tail--;  //队中高度大于等于h[i]元素出队
        l[i]=i-q[tail]-1;
        q[++tail]=i;
    }
}
void get_right()
{
    int i,head=1,tail=1;
    q[1]=n+1;
    for(i=n;i>=1;i--)
    {
        while(head<=tail&&h[q[tail]]>=h[i]) tail--;
        r[i]=q[tail]-i-1;
        q[++tail]=i;
    }
}
void get_max()
{
    int i;
    long long int cnt=0,sum=0;
    for(i=1;i<=n;i++)
    {
        sum=(l[i]+r[i]+1)*h[i];
        if(sum>cnt) cnt=sum;
    }
    cout<

代码(dp):

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define M 100005
int n;
int l[M],r[M];
long long int h[M];
void get_left()
{
    int i,tt;
    l[1]=1;
    for(i=2;i<=n;i++)
    {
        tt=i;
        while(tt>1&&h[i]<=h[tt-1]) //如果要入队的元素比当前找到的最左元素的左边一个要矮,最左元素更新
            {tt=l[tt-1];}
        l[i]=tt;  
    }
}
void get_right()
{
    int i,tt;
    r[n]=n;
    for(i=n-1;i>=1;i--)
    {
        tt=i;
        while(ttcnt) cnt=sum;
    }
    cout<



 
  

你可能感兴趣的:(2017暑假集训题目,二分和单调队列)