是这题的加强版
City Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4456 Accepted Submission(s): 1876
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
Source
这种算法使用来解决最大的一面积矩阵问题
1 1 1 1
0 0 1 0
1 1 0 1
1 1 0 0
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
int h[N][N];
int l[N][N];
int r[N][N];
int mp[N][N] = zero;
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
char ch[10];
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%s", ch);
if (ch[0] == 'F')
{
mp[i][j] = 1;
h[i][j] = h[i - 1][j] + 1;
}
else
{
mp[i][j] = 0;
h[i][j] = 0;
}
}
}
for (int i = 1; i <= n; i++)
{
l[i][1] = 1;
r[i][m] = m;
}
for (int i = 1; i <= n; i++)
{
for (int j = 2; j <= m; j++)
{
int t = j;
while (t > 1 && h[i][j] <= h[i][t - 1])
{
t = l[i][t - 1];
}
l[i][j] = t;
}
for (int j = m - 1; j >= 1; j--)
{
int t = j;
while (t < m && h[i][j] <= h[i][t + 1])
{
t = r[i][t + 1];
}
r[i][j] = t;
}
}
int maxn = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
int s = (r[i][j] - l[i][j] + 1) * h[i][j];
if (s > maxn)
maxn = s;
}
}
cout << 3 * maxn << endl;
}
return 0;
}
Largest Submatrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1447 Accepted Submission(s): 693
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
Source
加强版,分别构造a、b、c矩阵求最大
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
char mpc[N][N];
int n, m;
char sett[4][10] =
{
"awyz", "bwxz", "cxyz"
};
bool inset[4][300] = zero;
void init()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < strlen(sett[i]); j++)
{
inset[i][sett[i][j]] = 1;
}
}
}
int equal(char a, char b)
{
for (int i = 0; i < 3; i++)
{
if (inset[i][a] && inset[i][b])
return i;
}
return -1;
}
inline int get(char a)
{
int d = 0;
for (int i = 0; i < 3; i++)
{
if (inset[i][a])
d |= 1 << i;
}
return d;
}
int dp[N][N];
int mp1[N][N];
int mp2[N][N];
int mp3[N][N];
int h[N][N];
int l[N][N];
int r[N][N];
int solve(int mp[][N])
{
memset(h, 0, sizeof(h));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (mp[i - 1][j - 1])
{
h[i][j] = h[i - 1][j] + 1;
}
else
{
h[i][j] = 0;
}
}
}
for (int i = 1; i <= n; i++)
{
l[i][1] = 1;
r[i][m] = m;
}
for (int i = 1; i <= n; i++)
{
for (int j = 2; j <= m; j++)
{
int t = j;
while (t > 1 && h[i][j] <= h[i][t - 1])
{
t = l[i][t-1];
}
l[i][j] = t;
}
for (int j = m - 1; j >= 1; j--)
{
int t = j;
while (t < m && h[i][j] <= h[i][t + 1])
{
t = r[i][t+1];
}
r[i][j] = t;
}
}
int maxn = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
int s = (r[i][j] - l[i][j] + 1) * h[i][j];
if (s > maxn)
maxn = s;
}
}
return maxn;
}
void output(int mp[][N])
{
printf("******************\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", mp[i][j]);
}
printf("\n");
}
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
init();
while (scanf("%d%d", &n, &m) + 1)
{
memset(mp1, 0, sizeof(mp1));
memset(mp2, 0, sizeof(mp2));
memset(mp3, 0, sizeof(mp3));
for (int i = 0; i < n; i++)
{
scanf("%s", mpc[i]);
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int d = get(mpc[i][j]);
mp1[i][j] = (d & (1)) ? 1 : 0;
mp2[i][j] = (d & (1 << 1)) ? 1 : 0;
mp3[i][j] = (d & (1 << 2)) ? 1 : 0;
}
}
// output(mp1);
// output(mp2);
// output(mp3);
printf("%d\n", max(max(solve(mp1), solve(mp2)), solve(mp3)));
}
return 0;
}
Matrix Swapping II
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1200 Accepted Submission(s): 795
Problem Description
Given an N * M matrix with each entry equal to 0 or 1. We can find some rectangles in the matrix whose entries are all 1, and we define the maximum area of such rectangle as this matrix’s goodness.
We can swap any two columns any times, and we are to make the goodness of the matrix as large as possible.
We can swap any two columns any times, and we are to make the goodness of the matrix as large as possible.
Input
There are several test cases in the input. The first line of each test case contains two integers N and M (1 ≤ N,M ≤ 1000). Then N lines follow, each contains M numbers (0 or 1), indicating the N * M matrix
Output
Output one line for each test case, indicating the maximum possible goodness.
Sample Input
3 4 1011 1001 0001 3 4 1010 1001 0001
Sample Output
4 2 Note: Huge Input, scanf() is recommended.
Source
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
char mp[N][N];
int mp01[N][N];
int num[N];
int row[N];
int cmp(int a, int b)
{
return a > b;
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int n, m;
while (scanf("%d%d", &n, &m) + 1)
{
memset(num, 0, sizeof(num));
for (int i = 0; i < n; i++)
{
scanf("%s", mp[i]);
}
int ans = -INF;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (mp[i][j] == '1')
{
num[j]++;
}
else
{
num[j] = 0;
}
row[j] = num[j];
}
sort(row, row + m, cmp);
int k=0;
for (int j = 1; j <= m; j++)
{
if (ans < row[j - 1]*j)
ans = row[j-1] * j;
}
}
printf("%d\n", ans);
}
return 0;
}