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=;
int n,m,a[N][N],ans=;
char s[N];
int t[N],h[N],ne[N]; int cou[N],srt[N];
void buc(){
memset(cou,,sizeof(cou));
int v=n;
for(int i=;i<=n;i++) cou[t[i]]++;
for(int i=;i<=v;i++) cou[i]+=cou[i-];
for(int i=n;i>=;i--){
srt[cou[t[i]]--]=t[i];
}
}
int sol(int j){
int ans=;
for(int i=;i<=n;i++) t[i]=a[i][j];
//sort(t+1,t+1+n);
buc();
for(int i=n;i>=;i--)
ans=max(ans,(n-i+)*srt[i]);
return ans;
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%s",s);
for(int j=m-;j>=;j--){
if(s[j]=='') a[i][j+]=a[i][j+]+;
else a[i][j+]=;
}
} for(int j=;j<=m;j++)
ans=max(ans,sol(j));
printf("%d",ans);
return ;
}
 

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

  1. cf D. Maximum Submatrix 2

    http://codeforces.com/contest/376/problem/D 题意:给你一个矩阵,可以随意排列n行的次序,然后找出全部含有1的子矩阵.输出1的个数. 思路:c[i][j]表示 ...

  2. Codeforces 375B Maximum Submatrix 2 (DP)

    <题目链接> 题目大意:给出一个01矩阵,行与行之间可以互换位置,问能够得到最大的全1矩阵的面积. #include <bits/stdc++.h> using namespa ...

  3. 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 ...

  4. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  5. 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)

    2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...

  6. 由Maximum Gap,对话桶排序,基数排序和统计排序

    一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...

  7. 计数排序(counting-sort)——算法导论(9)

    1. 比较排序算法的下界 (1) 比较排序     到目前为止,我们已经介绍了几种能在O(nlgn)时间内排序n个数的算法:归并排序和堆排序达到了最坏情况下的上界:快速排序在平均情况下达到该上界.   ...

  8. 计数排序和桶排序(Java实现)

    目录 比较和非比较的区别 计数排序 计数排序适用数据范围 过程分析 桶排序 网络流传桶排序算法勘误 桶排序适用数据范围 过程分析 比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比 ...

  9. 计数排序-java

    今天看了一本书,书里有道题,题目很常见,排序,明了点说: 需求:输入:最多有n个正整数,每个数都小于n, n为107 ,没有重复的整数 输出:按升序排列 思路:假设有一组集合 {1,3,5,6,11, ...

随机推荐

  1. 苹果浏览器和uc浏览器在移动端的坑(日常积累,随时更新)

    先mark 1 .  移动端uc浏览器不兼容css3 calc() 2 .  ie8下a标签没有内容给宽高也不能触发点击跳转 3 . safari输入框加上readOnly="ture&qu ...

  2. AH00098 pid file overwritten

    错误日志: 由于定义了: <IfModule mpm_winnt_module> ThreadsPerChild 450 MaxConnectionsPerChild 4000 Accep ...

  3. 如何通过CSS3 实现响应式Web设计

    如何通过CSS3 实现响应式Web设计: 分为三个步骤:(1)允许网页宽度自动调整.首先在页面头部中,我们需要加入这样一行:<meta name="viewport" con ...

  4. 解决方案:带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载可能出现 COM 组件的80040154错误

      建议大家在微软的组件出现问题时,在GOOGLE上搜索解决方案,一般来说,总有结果:  带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载,可能出现 COM 组件的80 ...

  5. [Android]使用RecyclerView替代ListView(三)

    以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4268097.html  这次来使用RecyclerView实现Pinn ...

  6. Android 数据库 LiteOrm 的使用

    1.什么是 LiteOrm LiteOrm是一个小巧.强大.比系统自带数据库操作性能快1倍的 android ORM 框架类库,开发者一行代码实现数据库的增删改查操作,以及实体关系的持久化和自动映射. ...

  7. 苹果IPSW文件提取软件

    ipsw文件 提取系统文件 方法总结 由于修改运营商文件造成我的有锁4S无法使用移动卡了,在网上苦寻一番还是没有结果,最后萌生了从固件中提取文件的想法,于是便开始在网上搜集资料,最后文件终于提取成功并 ...

  8. Mac OS 下的解压缩软件——The Unarchiver

    The Unarchiver 是 Mac 上最流行的解压软件,免费开源.操作方式与系统自带解压工具 Archive Utility.app 一样,双击自动解压.最爽的一点是把解压后原始文件直接仍进废纸 ...

  9. android Gui系统之SurfaceFlinger(4)---Vsync(1)

    8.Vsync 8.1概论 VSYNC(Vertical Synchronization)是一个相当古老的概念,对于游戏玩家,它有一个更加大名鼎鼎的中文名字—-垂直同步. “垂直同步(vsync)”指 ...

  10. TCP的粘包现象

    看面经时,看到有面试官问TCP的粘包问题.想起来研一做购物车处理数据更新时遇到粘包问题,就总结一下吧. 1 什么是粘包现象 TCP粘包是指发送方发送的若干包数据到接收方接收时粘成一包,从接收缓冲区看, ...