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. [deviceone开发]-do_SlideListView的简单示例

    一.简介 利用提供的SlideListVIew实现那种cell可以滑动露出底部按钮的功能 主要组件:do_slidelistview 二.效果图 三.相关讨论 http://bbs.deviceone ...

  2. Flex布局窥探(一)

    一.Flex布局是神马? Flex是Flexible Box的缩写,意为‘弹性布局’,用来为盒模型提供最大的灵活性. 任何容器都能被指定为Flex布局: .box{ display: flex; } ...

  3. 30分钟手把手教你学webpack实战

    30分钟手把手教你学webpack实战 阅读目录 一:什么是webpack? 他有什么优点? 二:如何安装和配置 三:理解webpack加载器 四:理解less-loader加载器的使用 五:理解ba ...

  4. Asp.net web hosting

      start /D "C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0" /B WebDev.WebSe ...

  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q56-Q58)

    Question 56You work for a manufacturer who needs to advertise its catalog of products online using a ...

  6. webView 显示一段 html 代码

    1.布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...

  7. android studio 使用SVN 锁定文件,防止别人修改(基于Android studio 1.4 )

    首先假设开发 A , 和 开发 B , 在使用 SVN 进行项目管理.那么A如何才能 某个锁定文件,防止B修改. 1.第一步,给这个文件加锁    完成这一步,则这个文件就别锁定了. 2.第二步,假如 ...

  8. Android 手机卫士--实现设置界面的一个条目布局结构

    本文地址:http://www.cnblogs.com/wuyudong/p/5908986.html,转载请注明源地址. 本文以及后续文章,将一步步完善功能列表: 要点击九宫格中的条目,需要注册点击 ...

  9. 你真的了解UINavigationController吗?

    一:首先查看一下关于UINavigationController的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationController : ...

  10. nodejs get/request

    灌水评论示例: var http = require('http'); var querystring = require('querystring'); var postData = queryst ...