CF 375B Maximum Submatrix 2[预处理 计数排序]
2 seconds
512 megabytes
standard input
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 ≤ u; l ≤ j ≤ r). The area of the submatrix is the number of cells it contains.
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.
Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.
1 1
1
1
2 2
10
11
2
4 3
100
011
000
101
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[预处理 计数排序]的更多相关文章
- cf D. Maximum Submatrix 2
http://codeforces.com/contest/376/problem/D 题意:给你一个矩阵,可以随意排列n行的次序,然后找出全部含有1的子矩阵.输出1的个数. 思路:c[i][j]表示 ...
- Codeforces 375B Maximum Submatrix 2 (DP)
<题目链接> 题目大意:给出一个01矩阵,行与行之间可以互换位置,问能够得到最大的全1矩阵的面积. #include <bits/stdc++.h> using namespa ...
- 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 ...
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...
- 计数排序(counting-sort)——算法导论(9)
1. 比较排序算法的下界 (1) 比较排序 到目前为止,我们已经介绍了几种能在O(nlgn)时间内排序n个数的算法:归并排序和堆排序达到了最坏情况下的上界:快速排序在平均情况下达到该上界. ...
- 计数排序和桶排序(Java实现)
目录 比较和非比较的区别 计数排序 计数排序适用数据范围 过程分析 桶排序 网络流传桶排序算法勘误 桶排序适用数据范围 过程分析 比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比 ...
- 计数排序-java
今天看了一本书,书里有道题,题目很常见,排序,明了点说: 需求:输入:最多有n个正整数,每个数都小于n, n为107 ,没有重复的整数 输出:按升序排列 思路:假设有一组集合 {1,3,5,6,11, ...
随机推荐
- SAP 中的用户类型
在使用 SU01 维护用户时,在登录数据选项卡中会要求选择用户类型,一般我们都选择第一项 Dialog,但是其他几个选项有什么作用呢?下面我们就一一解释. 对话用户顾名思义,就是需要通过 SAP GU ...
- 服务 {49A27252-A326-4EF1-B698-6EBC7068833C} 的计时器作业 id {573BE459-DF82-481C-84BD-CA14D287450B} 配置刷新的上一个实例仍在运行,因此将跳过当前的实例。请考虑增加作业之间的时间间隔。
在SharePoint2007的错误日志中发现大量如下错误: 07/02/2013 16:17:25.99 OWSTIMER.EXE (0x0958) 0x097C Window ...
- C++ virtual虚函数
#include<iostream> using namespace std; class Base{ public: void m() { cout << "it' ...
- mac 终端 常用指令
开始正式研究ios 应用开发,由于是从C开始学起,所以学习下常用的mac终端指令,方便后续常用操作. mac 终端 常用指令: 1.ls指令 用途:列出文件 常用参数 -w 以简洁的形式列出所有文件和 ...
- MVC数据库数据分页显示
首先从数据库获取数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- (视频)《快速创建网站》 3.3 国际化高大上 - WordPress多语言支持
本文是<快速创建网站>系列的第7篇,如果你还没有看过之前的内容,建议你点击以下目录中的章节先阅读其他内容再回到本文. 访问本系列目录,请点击:http://devopshub.cn/tag ...
- Java字符串中常见的10个问题
下面是Java中10个最常见的关于字符串的问题. 怎样比较字符串?使用==还是equals() 简单的说,“==”用于判断引用是否相等,equals()用于判断值是否相等.除非你要比较两个字符串是否是 ...
- python之递归实现
一.递归函数 概念:递归算法是一种直接或者间接的调用自身算法的过程.在计算机编写程序中,递归算法对解决一大类问题是十分有效的. 特点: ①递归就是在过程或者函数里调用自身. ②在使用递归策略时,必须有 ...
- .NET序列化的一点技巧(附Demo)
阅读目录 介绍 详细 处理 结论 Demo下载 介绍 序列化是将对象状态转换为可保持或传输的形式的过程.序列化的补集是反序列化,后者将流转换为对象.这两个过程一起保证数据易于存储和传输. .NET F ...
- MySQL运行状态show status中文详解(转)
要查看MySQL运行状态,要优化MySQL运行效率都少不了要运行show status查看各种状态,下面是参考官方文档及网上资料整理出来的中文详细解释: 状态名 作用域 详细解释 Aborted_cl ...