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. 1016. 部分A+B

    /* * Main.c * 1016. 部分A+B * Created on: 2014年8月30日 * Author: Boomkeeper *******测试通过********* */ #inc ...

  2. 用gson 解 json

    1.json 简单介绍 轻量级数据存储传输文件,比xml精简. 2.Gson简单介绍 android中解json方式很多,比较常用json-lib  goole-Gson. json-lib要包含的包 ...

  3. matlab绘制函数

    >> x1=linspace(,*pi,); x2=linspace(,*pi,); x3=linspace(,*pi,); y1=sin(x1); y2=+sin(x2); y3=+si ...

  4. Protel在PCB中添加汉字

    使用Protel 99SE的工程人员都知道Protel在PCB绘制中是不支持汉字输入的,但作为工厂生产调试的方便,不可避免的要在PCB上制作中文标示,有时为说明板子的用途,注意事项等都要输入中文丝印, ...

  5. WPS Office手机版调用接口代码指导帖之一(Android)

    经常会有一些喜欢开发鼓捣的童鞋问我们,WPS Office手机版是否提供调用接口,希望在android中使用一个调用命令,直接调用WPS手机版来打开指定的DOC文件,而不用弹出一个程序可选列表(如果用 ...

  6. sql 多条件查询 拼接字符串 改成 普通查询格式

    set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROC [dbo].[usp_SRV_CheckServiceDemandOrder] ) = ...

  7. poj3086---数论

    #include <stdio.h> #include <stdlib.h> int T(int n) { ,i; ;i<=n;i++) { sum+=i; } retu ...

  8. vs2010:【“System.Data.OracleClient.OracleConnection”已过时】警告

    在oracle 安装目录下 找到 Oracle.DataAccess.dll添加引用,然后 using Oracle.DataAccess.Client;其他的都不用动,即可.连接字符串中 如有 用的 ...

  9. TCP为什么需要3次握手与4次挥手

    为什么需要“三次握手” 在谢希仁著<计算机网络>第四版中讲“三次握手”的目的是“为了防止已失效的连接请求报文段突然又传送到了服务端,因而产生错误”.在另一部经典的<计算机网络> ...

  10. cocos2dx3.3在Windows环境搭建以及新工程创建

    这个虽然比较简单,但是是学习cocos的第一步,不积跬步无以至千里,所以今天先分享Windows下环境搭建问题.关于mac搭建后续有机会在写(ps:因为我暂时没有mac),anyway,开始吧! 首先 ...