zoukankan      html  css  js  c++  java
  • CF 375B Maximum Submatrix 2[预处理 计数排序]

    B. Maximum Submatrix 2
    time limit per test
    2 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?

    Let's assume that the rows of matrix a are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. A matrix cell on the intersection of the i-th row and the j-th column can be represented as (i, j). Formally, a submatrix of matrix ais a group of four integers d, u, l, r (1 ≤ d ≤ u ≤ n; 1 ≤ l ≤ r ≤ m). We will assume that the submatrix contains cells (i, j)(d ≤ i ≤ ul ≤ j ≤ r). The area of the submatrix is the number of cells it contains.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 5000). Next n lines contain m characters each — matrix a. Matrix a only contains characters: "0" and "1". Note that the elements of the matrix follow without any spaces in the lines.

    Output

    Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.

    Examples
    input
    1 1
    1
    output
    1
    input
    2 2
    10
    11
    output
    2
    input
    4 3
    100
    011
    000
    101
    output
    2

    交换行,求全1子矩阵最大


    DP预处理a[i][j] i行j列到右面有几个连续的1
    枚举从那一列开始,按a来给行排序,统计就行了
    排序可以用计数排序,然而时间没有明显改善
    //
    //  main.cpp
    //  cf375b
    //
    //  Created by Candy on 9/15/16.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int N=5005;
    int n,m,a[N][N],ans=0;
    char s[N];
    int t[N],h[N],ne[N];
    
    int cou[N],srt[N];
    void buc(){
        memset(cou,0,sizeof(cou));
        int v=n;
        for(int i=1;i<=n;i++) cou[t[i]]++;
        for(int i=1;i<=v;i++) cou[i]+=cou[i-1];
        for(int i=n;i>=1;i--){
            srt[cou[t[i]]--]=t[i];
        }
    }
    int sol(int j){
        int ans=0;
        for(int i=1;i<=n;i++) t[i]=a[i][j];
        //sort(t+1,t+1+n);
        buc();
        for(int i=n;i>=1;i--)
            ans=max(ans,(n-i+1)*srt[i]);
        return ans;
    }
    int main(int argc, const char * argv[]) {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            for(int j=m-1;j>=0;j--){
                if(s[j]=='1') a[i][j+1]=a[i][j+2]+1;
                else a[i][j+1]=0;
            }
        }
        
        for(int j=1;j<=m;j++)
            ans=max(ans,sol(j));
        printf("%d",ans);
        return 0;
    }
     
  • 相关阅读:
    iOS如何测试微信小游戏&小程序?
    Android如何测试微信小游戏&小程序?
    Perfdog玩转内存泄漏
    【版本更新】PerfDog 4.0来袭,新增图表操作提示、子进程帧率精准测试,优化诸多细节
    PerfDog常用小技巧
    FAQ | PerfDog常见问题解答第一期
    扒一扒安卓渲染原理
    PerfDog测试腾讯视频、优酷、爱奇艺视频类小程序性能
    PerfDog可以助力高帧率游戏生态更全面发展
    【版本更新】PerfDog中文版震撼来袭,Web平台支持手机版与所有主流浏览器
  • 原文地址:https://www.cnblogs.com/candy99/p/5874853.html
Copyright © 2011-2022 走看看