HDU 2830 Matrix swapping II (movable column of the maximal complete sub-matrix)

Source: Internet
Author: User

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 Descriptiongiven an N * M matrix with each entry equal to 0 or 1. We can find some rectangles in the matrix whose entries is all 1, and we define the maximum area of such rectangle as thi S matrix ' s goodness.

We can swap any or both columns any times, and we is to make the goodness of the matrix as large as possible.

Inputthere is several test cases in the input. The first line of all test case contains, integers N and M (1≤n,m≤1000). Then n lines follow, each contains m numbers (0 or 1), indicating the n * M Matrix

Outputoutput one line for each test case, indicating the maximum possible goodness.
Sample Input
3 41011100100013 4101010010001

Sample Output
42note:huge Input, scanf () is recommended.

Source2009 multi-university Training Contest 2-host by Tju
Recommendgaojie | We have carefully selected several similar problems for you:2870 2577 1505 1421 2571
Look at other people's will, want me to see how to see all the case, to find the largest matrix.
Pay attention to continuous line. I saw the interchange go. OTL

Test instructions

Give you a matrix where the numbers are only 0 and 12, where the columns can be moved arbitrarily. Asking how to move can make the element in a sub-matrix all 1, and find out the area of the largest sub-matrix.

Problem Solving Ideas:

Enumerate all the trailing rows, and then for each tail row, record the number of consecutive 1 rows per column so that for the sake of image, we can treat each column as a width of 1 and a continuous number as its height.

The problem can then be seen as finding the largest rectangle in a number of adjacent rectangles with a height of 0, that is, the largest rectangular problem, such as.


Another condition of this problem is that you can move the column arbitrarily, and we must try to put the height together as much as possible, so we can sort the heights from big to small and then have h[i-1]>=h[i],

That is, if 1,2...i a rectangle together, its height should be h[i], so the area is obviously h[i] * i.

Then we can solve this problem by enumerating I from 1 to N.

Code: 763MS
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h>using namespaceStd;#define M 2000#define Max (A, B) (A>B?A:B)BOOLCmp (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]++; If it is one, the height is +1 of the height before it.  Else Dis[J]=0                   //If the point is dimension one, then it is discontinuous and the current height is zero. Vis[J]=Dis[J]; Another array is used here, because the dis cannot move because it is later to be sorted from large to small, because it is useful afterwards. }Sort(Vis+1,Vis+1+M,Cmp); The intention was preceded by a clear speech. for ( J=1;J<=M;J++)  if (Vis[J]*J>Max)Max=Vis[J]*J; Vis is still only high. }cout<<Max<<Endl; }  return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.