poj3494

给定一个01矩阵,求出面积最大的1矩阵。


一行一行来,就变成基本的求直方图的最大面积的情况了。

在poj上要用scanf和printf否则超时


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;

typedef long long LL;
int a[5050][5050], l[5050], r[5050], h[5050];

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n, m, i, j, k, ans, t;
    string s;
    cin >> n >> m;
    for(i = 1; i <= n; i++)
    {
        cin >> s;
        for(j = 1; j <= m; j++) a[i][j] = s[j-1] - '0';
    }
    ans = 0;
    rep(i, 1, m) h[i] = 0;

    rep(i, 1, n)
    {
        h[0] = h[m+1] = -1;
        for(k = 1; k <= m; k++)
        {
            if(a[i][k]) h[k]++;
            else h[k] = 0;
        }
        for(j = 1; j <= m; j++)
        {
            t = j;
            while(h[t-1] >= h[j]) t = l[t-1];
            l[j] = t;
        }
        for(j = m; j >= 1; j--)
        {
            t = j;
            while(h[t+1] >= h[j]) t = r[t+1];
            r[j] = t;
        }

        for(j = 1; j <= m; j++)
        {
            ans = max(ans, h[j] * (r[j] - l[j] + 1));
        }
    }
    cout << ans << endl;
    return 0;
}


你可能感兴趣的:(dp)