HDU 2830 Matrix Swapping II (最大完全子矩阵之可移动列)

本文探讨了一种算法,用于优化给定矩阵中的矩形结构,以最大化包含全1元素的子矩阵面积。通过列交换操作,实现矩阵优化,最终解决最大矩形面积问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matrix Swapping II

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



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
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   2870  2577  1505  1421  2571
  看了别人的才会,要我看怎么看都是枚举出所有情况,求最大矩阵。
注意要连续才行。我看成行互换去了。OTL

题意:

给你一个矩阵,里面的数字只有0和1两种,其中,列可以任意移动。问如何移动可以使某个子矩阵中元素全部是1,求出这个最大子矩阵的面积。

解题思路:

枚举所有的尾行,然后对于每个尾行,记录到这行为止每列连续的1的个数,为了形象起见,我们可以把每列看做宽度为1,连续个数看做它的高度。

然后问题就可以看做在一些高度可能为0的相邻矩形中找到最大矩形,即最大长方形那道题,如下图。


此题的另外一个条件是可以将列任意移动,我们肯定要尽量将高度大的放在一起,所以,我们可以将高度从大到小排序,然后有h[i-1]>=h[i],

即如果将1,2…i个矩形连在一起,它的高应该是h[i],所以面积显然是h[i] * i。

然后我们就枚举i从1到n,就可以解决这个问题了。

代码:763MS
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
#define M 2000
#define max(a,b) (a>b?a:b)
bool cmp(int x,int y){
return x>y;
}
char map[M][M];
int main()
{
    int i,j,n,m,Max,vis[M],dis[M];
    while(cin>>n>>m)
   {   Max=0;
       memset(dis,0,sizeof(0));
       for(i=1;i<=n;i++)
      {
       for(j=1;j<=m;j++)
      {
        cin>>map[i][j];
        if(map[i][j]=='1') dis[j]++;    //如果是一就表示高度为之前的高度+1。
        else dis[j]=0                   //如果该点维一,那么就不连续,且当前高度为零。
        vis[j]=dis[j];                  //这里要用到另一个数组,因为后面要从大到小排序,dis不能动,因为之后还有用的。
      }
      sort(vis+1,vis+1+m,cmp);          //用意前面以讲明。
      for(j=1;j<=m;j++) 
          if(vis[j]*j>Max)  Max=vis[j]*j;  //vis还只是高度。
      }
     cout<<Max<<endl;
   }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值