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);
    }
    }
  • 相关阅读:
    [转]只有程序员才能看懂的幽默
    论安防集成管理平台规范标准建设的重要性和必要性
    视频集成技术演进与创新实践
    (转)jQuery中Ajax事件beforesend及各参数含义
    (转)如何打一手好Log
    marquee标签
    如何学习Git,如何使用Git
    学习vue
    python安装教程
    Jmeter压力测试(入门篇)
  • 原文地址:https://www.cnblogs.com/fanyu1/p/12423090.html
Copyright © 2011-2022 走看看