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. ecshop类的解析2 json_encode和json_decode的具体实现

    在看ecshop源码时,看到json这个类,研究了一下,它是为了兼容低版本php而做出的类,是对php的数据和json转换时使用的. encode和decode函数是对json的操作,对应json_e ...

  2. Django day14(一) cookie

    一: Cookie 1.  Cookie是什么?存储在客户端浏览器上的键值对 2. 原理: 是服务器产生,发给客户端浏览器,浏览器保存起来,下次发请求,会携带这个键值对到服务器 4. Cookie的覆 ...

  3. 【寒假集训系列DAY.1】

    Problem A. String Master(master.c/cpp/pas) 题目描述 所谓最长公共子串,比如串 A:“abcde”,串 B:“jcdkl”,则它们的最长公共子串为串 “cd” ...

  4. HTML+CSS+JS总结

    ==================HTML(超文本标记语言)========== <!DOCTYPE> 声明位于文档中的最前面的位置,处于 <html> 标签之前.此标签可告 ...

  5. 关于Membership和身份认证的记录

    在今天写好的code中测试环节,当我用webconfig中的测试数据库就是ok的,但是更替为正式的就不行了: 报错的类是MemberShip,那就关系到身份认证的环节了 找了几个链接,记录下 1.身份 ...

  6. OpenCV视频进度播放控制

    本来打算把进度条嵌入MFC的PIC空间里面,结果显示进度条消失,看来还是不要这个样子了. 全局变量区域: //2.初始化进度条的位置 int G_slider_position = 0; CvCapt ...

  7. java中负数的补码转换为十进制

    一个数如果为正,则它的原码.反码.补码相同:一个正数的补码,将其转化为十进制,可以直接转换. 已知一个负数的补码,将其转换为十进制数,步骤: 1.先对各位取反: 2.将其转换为十进制数: 3.加上负号 ...

  8. GET 请求控制器 返回绑定后HTML

    //$.get("/Home/index/" + $("#S_BookName").val(), function (data) { //MVC控制器返回Vie ...

  9. Tomcat jsp页面显示有问题

    1.干掉tomcat下的work文件夹里面的东西,让jsp文件重新编译,相当于清楚缓存 2.work 里面是 jsp 编译的类 ,只要jsp 被访问了,就会被编译,就会生成相应的类 3.tomcat下 ...

  10. spring boot注解

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