A job week5

Meaning of the questions:
to a histogram, find the largest rectangular area of the histogram. For example, the following image height in the histogram from left to right are 2, 1, 4, 5, 1, 3, 3, 1 they are wide, the largest rectangle is shaded.
Here Insert Picture Description
input:
Input comprising a plurality of sets of data. Each set of data is represented by an integer number n of small rectangular histogram, you can assume that 1 <= n <= 100000. then the next n integers h1, ..., hn, satisfying 0 <= hi <= 1000000000. These histogram from left to right the digital representation of each small rectangle of height, the width of each small rectangle is 1. Test data to 0 at the end.
output:
For each line the test data output represents an integer answer.
INPUT Sample:
. 7. 1 2. 5. 4. 3. 1. 3
. 4 1000 1000 1000 1000
0
Sample Output:
. 8
4000
ideas:
the input data, when the input n is 0, the program ends. Define two arrays store data. In one of the input when the data storage array. If at this time when the input data is larger than the first stack data array, the data will be stored in the stack, the memory array is also increased by one when the width. If less data than the data in the stack, if the matrix is formed on the area of the two are aligned, the final output value of the area of a large area matrix.
Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
int main()
{
    int n;
    while(cin>>n)
    {
 	if(n==0)
 	    break;
    int a[100050],w[100050];
    int stack[100050];
    int top=0;
    long long ans=0;
    memset(a,0,sizeof(a));
    for(int i=0;i<n;i++)
    {   	
 	    cin>>a[i];
    } 
    for(int i=0;i<=n;i++)
    {
        if(a[i]>stack[top])
        {
            stack[++top]=a[i];
            w[top]=1; 
        }
        else
        {
            int width=0;
            while(stack[top]>a[i])
            {
                width+=w[top];
                if(ans<(long long)width*stack[top])                    ans=(long long)width*stack[top];
                top--;
            }
            top++;
            stack[top]=a[i];
            w[top]=width+1;
        }
    }    cout<<ans<<endl;
    } 
    return 0;
 } 
Published 19 original articles · won praise 0 · Views 208

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/105328578