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. 在linux服务器centos上使用svn同步代码到项目中

    一.需求 1.在多人开发过程中代码的管理以及版本的控制是一个很重要的问题,因为在开发过程中我们可能会同时更改过某个文件或者更改过多个文件, 这会导致我们很容易发生错误.所以我们需要一个方式去管理我们的 ...

  2. SQL学习--Select(一)TOP、派生表、连接、谓词

    TOP关键字 WITH TIES t.title,sum(s.qty)as totalSales from sales s left join titles t on s.title_id=t.tit ...

  3. easyui combobox的增加全选解决方案

      1.解决方案背景: 项目中偶然需要用到easyui的combobox的组件,但是本组件自己没有包含全选的api事件.搜索了一些解决方案,但是不是很符合,后来发现是因为所使用的版本不一致所导致的.项 ...

  4. Embedded之Makefile

    1 Header files The header files are empty, so you can create them with touch: $ touch a.h $ touch b. ...

  5. python监听鼠标和键盘

    import PyHook3 def OnMouseEvent(event): print('MessageName:',event.MessageName) print('Message:',eve ...

  6. 新书《计算机图形学基础(OpenGL版)》PPT已发布

    为方便有些老师提前备课,1-10章所有章节已发布到本博客中. 欢迎大家下载使用,也欢迎大家给我们的新书反馈与意见,谢谢!

  7. WebForm 登陆test

    <script> window.onload = function () { document.getElementById("form1").onsubmit = f ...

  8. jquery.datatable.js实际运用

    $.dataTablesSettings = { deferRender: true,// 当处理大数据时,延迟渲染数据,有效提高Datatables处理能力 bStateSave: true,//表 ...

  9. Appium Android 获取包名和 Activity 的几种方法 (转)

    本文档主要记录“获取包名和 Activity 的方法”,用于自动化测试时启动APP.以下方法主要来源于网络和社区同学的贡献,特此感谢! 1. 方法一: pm list package查看包名 adb ...

  10. java源码

    Java泛型中的标记符含义: K - Key(键) V - Value(值) E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类,可做泛型) N - Nu ...