HDOJ2870 Largest Submatrix
一道\(DP\)
原题链接
发现只有\(a,b,c\)三种情况,所以直接初始化成三个\(01\)方阵,找最大子矩阵即可。
我是先初始化垂直上的高度,然后对每一行处理出每个点向左向右的最大延伸,并不断计算矩阵大小来更新答案。
因为不想开函数传数组,所以全写在主函数复制粘贴了三遍。。代码显得比较冗长。
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1010;
int A[N][N], B[N][N], C[N][N], l[N], r[N];
char re_l()
{
char c = getchar();
for (; c != 'a'&&c != 'b'&&c != 'c'&&c != 'x'&&c != 'y'&&c != 'z'&&c != 'w'; c = getchar());
return c;
}
inline int maxn(int x, int y)
{
return x > y ? x : y;
}
int main()
{
int i, j, ma, n, m;
char c;
while (scanf("%d%d", &n, &m)==2)
{
ma = 1;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(C, 0, sizeof(C));
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
{
c = re_l();
if (c == 'a' || c == 'w' || c == 'y' || c == 'z')
A[i][j] = A[i - 1][j] + 1;
if (c == 'b' || c == 'w' || c == 'x' || c == 'z')
B[i][j] = B[i - 1][j] + 1;
if (c == 'c' || c == 'x' || c == 'y' || c == 'z')
C[i][j] = C[i - 1][j] + 1;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
l[j] = r[j] = j;
A[i][0] = A[i][m + 1] = -1;
for (j = 1; j <= m; j++)
while (A[i][j] <= A[i][l[j] - 1])
l[j] = l[l[j] - 1];
for (j = m; j; j--)
while (A[i][j] <= A[i][r[j] + 1])
r[j] = r[r[j] + 1];
for (j = 1; j <= m; j++)
ma = maxn(ma, (r[j] - l[j] + 1)*A[i][j]);
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
l[j] = r[j] = j;
B[i][0] = B[i][m + 1] = -1;
for (j = 1; j <= m; j++)
while (B[i][j] <= B[i][l[j] - 1])
l[j] = l[l[j] - 1];
for (j = m; j; j--)
while (B[i][j] <= B[i][r[j] + 1])
r[j] = r[r[j] + 1];
for (j = 1; j <= m; j++)
ma = maxn(ma, (r[j] - l[j] + 1)*B[i][j]);
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
l[j] = r[j] = j;
C[i][0] = C[i][m + 1] = -1;
for (j = 1; j <= m; j++)
while (C[i][j] <= C[i][l[j] - 1])
l[j] = l[l[j] - 1];
for (j = m; j; j--)
while (C[i][j] <= C[i][r[j] + 1])
r[j] = r[r[j] + 1];
for (j = 1; j <= m; j++)
ma = maxn(ma, (r[j] - l[j] + 1)*C[i][j]);
}
printf("%d\n", ma);
}
return 0;
}
HDOJ2870 Largest Submatrix的更多相关文章
- Largest Submatrix(动态规划)
Largest Submatrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- POJ-3494 Largest Submatrix of All 1’s (单调栈)
Largest Submatrix of All 1’s Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8551 Ac ...
- hdu 2870 Largest Submatrix(平面直方图的最大面积 变形)
Problem Description Now here is a matrix with letter 'a','b','c','w','x','y','z' and you can change ...
- Largest Submatrix of All 1’s
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we m ...
- codeforces 407D Largest Submatrix 3
codeforces 407D Largest Submatrix 3 题意 找出最大子矩阵,须满足矩阵内的元素互不相等. 题解 官方做法 http://codeforces.com/blog/ent ...
- Largest Submatrix of All 1’s(思维+单调栈)
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1's which is the largest? By largest we m ...
- POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈
POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...
- POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵
Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...
- HDU 2870 Largest Submatrix (单调栈)
http://acm.hdu.edu.cn/showproblem.php? pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- Pandas数据排序
Pandas数据排序 .sort_index() 在指定轴上根据索引进行排序,索引排序后内容会跟随排序 b = pd.DataFrame(np.arange(20).reshape(4,5),inde ...
- HBuilder在MAC下的SVN
1. 安装svn插件,工具--插件安装---svn--安装 2. 使用svn,文件---导入---从svn检出项目 -- 下一步 3. 创建新的资源库位置--下一步-- URL(输入svn仓库地址)- ...
- GitLab 汉化
汉化Gitlab Gitlab默认语言是英文,对于想加强英文的同学,建议继续使用英文,但要求使用中文,这里需要下载一个汉化包下载最新的汉化包: 下载最新的汉化包: [root@gitlab ~]# g ...
- Realtime Rendering 1.1
[Realtime Rendering 1.1] 1.A linear transform is one that preserves vector addition and scalar multi ...
- 《xss跨站脚本剖析与防御》实验笔记
1.书籍<xss跨站脚本剖析与防御>上介绍的xss测试代码 <img src="javascrpt:alert('xss');">, <table b ...
- angularjs 粘贴事件
参考 http://www.jb51.net/article/89708.htm ng-paste 需要setTimeout,否则无法获取到数据
- cetnos 7 增加新的硬盘
fdisk -l 查看新的硬盘是否挂载 如没有挂载 ls /sys/class/scsi_host/ 查看设备列表 echo "- - - " > /sys/class/sc ...
- Websocket实现群聊、单聊
Websocket 使用的第三方模块:gevent-websocket 群聊 ws群聊.py中的内容 from flask import Flask, request, render_template ...
- Shell教程 之数组
1.定义数组 数组中可以存放多个值.Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似). 与大部分编程语言类似,数组元素的下标由0开始. Shell ...
- PHP中的变量与PHP中算false的情况
一PHP中的变量 1.PHP中的变量,声明与使用必须用$开头 2.PHP是一种弱类型语言,变量其实并不需要声明.可以直接给变量赋任何类型的值: 3.PHP中可以使用连等同时声明多个变量, eg:num ...