Hang Dian 1506 questions.

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

Question meaning: Given many rectangles, the width of each rectangle has been specified as 1, and the height is the input h[i]. We are required to find the rectangle with the largest area! ! Find the area. That is, find the largest area of ​​the rectangles that are close together.

The first thing that is easy to think of is to ask directly, but when the data is too large, violence cannot solve it! ! Therefore, referring to other people's methods, you can follow:

       (For each piece of wood, Area=height[i]*(j-k+1) Among them, j<=x<=k, height[x]>=height[i]; find j and k as the key, the general method It must be timed out. Using dynamic programming, if the height on its left is greater than or equal to itself, then the left boundary on its left must meet this property, and then iterate from the left of this boundary
    for(i=1;i<=n;i++)
        {            
            while (a[l[i]-1]>=a[i])
                l[i]=l[l[i]-1];
                
        }
    
    for(i=n;i>=1;i--)
        {
            while (a[r[i]+1]>=a[i])
                r[i]=r[r[i]+1];
        }

This approach is to find the heights of the rectangles that are not leaning in and replace them in turn, and finally determine the final area of ​​the rest. .

Then the code is simple:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100005;
__int64  h[maxn],l[maxn],r[maxn];
int main()
{
    __int64  i,j,n;
    while(scanf("%I64d",&n)!=EOF)
    {
       // memset(l,0,sizeof(l));
       // memset(r,0,sizeof(r));
        if(n==0)break;
        for(i=1;i<=n;i++)
        {
            scanf("%I64d",&h[i]);
            l[i]=i;
            r[i]=i;
        }
       // int x=0,y=0;
        for(i=1;i>=n;i++)
        {
           // l[i]=i;
            while(h[l[i]-1]>=h[i]&&l[i]>1)
                l[i]=l[l[i]-1];
        }
        for(j=n;j>=1;j--)
        {
           // r[j]=j;
            while(h[r[j]+1]>=h[j]&&r[j]<n)
               r[j]=r[r[j]+1];
        }
        __int64 sum=-10000,t;
        for(i=1;i<=n;i++)
        {
           t=(r[i]-l[i]+1)*h[i];
           if(sum<t)
                sum=t;
        }
       //rintf("%d");
       //cout<<sum<<endl;
       printf("%I64d\n",sum);
    }
    return 0;
}


 
 

Guess you like

Origin blog.csdn.net/u010200793/article/details/16119365