Matrix Swapping II

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1430    Accepted Submission(s): 950

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.
 
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
2009 Multi-University Training Contest 2
- Host by TJU




题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2830



题目大意:给一个0/1矩阵,能够随意交换当中的两列,求由1组成的最大子矩形的面积



题目分析:预处理出每一个点下方有多个连续的1即cnt[i][j]。对每行的cnt值从大到小排序。枚举列dp就可以,dp[i]表示以第i行为上边的矩形的面积最大值。转移方程:dp[i] = max(dp[i], j * cnt[i][j])

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1e3 + 5;
int const INF = 0x3fffffff;
char s[MAX][MAX];
int cnt[MAX][MAX];
int dp[MAX];
int n, m; bool cmp(int a, int b)
{
return a > b;
} int main()
{
while(scanf("%d %d", &n ,&m) != EOF)
{
memset(cnt, 0, sizeof(cnt));
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++)
scanf("%s", s[i] + 1);
for(int i = n; i >= 1; i--)
for(int j = 1; j <= m; j++)
if(s[i][j] - '0')
cnt[i][j] = cnt[i + 1][j] + 1;
for(int i = 1; i <= n; i++)
{
sort(cnt[i] + 1, cnt[i] + 1 + m, cmp);
for(int j = 1; j <= m; j++)
if(cnt[i][j])
dp[i] = max(dp[i], j * cnt[i][j]);
}
int ans = 0;
for(int i = 1; i <= n; i++)
ans = max(ans, dp[i]);
printf("%d\n", ans);
}
}

 

HDU 2830 Matrix Swapping II (预处理的线性dp)的更多相关文章

  1. HDu 2830 Matrix Swapping II(dp)

    Problem Description Given an N * M matrix with each entry equal to 0 or 1. We can find some rectangl ...

  2. HDU 2830 Matrix Swapping II

    给一个矩阵,依然是求满足条件的最大子矩阵 不过题目中说任意两列可以交换,这是对题目的简化 求出h数组以后直接排序,然后找出(col-j)*h[j]的最大值即可(这里的j是从0开始) 因为排序会影响到h ...

  3. hdu 2830 Matrix Swapping II(额,,排序?)

    题意: N*M的矩阵,每个格中不是0就是1. 可以任意交换某两列.最后得到一个新矩阵. 问可以得到的最大的子矩形面积是多少(这个子矩形必须全是1). 思路: 先统计,a[i][j]记录从第i行第j列格 ...

  4. 【HDOJ】2830 Matrix Swapping II

    简单DP. /* 2830 */ #include <iostream> #include <string> #include <map> #include < ...

  5. Matrix Swapping II(求矩阵最大面积,dp)

    Matrix Swapping II Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 2830:Matrix Swapping II(思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=2830 题意:-- 思路:对于每一列,它是固定的,用dp[][]处理出连续的长度.例如: 假设我们扫第四列的时候, ...

  7. [HDOJ2830]Matrix Swapping II(胡搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2830 给一个矩阵只有0和1,矩阵的列可以和其他列交换无数次,问交换后整个矩阵形成的最大的全是1的子矩阵 ...

  8. [ An Ac a Day ^_^ ] hdu 2830 矩阵交换II

    第一眼觉得是个dp 但是有了可以随意交换的条件觉得简单了不少 但是还是没做出来…… 看了一下别人的做法才觉得自愧不如 因为所有列都可以随意交换 应该尽量把长的放在一起 那么将所有的矩形排序之后 以第j ...

  9. HDU 2059 龟兔赛跑(超级经典的线性DP,找合适的j,使得每个i的状态都是最好的)

    龟兔赛跑 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

随机推荐

  1. 移动App服务端架构设计

      我从事手机app服务端开发现在已经是3个年头,自己也整理出了一套相对好用的服务架构,写出来,跟大家一起分享.如有不足,还请多指教. 一:基础流程图. 其实有一点还需要加上,就是对json的压缩和加 ...

  2. 【IDEA】(4)---很好用的DEBUG功能

    IDEA-DEBUG功能 一.常用快捷键 快捷键并不是完全一样的,我这边是MAC安装的IDEA, 这边最主要还是知道DEBUG时常用的功能. 1.快捷键 F7 #进入下一步,如果当前行是一个方法,则进 ...

  3. BZOJ 4057 状压DP

    思路: 状压一下 就完了... f[i]表示选了的集合为i 转移的时候判一判就好了.. //By SiriusRen #include <cstdio> #include <cstr ...

  4. BZOJ 3831 单调队列DP

    思路: 这好像是我刚学单调性的时候做的题 (我是不会告诉你 我被这题教做人了的...) i-stk[head]>k 删队头 f[stk[tail]]>f[i]||(f[stk[tail]] ...

  5. Django分页器及自定义分页器

    Django的分页器 view from django.shortcuts import render,HttpResponse # Create your views here. from app0 ...

  6. HBase、Hive、MapReduce、Hadoop、Spark 开发环境搭建后的一些步骤(export导出jar包方式 或 Ant 方式)

    步骤一 若是,不会HBase开发环境搭建的博文们,见我下面的这篇博客. HBase 开发环境搭建(Eclipse\MyEclipse + Maven) 步骤一里的,需要补充的.如下: 在项目名,右键, ...

  7. SQL server基本语法

    此处源于一个基本的SQL Server试题,基本上涵盖了SQL Server的全部基本语法,粘贴在此处,权当分享   --1.  创建TestDB数据库 create database TestDB; ...

  8. WEB笔记-5、字体和文本

    字体样式 font-family font-size font-weight font-style font-variant font(简写) 常用字体: serif,衬线字体,每个字符笔画末端会有装 ...

  9. Assembly之instruction之JUMP

    JMP  Jump unconditionally Syntax   JMP  label Operation PC + 2 × offset −> PC Description The 10- ...

  10. C#--条形码和二维码的简单实现

    首先 简单的介绍一下条形码和二维码 条形码: 条形码技术是在计算机应用中产生发展起来的一种广泛应用于商业.邮政.图书管理.仓储.工业生产过程控制.交通运输.包装.配送等领域的自动识别技术.它最早出现在 ...