poj 3494 Largest Submatrix of All 1’s

Largest Submatrix of All 1’s
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 3964   Accepted: 1476
Case Time Limit: 2000MS

Description

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output

For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input

2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0

Sample Output

0
4

数据输入用scanf快得多,同样的代码,用cin就会超时
#include
using namespace std;

int height[2005];
int l[2005],r[2005];
int mmax = 0;

void cal(int m)
{
    int k;
    for(k = 1; k <= m; ++k)
    {
        l[k] = k;
        r[k] = k;
    }
    height[0] = height[m+1] = -1;
    
    for(k = 1; k <= m; ++k)
    {
        while(height[l[k]-1] >= height[k])
            l[k] = l[l[k]-1];
    }
    for(k = m; k >= 1; --k)
    {
        while(height[r[k]+1] >= height[k])
            r[k] = r[r[k]+1];
    }
    for(k = 1; k <= m; ++k)
    {
        int t = height[k] * (r[k] - l[k] + 1);
        if(t > mmax)
            mmax = t;
    }
}

int main()
{
    int n, m;
    int i, j, k;
    int temp;
    freopen("E:\\c++\\oj\\t.txt", "r", stdin);
    freopen("E:\\c++\\oj\\out.txt", "w", stdout);
    
    while(cin>>n>>m)
    {
        mmax = 0;
        memset(height, 0, sizeof(height));
        for(i = 1; i <= n; ++i)
        {
            for(j = 1; j <= m; ++j)
            {
                scanf("%d", &temp);
                if(temp == 1)
                    height[j]++;
                else
                    height[j]= 0;
            }
            cal(m);
        }        
        cout<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/w0w0/archive/2012/05/05/2484849.html

你可能感兴趣的:(poj 3494 Largest Submatrix of All 1’s)