poj 3494 Largest Submatrix of All 1’s

来源:互联网 发布:粒子群算法matlab实例 编辑:程序博客网 时间:2024/05/19 17:58

这题是poj2559的变形,每次枚举底边,然后完全能转换为poj2559
http://poj.org/problem?id=2559

解法和poj2559差不多
由于这里要频繁用栈
所以还是不要用stl的stack
我之前用了,就TLE

#include <iostream>#include <vector>#include <cstring>#include <algorithm>#include <stack>#include <cstdio>using namespace std;const int MAXN = 2000 + 5;typedef long long ll;int h[MAXN];int mat[MAXN][MAXN];int s[MAXN];int top;int main() {    int n, m;    while (scanf("%d %d", &n, &m) != EOF)    {        for (int i = 1; i <= n; ++i)            for (int j = 1; j <= m; ++j)                scanf("%d", &mat[i][j]);        memset(h, 0, sizeof(h));        int ans = 0;        for (int i = 1; i <= n; ++i)    //枚举行,就是底边        {            for (int j = 1; j <= m; ++j)                h[j] = mat[i][j] == 0 ? 0 : h[j] + 1;            h[0] = 0;            h[m + 1] = 0;            top = 0;            s[top++] = 0;            for (int j = 1; j <= m + 1; ++j)            {                while (h[j] < h[s[top - 1]])                {                    int a = h[s[--top]];                    int b = s[top - 1] + 1;                    int t = a * (j - 1 - b + 1);                    ans = max(ans, t);                }                s[top++] = j;            }        }        printf("%d\n", ans);    }    return 0;}
原创粉丝点击