hdu 1506,1505,2870

来源:互联网 发布:如何强制关掉mac软件 编辑:程序博客网 时间:2024/06/03 22:41

1505是1506的加强, 2870是1505的加强!

1506如下:

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10850    Accepted Submission(s): 2964


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.


Largest Rectangle http://acm.hdu.edu.cn/showproblem.php?pid=1506
    对于每一块木板,Area=height[i]*(j-k+1)  其中,j<=x<=k,height[x]>=height[i];找j,k成为关键,一般方法肯定超时,利用动态规划,如果它左边高度大于等于它本身,那么它左边的左边界一定满足这个性质,再从这个边界的左边迭代下去
    
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];


故代码如下:

#include <iostream>#include<cstdio>#include<cstring>#define M 100000using namespace std;__int64 a[M+5];int l[M+5],r[M+5];int main(){    int n;    while(~scanf("%d",&n) && n)    {        for(int i=1;i<=n;i++)            scanf("%I64d",&a[i]);        a[0]=a[n+1]=-1;        for(int i=1;i<=n;i++)        {            l[i]=i;            while(a[l[i]-1]>=a[i])                l[i]=l[l[i]-1];        }        for(int i=n;i>=1;i--)        {            r[i]=i;            while(a[r[i]+1]>=a[i])                r[i]=r[r[i]+1];        }        __int64 tmp,ans=0;        for(int i=1;i<=n;i++)        {            tmp=(r[i]-l[i]+1)*a[i];            if(tmp>ans) ans=tmp;        }        printf("%I64d\n",ans);    }    return 0;}



1505 就是说在平地上建个矩形建筑,平地中间会有 东西隔断。


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
25 6R F F F F FF F F F F FR R R F F FF F F F F FF F F F F F5 5R R R R RR R R R RR R R R RR R R R RR R R R R
 

Sample Output
450
 

先把高度up 统计一下,把2维转换化成以每一行底,然后再按1506 dp一下,

代码:

#include <iostream>#include<cstdio>#include<cstring>#define M 1000using namespace std;int up[M+5][M+5];int l[M+5][M+5],r[M+5][M+5];int main(){    int T,n,m;    char s[10];    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&m,&n);        for(int i=1;i<=m;i++)            for(int j=1;j<=n;j++)                {                    scanf("%s",s);                    if(s[0]=='F') up[i][j]=up[i-1][j]+1;                    else up[i][j]=0;  // 高度                }        for(int i=1;i<=m;i++)        {           up[i][0]=up[i][n+1]=-1;            for(int j=1;j<=n;j++)            {                l[i][j]=j;                while(up[i][l[i][j]-1]>=up[i][j])                    l[i][j]=l[i][l[i][j]-1];            }            for(int j=n;j>=1;j--)            {                r[i][j]=j;                while(up[i][r[i][j]+1]>=up[i][j])                    r[i][j]=r[i][r[i][j]+1];            }        }        int ans=0,tmp;        for(int i=1;i<=m;i++)            for(int j=1;j<=n;j++)                {                    tmp=up[i][j]*(r[i][j]-l[i][j]+1);                    if(tmp>ans) ans=tmp;                }        printf("%d\n",ans*3);    }    return 0;}


最后,再加强版:

这道题的大体意思是求最大矩形面积(由相同字母组成的最大矩形);

Largest Submatrix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1452    Accepted Submission(s): 696


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 4abcwwxyz
枚举+dp

代码:

#include <iostream>#include<cstdio>#include<cstring>#define M 1000using namespace std;int a[M+5][M+5],b[M+5][M+5];int up[M+5][M+5];int l[M+5][M+5],r[M+5][M+5];int ans;void fun(int m,int n){    for(int i=1;i<=m;i++)        {           up[i][0]=up[i][n+1]=-1;            for(int j=1;j<=n;j++)            {                l[i][j]=j;                while(up[i][l[i][j]-1]>=up[i][j])                    l[i][j]=l[i][l[i][j]-1];            }            for(int j=n;j>=1;j--)            {                r[i][j]=j;                while(up[i][r[i][j]+1]>=up[i][j])                    r[i][j]=r[i][r[i][j]+1];            }        }        int tmp;        for(int i=1;i<=m;i++)            for(int j=1;j<=n;j++)                {                    tmp=up[i][j]*(r[i][j]-l[i][j]+1);                    if(tmp>ans) ans=tmp;                }}int main(){    int T,n,m;    char s[M+5][M+5];    while(~scanf("%d%d",&m,&n))    {        ans=0;        for(int i=1;i<=m;i++)                scanf("%s",s[i]);        // 'a'        for(int i=1;i<=m;i++)            for(int j=0;j<n;j++)            {                   if(s[i][j]=='a' || s[i][j]=='w' || s[i][j]=='y' || s[i][j]=='z')                    up[i][j+1]=up[i-1][j+1]+1;                    else up[i][j+1]=0;            }        fun(m,n);        // 'b'        for(int i=1;i<=m;i++)            for(int j=0;j<n;j++)            {                   if(s[i][j]=='b' || s[i][j]=='w' || s[i][j]=='x' || s[i][j]=='z')                    up[i][j+1]=up[i-1][j+1]+1;                    else up[i][j+1]=0;            }        fun(m,n);        // 'c'        for(int i=1;i<=m;i++)            for(int j=0;j<n;j++)            {                   if(s[i][j]=='c' || s[i][j]=='x' || s[i][j]=='y' || s[i][j]=='z')                    up[i][j+1]=up[i-1][j+1]+1;                    else up[i][j+1]=0;            }        fun(m,n);        printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击