cf D. Maximum Submatrix 2

http://codeforces.com/contest/376/problem/D

题意:给你一个矩阵,可以随意排列n行的次序,然后找出全部含有1的子矩阵。输出1的个数。

思路:c[i][j]表示在i列第j行的格子的右边连续的1的个数。对于每一列,排序c[i],然后比较c[i][j]*(n-j+1)与max1的大小,max1就是答案。

 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <algorithm>

 5 #define maxn 5001

 6 using namespace std;

 7 

 8 char g[maxn][maxn];

 9 int c[maxn][maxn];

10 int n,m;

11 int x[maxn];

12 

13 int main()

14 {

15      while(scanf("%d%d",&n,&m)!=EOF)

16      {

17          memset(c,0,sizeof(c));

18          for(int i=1; i<=n; i++)

19          {

20              scanf("%s",g[i]+1);

21          }

22          for(int i=1; i<=n; i++)

23          {

24              for(int j=1; j<=m; j++)

25              {

26                  if(g[i][j]=='1')

27                  {

28                       c[j][i]=c[j-1][i]+1;

29                  }

30              }

31          }

32          int max1=-1;

33          for(int i=1; i<=m; i++)

34          {

35              sort(c[i]+1,c[i]+1+n);

36              for(int j=1; j<=n; j++)

37              {

38                  if(c[i][j]*(n-j+1)>max1)

39                  {

40                      max1=c[i][j]*(n-j+1);

41                  }

42              }

43          }

44          cout<<max1<<endl;

45      }

46      return 0;

47 }
View Code

 

你可能感兴趣的:(Matrix)