City Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5970 Accepted Submission(s): 2563
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.
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.
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
此题为1506的加强版,重点在于将二维矩阵的求最大转化为一维的,然后就自然而然转化为1506。
关键点:一是预处理,巧妙地层层叠加,为下一步DP打下了良好的基础。
二是读入字符时,原题的输入数据略恶心,两个字符之间未必只空一个空格,有可能有两个,为此WA两次。
解决方法是用%s读入字符串,滤去多余的空格或回车。
/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 10E9+7
/*---------------------Work-----------------------*/
int s[1050][1050];
int l[1050],r[1050];
void work()
{
int K; cin>>K;
for(int k=1;k<=K;k++)
{
int M,N;
char ch[10];
scanf("%d%d",&M,&N);
memset(s,0,sizeof(s));
for(int i=1;i<=M;i++)
{
for(int j=1;j<=N;j++)
{
//getchar();
scanf("%s",&ch); //WA两次 ,应用%s过滤掉多余的空格
//原来是使用%c和getchar(),只能过滤一个
if(ch[0]=='F') s[i][j]=s[i-1][j]+1;
else s[i][j]=0;
}
}
/*
for(int i=1;i<=M;i++)
{
for(int j=1;j<=N;j++)
printf("%d ",s[i][j]);
printf("\n");
}
*/
int Max=0;
for(int i=1;i<=M;i++)
{
l[1]=1,r[N]=N;
for(int j=2;j<=N;j++) //从小到大 ,注意顺序,正是因为此顺序才能顺利DP
{
int ans=j;
while(ans>1&&s[i][j]<=s[i][ans-1]) ans=l[ans-1]; //DP过程
l[j]=ans;
}
for(int j=N-1;j>=1;j--) //从大到小
{
int ans=j;
while(ans<N&&s[i][j]<=s[i][ans+1]) ans=r[ans+1];
r[j]=ans;
}
for(int j=1;j<=N;j++)
{
Max=max(Max,(r[j]-l[j]+1)*s[i][j]);
}
}
printf("%d\n",Max*3);
}
}
/*------------------Main Function------------------*/
int main()
{
//freopen("test.txt","r",stdin);
//freopen("cowtour.out","w",stdout);
//freopen("cowtour.in","r",stdin);
work();
return 0;
}
Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2020 Accepted Submission(s): 969
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
/*------------------Header Files------------------*/
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
/*------------------Definitions-------------------*/
#define LL long long
#define PI acos(-1.0)
#define INF 0x3F3F3F3F
#define MOD 10E9+7
/*---------------------Work-----------------------*/
char mar[1050][1050];
int maze[1050][1050];
int l[1050],r[1050];
int Max,m,n;
void solve()
{
for(int i=1; i<=m; i++)
{
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
l[1]=1,r[n]=n;
for(int j=2; j<=n; j++)
{
int ans=j;
while(ans>1&&maze[i][j]<=maze[i][ans-1]) ans=l[ans-1];
l[j]=ans;
}
for(int j=n-1;j>=1;j--)
{
int ans=j;
while(ans<n&&maze[i][j]<=maze[i][ans+1]) ans=r[ans+1];
r[j]=ans;
}
for(int j=1;j<=n;j++)
Max=max(Max,(r[j]-l[j]+1)*maze[i][j]);
}
}
void work()
{
while(cin>>m>>n)
{
memset(maze,0,sizeof(maze));
for(int i=1; i<=m; i++)
{
getchar();
for(int j=1; j<=n; j++)
{
scanf("%c",&mar[i][j]);
if(mar[i][j]=='a'||mar[i][j]=='y'||mar[i][j]=='z'||mar[i][j]=='w') maze[i][j]=maze[i-1][j]+1;
else maze[i][j]=0;
}
}
Max=-INF;
solve();
memset(maze,0,sizeof(maze));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
if(mar[i][j]=='b'||mar[i][j]=='w'||mar[i][j]=='x'||mar[i][j]=='z') maze[i][j]=maze[i-1][j]+1;
else maze[i][j]=0;
}
solve();
memset(maze,0,sizeof(maze));
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
if(mar[i][j]=='c'||mar[i][j]=='y'||mar[i][j]=='x'||mar[i][j]=='z') maze[i][j]=maze[i-1][j]+1;
else maze[i][j]=0;
}
solve();
printf("%d\n",Max);
}
}
/*------------------Main Function------------------*/
int main()
{
//freopen("test.txt","r",stdin);
//freopen("cowtour.out","w",stdout);
//freopen("cowtour.in","r",stdin);
work();
return 0;
}