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);
    }
    }
  • 相关阅读:
    centos7环境下安装mysql5.6-----解压安装包的方法
    Linux的常用命令
    在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法
    springboot下实现邮件发送功能
    centos7环境下开启指定端口
    阿里云开放指定端口
    Nginx的alias的用法及与root的区别
    关于Springboot打包错误的问题 | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin
    怎么简单高效破解MyEclipse10、获取注册码
    git删除远程分支
  • 原文地址:https://www.cnblogs.com/fanyu1/p/12423090.html
Copyright © 2011-2022 走看看