题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2830
给一个矩阵只有0和1,矩阵的列可以和其他列交换无数次,问交换后整个矩阵形成的最大的全是1的子矩阵的面积。
思路:可以每次枚举当前行的每一列的连续为1的个数。然后从大到小排序,每次更新当前最大的面积。(排完序以后可以看成某经典单调队列题,面积就是下标*当前高度)
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 #define fr first 23 #define sc second 24 #define Rint(a) scanf("%d", &a) 25 #define Rll(a) scanf("%lld", &a) 26 #define Rs(a) scanf("%s", a) 27 #define FRead() freopen("in", "r", stdin) 28 #define FWrite() freopen("out", "w", stdout) 29 #define Rep(i, n) for(int i = 0; i < (n); i++) 30 #define For(i, a, n) for(int i = (a); i < (n); i++) 31 #define Cls(a) memset((a), 0, sizeof(a)) 32 const int maxn = 1010; 33 34 int n, m; 35 int h[maxn]; 36 int dp[maxn]; 37 char G[maxn][maxn]; 38 39 int main() { 40 FRead(); 41 while(~Rint(n) && ~Rint(m)) { 42 int ans = 0; 43 Cls(h); Cls(dp); 44 Rep(i, n) Rs(G[i]); 45 Rep(i, n) { 46 Rep(j, m) { 47 if(G[i][j] == '1') h[j]++; 48 else h[j] = 0; 49 dp[j] = h[j]; 50 } 51 sort(dp, dp+m, greater<int>()); 52 Rep(j, m) { 53 if(dp[j] == 0) break; 54 ans = max(ans, dp[j] * (j + 1)); 55 } 56 } 57 printf("%d ", ans); 58 } 59 return 0; 60 }