Matrix Swapping II

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1543    Accepted Submission(s): 1036

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.

 
题解:

也是求最大矩阵的,只不过可以相互交换任意两列。

访问每一行时,求出每个点的高度,然后排序,以该点为高时的宽度就很容易看出来,面积取最大的就可以了。

ans=max(ans,(k-j+1)*s[j]);由于可以交换列,这里随着往右走,宽度逐渐减小,找最大值;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=;
char mp[MAXN][MAXN];
int dp[MAXN][MAXN];
int s[MAXN];
int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
mem(dp,);
int ans=;
for(int i=;i<=N;i++){
scanf("%s",mp[i]);
for(int j=M;j>=;j--){
mp[i][j]=mp[i][j-];
}
}
for(int i=;i<=N;i++){
int k=;
for(int j=;j<=M;j++){
if(mp[i][j]==''){
dp[i][j]=dp[i-][j]+;
s[++k]=dp[i][j];
}
}
sort(s+,s+k+);
for(int j=;j<=k;j++){
ans=max(ans,(k-j+)*s[j]);
}
}
printf("%d\n",ans);
}
return ;
}

Matrix Swapping II(求矩阵最大面积,dp)的更多相关文章

  1. HDU 2830 Matrix Swapping II (预处理的线性dp)

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

  2. 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 ...

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

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

  4. HDU 2830 Matrix Swapping II

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

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

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

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

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

  7. 【HDOJ】2830 Matrix Swapping II

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

  8. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  9. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

随机推荐

  1. SQLite header and source version mismatch 解决方案

    我下载了sqlite源码,并且configure, make, make install. 然后就出现SQLite header and source version mismatch  的错误. 上 ...

  2. .NET日志工具介绍

    最近项目需要一个日志工具来跟踪程序便于调试和测试,为此研究了一下.NET日志工具,本文介绍了一些主流的日志框架并进行了对比.发表出来与大家分享. 综述 所谓日志(这里指程序日志)就是用于记录程序执行过 ...

  3. [方法] Windows 下SSH远程连接Linux

    考虑到Linux服务器自带SSH服务,并且SSH服务开机启动. 因此,方法如下: 安装putty 输入Linux服务器IP地址 此外,如果想远程重启Linux服务器,在命令行中输入reboot即可.

  4. AIX 命令大全

    http://www.ahinc.com/aix/general.htm http://web.mit.edu/javadev/packages/Acme/ http://jparsec.codeha ...

  5. php中strstr、strrchr、substr、stristr四个函数用法区别

    php中strstr.strrchr.substr.stristr四个函数用法区别: php中strstr strrchr substr stristr这四个字符串操作函数特别让人容易混淆,常用的是s ...

  6. 本地/远程Service 和Activity 的交方式(转)

    android SDK提供了Service,用于类似*nix守护进程或者windows的服务. Service有两种类型: 本地服务(Local Service):用于应用程序内部 远程服务(Remo ...

  7. snort简介以及在Ubuntu下的安装

    一.简介 Snort是一套开放源代码的网络入侵预防软件与网络入侵检测软件.Snort使用了以侦测签章(signature-based)与通信协议的侦测方法.Snort是一个免费的IDS(入侵监测系统) ...

  8. chroot_local_user和chroot_list_enable含义

    很多情况下,我们希望限制ftp用户只能在其主目录下(root dir)下活动,不允许他们跳出主目录之外浏览服务器上 的其他目录,这时候我就需要使用到chroot_local_user,chroot_l ...

  9. POJ2553-The Bottom of a Graph

    id=2553">主题链接 题意:求解Bottom(G).即集合内的点能够互相到达. 思路:有向图的强连通.缩点,找出出度为0的点,注意符合的点要按升序输出. 代码: #include ...

  10. 怎样解决xcode里开发cocos2dx改动lua脚本后不刷新的问题

    用xcode来开发cocos2dx,结果发现一个非常纠结的问题,假设我一旦改动了一个Lua文件,我必须clean之后再build,否则改动的Lua文件不会体现出来.这是一个非常令纠结的结果,特别是我要 ...