Matrix Swapping II
Time Limit: 9000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 809 Accepted Submission(s): 547
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
View Code
1 /* 2 这道题跟1505,1506,2870又是类似的。求最大矩阵。 3 不同的是,这里的每一列都是可以移动的,所以不必找左右边界的问题,对于 4 每一层,统计这一层中每一列的高度,然后将这些高度按降序排序,就解决了 5 列可以移动的问题,最后答案ans = max(ans,num[i]*i); 6 7 328MS 5168K 8 */ 9 #include <iostream> 10 #include <cstdio> 11 #include <cstring> 12 #include <algorithm> 13 #define SIZE 1005 14 15 using namespace std; 16 17 char ch[SIZE][SIZE]; 18 int height[SIZE][SIZE]; 19 int num[SIZE]; 20 int n,m; 21 22 bool cmp(int a,int b) 23 { 24 return a > b; 25 } 26 27 int main() 28 { 29 while(~scanf("%d%d",&n,&m)) 30 { 31 int ans = 0; 32 for(int i=1; i<=n; i++) 33 { 34 scanf("%s",ch[i]+1); 35 } 36 for(int i=1; i<=n; i++) 37 { 38 for(int j=1; j<=m; j++) 39 { 40 if(ch[i][j] == '1') 41 height[i][j] = height[i-1][j]+1; 42 else 43 height[i][j] = 0; 44 num[j] = height[i][j]; 45 } 46 sort(num+1,num+1+m,cmp); 47 for(int i=1; i<=m; i++) 48 ans = max(ans,num[i]*i); 49 } 50 printf("%d\n",ans); 51 } 52 return 0; 53 } 54 /* 55 网上看到的空间优化,而且代码更简洁,值得学习,按照这个思路,再打了一遍。 56 57 312MS 240K 58 59 #include <iostream> 60 #include <cstdio> 61 #include <cstring> 62 #include <algorithm> 63 #define SIZE 1005 64 65 using namespace std; 66 67 char ch[SIZE]; 68 int height[SIZE]; 69 int num[SIZE]; 70 int n,m; 71 72 bool cmp(int a,int b) 73 { 74 return a > b; 75 } 76 77 int main() 78 { 79 while(~scanf("%d%d",&n,&m)) 80 { 81 memset(height,0,sizeof(height)); 82 int ans = 0; 83 for(int i=1; i<=n; i++) 84 { 85 scanf("%s",ch+1); 86 for(int j=1; j<=m; j++) 87 { 88 if(ch[j] == '1') 89 height[j] ++; 90 else 91 height[j] = 0; 92 num[j] = height[j]; 93 } 94 sort(num+1,num+1+m,cmp); 95 for(int i=1; i<=m; i++) 96 ans = max(ans,num[i]*i); 97 } 98 printf("%d\n",ans); 99 } 100 return 0; 101 } 102 */

400

被折叠的 条评论
为什么被折叠?



