【DP+预处理|最长连续子序列】HDU-1505 City Game
来源:互联网 发布:染色体分析软件 编辑:程序博客网 时间:2024/06/01 12:34
City Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
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
————————————————————————————————————————————————————————
题意:给出一个01矩阵,求其中全部都是1的最大的子矩阵。
思路:
这其实就是二维的最长连续子序列问题。一维的这儿有:http://blog.csdn.net/j_sure/article/details/41719789
需要降维,在O(n^2)内解决。
如果只有一行,显然可以直接预处理出该行所有点左边界、右边界。
n行的话,还需要知道高。这也没问题,预处理出上边界即可,方法是,如果是1,那么加上上一行的值。如果不是,重新从0开始计起。
i 从1到n,对于前i行,这就是一个一维的最长连续子序列而已,只不过每一列的高度在不断增加而已。
代码如下:
/** * ID: j.sure.1 * PROG: * LANG: C++ */#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define For(i, x, y) for(int i=x; i<=y; i++)#define For_(i, x, y) for(int i=x; i>=y; i--)#define Mem(f, x) memset(f, x, sizeof(f))#define Sca(x) scanf("%d", &x)#define Pri(x) printf("%d\n", x)#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 1111;int n, m, best;int l[N], r[N], u[N][N];int main(){#ifdef J_Sure freopen("000.in", "r", stdin); //freopen("999.out", "w", stdout);#endif int T; Sca(T); while(T--) { scanf("%d%d", &n, &m); char c; For(i, 1, n) { For(j, 1, m) { scanf(" %c", &c); u[i][j] = c == 'F' ? u[i-1][j]+1 : 0; } } best = 0; For(i, 1, n) { u[i][0] = u[i][m+1] = -1; For(j, 1, m) { l[j] = r[j] = j; while(u[i][j] <= u[i][l[j]-1]) { l[j] = l[l[j]-1]; } } For_(j, m, 1) { while(u[i][j] <= u[i][r[j]+1]) { r[j] = r[r[j]+1]; } } For(j, 1, m) { int cur = (r[j] - l[j] + 1)*u[i][j]; best = max(best, cur); } } printf("%d\n", 3*best); } return 0;}
0 0
- 【DP+预处理|最长连续子序列】HDU-1505 City Game
- 【DP+预处理|最长连续子序列】HDU-2870 Largest Submatrix
- 【DP+预处理|最长连续子序列】HDU-1506 Largest Rectangle in a Histogram
- 最长连续子序列 DP
- HDU 1505 City Game-dp-(最大子矩阵模型)
- hdu 1505 City Game 简单DP 最大子矩阵 迭代法
- hdu 1505 dp City Game
- HDU 1505 City Game (DP)
- hdu 1505 City Game(DP)
- hdu 1505 City Game(DP)
- hdu 1505 City Game dp
- HDU 1505 City Game(DP)
- hdu 1505 City Game dp
- HDU 1505 City Game(DP)
- HDU 1505 City Game dp
- 最长公共子序列 最长递增子序列(和) 最长递增公共子序列 最长(连续)子序列乘积(HDU)--dp
- poj1458(DP最长连续公共子序列)
- hdu1003最长连续子序列和dp
- eclipse中的java文件图标变成成空心j
- java反射的学习
- Mybatis入门实例(二)
- POJ C Looooops
- 判断网络是否可用
- 【DP+预处理|最长连续子序列】HDU-1505 City Game
- CUDA矩阵乘法——VS2010中使用CUDA示例
- dos下查看端口是否被占用,及80端口被System进程占用的解决方法
- Java DateTime 日期时间处理
- 基本的socket函数
- Apache POI组件操作Excel,制作报表(二)
- cuda向量加法时间
- hdu 5119 ^和>=m情况数+dp
- [leetcode]Convert Sorted Array to Binary Search Tree