zoukankan      html  css  js  c++  java
  • poj3494Largest Submatrix of All 1’s

    Largest Submatrix of All 1’s
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 9943   Accepted: 3538
    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

    Source

     
    先进行预处理,将每行连着的高度向下加,如
    0 0 0 0                 0 0 0 0
    0 1 1 0————>0  1 1 0
    0 1 1 0                 0 2 2 0 
    0 0 0 0                 0 0 0 0
    之后转变为求最大矩形面积模板题;
     
     
     代码
    #include<iostream>
    #include<cstring>
    #include<stack>
    #include<cstdio>
    using namespace std;
    const int N = 2e3+50;

    int mp[N][N], dp[N][N];
    int main()
    {
        int m,n;
        while(scanf("%d%d",&n,&m)!=EOF){
        int ans=0;
        memset(dp,0,sizeof(dp));
        //预处理
        for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
        scanf("%d",&mp[i][j]);
        if(mp[i][j]==1)
        dp[i][j]=dp[i-1][j]+1;
    }
    dp[i][0]=dp[i][m+1]=-1;
    }
    for(int i=1;i<n;++i){
    stack<int> s;
    int r[N];
    s.push(m+1);
    for(int j=m;j;--j){
    while(dp[i][s.top()]>=dp[i][j]) s.pop();
    r[j]=s.top();
    s.push(j);
    }
    while(!s.empty())s.pop();
    s.push(0);
    for(int j=1;j<=m;++j){
    while(dp[i][s.top()]>=dp[i][j])s.pop();
    ans=max(ans,dp[i][j]*(r[j]-s.top()-1));
    s.push(j);
    }
    }
    printf("%d ",ans);
    }
    }
     
    数组模拟
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int N = 2e3+50;
    struct elem{
     int height;
     int count;
    };
    int mp[N][N], dp[N][N];
    elem stack[N];
    int top,tmp;
    int main()
    {
        int m,n,height,tot;
        while(scanf("%d%d",&n,&m)!=EOF){
        int ans=0;
        memset(dp,0,sizeof(dp));
        //预处理
        for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
        scanf("%d",&mp[i][j]);
        if(mp[i][j]==1)
        dp[i][j]=dp[i-1][j]+1;
    }
    dp[i][0]=dp[i][m+1]=-1;
    }
    //
    //for(int i=1;i<=n;i++){
    // for(int j=1;j<=m;j++){
    //  cout<<dp[i][j];
    // }
    // cout<<endl;
    //}
    for(int i=1;i<=n;i++){
     top=0;
     for(int j=1;j<=m;++j){
      height=dp[i][j];
          tmp=0;
          while (top > 0 && stack[top - 1].height >= height){
                    tot = stack[top - 1].height * (stack[top - 1].count + tmp);
        if (tot > ans) ans = tot;
        tmp += stack[top - 1].count;
        --top;
        }
       stack[top].height = height;
       stack[top].count = 1 + tmp;
       ++top;
        }
        tmp = 0;
        while (top > 0)
        {
        tot = stack[top - 1].height * (stack[top - 1].count + tmp);
        if (tot > ans) ans = tot;
    //    cout<<ans<<endl;
        tmp += stack[top - 1].count;
        --top;
        } 
     }
    printf("%d ",ans);
    }
    }
  • 相关阅读:
    $Poj2228$/洛谷$SP283 Naptime$ 环形$DP$
    $Poj1952 $洛谷$1687 Buy Low,Buy Lower$ 线性$DP+$方案计数
    $Poj3585 Accumulation Degree$ 树形$DP/$二次扫描与换根法
    洛谷$1541$ 乌龟棋 线性$DP$
    $Loj10157$ 皇宫看守 树形$DP$
    $loj10156/$洛谷$2016$ 战略游戏 树形$DP$
    $Loj10155$ 数字转换(求树的最长链) 树形$DP$
    洛谷$2015$二叉苹果树
    $CH5302$ 金字塔 区间$DP$/计数类$DP$
    [hiho1035] 自驾旅行III
  • 原文地址:https://www.cnblogs.com/fanyu1/p/12423090.html
Copyright © 2011-2022 走看看