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, ...
随机推荐
- JavaScript强化教程——jQuery UI API 类别
---恢复内容开始--- 主要介绍:JavaScript强化教程—— jQuery UI API 类别 jQuery UI 在jQuery 内置的特效上添加了一些功能.jQuery UI 支持颜色动 ...
- SharePoint 2013 开发文档管理字段小记
前言 最近有这样一个需求,就是要求在列表库里管理文档,需要多文档管理.带版本控制.可以单独授权等基本操作.于是乎,就开发了一个自定义段,这里介绍一下字段的思路,里面有一些遇到的问题,在群友的帮助下已解 ...
- Android 抽屉效果的导航菜单实现
Android 抽屉效果的导航菜单实现 抽屉效果的导航菜单 看了很多应用,觉得这种侧滑的抽屉效果的菜单很好. 不用切换到另一个页面,也不用去按菜单的硬件按钮,直接在界面上一个按钮点击,菜单就滑出来,而 ...
- UISlider相关
设置slider当前位置的图像 [slider setThumbImage:[UIImage imageNamed:@"dd.png"] forState:UIControlSta ...
- 属性观察器willSet与didSet
willSet与didSet是swift特有的,就是监听属性值的变化,但有一个小注意点. willSet与didSet调用时机:对象创建后的第一次赋值时开始调用.也就是说,初始化时(包含重载的init ...
- ios网络编程学习
//网络访问获取数据//定义一个UIWebView属性,用来展示数据 @property (strong, nonatomic) IBOutlet UIWebView *myWebView; //.. ...
- Android studio 如何查看当前git 分支的4种方式
1.第一种 2.第二种 3.第三种 4.第四种 前面3种都是通过android studio 操作的. 第四种是通过命令行操作.(可以在 git bash 中输入命 ...
- JNI输出log信息
1.修改Android.mk 如生成的库文件是“.so文件”,则在Android.mk中添加如下内容: LOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -llog 如生成的库文件 ...
- jQuery添加options点击事件并传值
说明: 根据选择不同店铺选项,上送不同id值,展示不同商品列表 var formStr = "{'supplierId':'供应链企业|%-jm-sprt-%|93794498-3'}& ...
- mysql访问连接过多
今天开发中启动服务器,发现不管怎么样都会报连接池已满,随后删除数据库中的连接,发现可以启动,后来关闭后重新启动又出现连接池已满的错误.后监控数据库发现,当我关闭服务的时候,数据库的连接并没有关闭,随后 ...