扫描线 hdu 1506 hdu1505 hdu2870

 hdu1506     Largest Rectangle in a Histogram

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
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
__int64 high[100010];
int l[100010],r[100010];
int main()
{
    int n,i,j;
    LL ans;
    while(scanf("%d",&n)&&n)
    {
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&high[i]);
            l[i]=1;
            r[i]=1;
        }
        for(i=1;i<n;i++)
        {
           int s=i-1;
           while(high[i]<=high[s]&&s>=0)
           {
               l[i]+=l[s];
               s-=l[s];
           }
        }
        for(i=n-2;i>=0;i--)
        {
            int s=i+1;
            while(high[i]<=high[s]&&s<=n-1)
            {
                r[i]+=r[s];
                s+=r[s];
            }
        }
        ans=0;
        for(i=0;i<n;i++)
        {

            if(high[i]*(l[i]+r[i]+-1)>ans)
                ans=high[i]*(l[i]+r[i]+-1);
        }
        printf("%I64d\n",ans);
        //cout<<ans<<endl;
    }
    return 0;
}

hdu 1505       City Game
Problem Description
Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.

Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$.

Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.
 

Input
The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are:

R – reserved unit

F – free unit

In the end of each area description there is a separating line.
 

Output
For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.
 

Sample Input
   
   
2 5 6 R F F F F F F F F F F F R R R F F F F F F F F F F F F F F F 5 5 R R R R R R R R R R R R R R R R R R R R R R R R R
 

Sample Output
   
   
45 0
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1010][1010];
int l[1010],r[1010];
char Map[1010][1010];
int main()
{
    int t,n,m;
    int sum;
    int i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d %d",&n,&m);
        for( i=1; i<=n; i++)
            for( j=1; j<=m; j++)
            {
                char ch = getchar();
                while(ch != 'R' && ch !='F')
                {
                    ch = getchar();
                }
                Map[i][j] = ch;
                if(Map[i][j]=='R')
                    a[i][j]=0;
                else
                    a[i][j]=a[i-1][j]+1;
            }
        sum=-1;
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                    l[j]=1;
                    r[j]=1;
            }
            for(j=2; j<=m; j++)
            {
                k=j-1;
                while(a[i][j]<=a[i][k]&&k>=1)
                {
                    l[j]+=l[k];
                    k-=l[k];
                }
            }
            for(j=m-1; j>=1; j--)
            {
                k=j+1;
                while(a[i][j]<=a[i][k]&&k<=m)
                {
                    r[j]+=r[k];
                    k+=r[k];
                }
            }
            for( j=1; j<=m; j++)
            {
                int res=a[i][j]*(r[j]+l[j]-1);
                if(sum<res)
                    sum=res;
            }
        }
        printf("%d\n",3*sum);
    }
    return 0;
}

hdu 2870    Largest Submatrix

Problem Description
Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change 'w' to 'a' or 'b', change 'x' to 'b' or 'c', change 'y' to 'a' or 'c', and change 'z' to 'a', 'b' or 'c'. After you changed it, what's the largest submatrix with the same letters you can make?
 

Input
The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 1000) on line. Then come the elements of a matrix in row-major order on m lines each with n letters. The input ends once EOF is met.
 

Output
For each test case, output one line containing the number of elements of the largest submatrix of all same letters.
 

Sample Input
   
   
2 4 abcw wxyz
 

Sample Output
   
   
3
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char str[1010][1010];
int a[1010][1010];
int l[1010],r[1010];
int n,m;
int sum;
void dp()
{
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<m; j++)
        {
            l[j]=1;
            r[j]=1;
        }
        for(int j=1; j<m; j++)
        {
            int k=j-1;
            while(a[i][j]<=a[i][k]&&k>=0)
            {
                l[j]+=l[k];
                k-=l[k];
            }
        }
        for(int j=m-2; j>=0; j--)
        {
            int k=j+1;
            while(a[i][j]<=a[i][k]&&k<m)
            {
                r[j]+=r[k];
                k+=r[k];
            }
        }
        for(int j=0; j<m; j++)
        {
            int temp=a[i][j]*(l[j]+r[j]-1);
            if(temp>sum)
                sum=temp;
        }
    }
}
int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        sum=-1;
        memset(a,0,sizeof(a));
        getchar();
        for(int i=1; i<=n; i++)
            scanf("%s",str[i]);
        for(int i=1; i<=n; i++)
            for(int j=0; j<m; j++)
            {
                if(str[i][j]=='a'||str[i][j]=='w'||str[i][j]=='y'||str[i][j]=='z')
                    a[i][j]=a[i-1][j]+1;
                else
                    a[i][j]=0;
            }
        dp();
        memset(a,0,sizeof(a));
        for(int i=1; i<=n; i++)
            for(int j=0; j<m; j++)
            {
                if(str[i][j]=='b'||str[i][j]=='w'||str[i][j]=='x'||str[i][j]=='z')
                    a[i][j]=a[i-1][j]+1;
                else
                    a[i][j]=0;
            }
        dp();
        memset(a,0,sizeof(a));
        for(int i=1; i<=n; i++)
            for(int j=0; j<m; j++)
            {
                if(str[i][j]=='c'||str[i][j]=='x'||str[i][j]=='y'||str[i][j]=='z')
                    a[i][j]=a[i-1][j]+1;
                else
                    a[i][j]=0;
            }
        dp();
        printf("%d\n",sum);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值