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 11

output

1

input

2 21011

output

2

input

4 3100011000101

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;
}
 
时间: 2024-08-14 03:07:30

CF 375B Maximum Submatrix 2[预处理 计数排序]的相关文章

codeforces 375B Maximum Submatrix 2

题意:给你一个矩阵,问你只变动行,能到到最大面积的全为‘1’ 的矩阵. 解题思路:先一行一行的求出到了这一列最长连续的1有多少,然后一列一列先hash 然后求 最大值 ,发现%1d好慢. 解题代码: 1 // File Name: 375b.cpp 2 // Author: darkdream 3 // Created Time: 2015年03月09日 星期一 21时05分00秒 4 5 #include<vector> 6 #include<list> 7 #include&l

Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序

B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/375/B Description 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

有Leetcode中一道题,谈桶排序,基数排序和计数排序

几种非比较排序 在LeetCode中有个题目叫Maximum Gap,是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都可以实现.下面说一下桶排序.基数排序和计数排序这三种非比较排序. 桶排序 这种排序的主要思想是,把数列分配到多个桶中,然后再在各个桶中使用排序算法进行排序,当然也可以继续使用桶排序. 假设数组的最大值是A,最小值是B,长度是L,则每个桶的大小可以是S=Max(1,(A-B)/(L-1))则可以分为(A-B)/S+1个桶. 对于数列中的数字x,用(x-B

ACM学习历程—HDU 4726 Kia&#39;s Calculation( 贪心&amp;&amp;计数排序)

DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so careless and alway forget to carry a number when the sum of two digits exceeds 9. For example, when she calculates 4567+5789, she will get 9246, and for 12

排序之计数排序

计数排序并非一种基于比较进行的排序,它是计算一个序列中的值在正常排好序中的序列所处的位置,怎么求解一个数的位置呢?就是利用下脚标进行求解,新建一个数组resu[],数组的长度要比序列中的最大值大1,数组中的值全部初始化为0,然后遍历原序列,将原序列的值i作为新建数组resu[]的下脚表,对resu[i]++操作,这样就得出值 i 出现的次数:然后再利用resu[i]=resu[i]+resu[i-1]求解出i在原序列中的位置. 计数排序需要两个额外的数组作辅助,提供临时存储的resu数组,存放最

基数排序与桶排序,计数排序【详解】

桶排序简单入门篇^-^ 在我们生活的这个世界中到处都是被排序过的东东.站队的时候会按照身高排序,考试的名次需要按照分数排序,网上购物的时候会按照价格排序,电子邮箱中的邮件按照时间排序……总之很多东东都需要排序,可以说排序是无处不在.现在我们举个具体的例子来介绍一下排序算法. 首先出场的是我们的主人公小哼,上面这个可爱的娃就是啦.期末考试完了老师要将同学们的分数按照从高到低排序.小哼的班上只有5个同学,这5个同学分别考了5分.3分.5分.2分和8分,哎,考得真是惨不忍睹(满分是10分).接下来将分

计数排序

算法思想 编辑 计数排序对输入的数据有附加的限制条件: 1.输入的线性表的元素属于有限偏序集S: 2.设输入的线性表的长度为n,|S|=k(表示集合S中元素的总数目为k),则k=O(n). 在这两个条件下,计数排序的复杂性为O(n). 计数排序的基本思想是对于给定的输入序列中的每一个元素x,确定该序列中值小于x的元素的个数(此处并非比较各元素的大小,而是通过对元素值的计数和计数值的累加来确定).一旦有了这个信息,就可以将x直接存放到最终的输出序列的正确位置上.例如,如果输入序列中只有17个元素的

计数排序Counting sort

注意与基数排序区分,这是两个不同的排序 计数排序的过程类似小学选班干部的过程,如某某人10票,作者9票,那某某人是班长,作者是副班长 大体分两部分,第一部分是拉选票和投票,第二部分是根据你的票数入桶 看下具体的过程,一共需要三个数组,分别是待排数组,票箱数组,和桶数组 var unsorted = new int[] { 6, 2, 4, 1, 5, 9 };  //待排数组 var ballot = new int[unsorted.Length];          //票箱数组 var b

计数排序(Count Sort )与插入排序(Insert Sort)

计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比较的排序方法 1 void count(int top,int length,int arr[]) 2 { 3 int min=arr[0],max=arr[0],i=1,j=0; 4 int *count=(int*)malloc(sizeof(int)*(max-min+1)); 5 if(ar