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. OC常用的数学函数及宏定义

    一.函数 1. 三角函数 double sin (double);正弦 double cos (double);余弦 double tan (double);正切 2 .反三角函数 double as ...

  2. AMD 与 CMD 区别

    作者:玉伯链接:https://www.zhihu.com/question/20351507/answer/14859415来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  3. flash 遮住 div 解决办法

    被遮盖的div 下面的代码   <!--列表菜单-->             <div id="opreationmenu" style="posit ...

  4. Tour UVA - 1347

    John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts vi ...

  5. HTML+CSS(10)

    n  组合选择器 多元素选择器 n  描述:给多个元素加同一个样式,多个选择器之间用逗号隔开. n  举例:h1,p,div,body{color:red;} 后代元素选择器(最常用) n  描述:给 ...

  6. Error:CreateProcess error=216的错误,JDK版本不匹配问题。

    今天刚下载安装完android studio,结果随便新建一个工程的时候就出现了如下提示: Error:CreateProcess error=216, 该版本的 %1 与您运行的 Windows 版 ...

  7. iOS 加密算法汇总

    CCCryptorStatus CCCryptorCreate( CCOperation op,             /* kCCEncrypt, etc. */ CCAlgorithm alg, ...

  8. SQLite 的使用

    private void button1_Click(object sender, EventArgs e) { //查询数据库内容并绑定 string sql= "select* from ...

  9. webpack学习(六)—webpack+react+es6(第2篇)

    接上篇        webpack学习(五)—webpack+react+es6(第1篇) 本文做个简单的图片加文字的页面.其中,配置文件跟上篇一致.项目结构: index.html <!DO ...

  10. spring boot注解

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...