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, ...
随机推荐
- 【iScroll源码学习00】模拟iScroll
前言 相信对移动端有了解的朋友对iScroll这个库非常熟悉吧,今天我们就来说下我们移动页面的iScroll化 iScroll是我们必学框架之一,我们这次先根据iScroll功能自己实现其功能,然后再 ...
- cordova 添加闪屏效果
为项目添加SplashScreen插件 在Cordova项目目录运行: cordova plugin add apache.cordova.splashscreen 这个命令从插件git库下载插件代码 ...
- IOS中限制TextField中输入的类型以及长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementSt ...
- 小技巧,如何在Label中显示图片
这个需求其实是有的,比如QQ聊天界面里面发送的信息,可以用label来显示文字(也可以用button显示),但是有时候用户可能会发送图片.如果能让Label遇到文字就显示文字,遇到图片就显示图片就好了 ...
- 访问其他程序的SheardPreferents
程序A: SharedPreferences preferences=getSharedPreferences("myPreferences", Context.MODE_WORL ...
- Android java传递string类型数据给C
本文接着实现<Android java传递int类型数据给C>的还未实现的方法: public native String sayHelloInC(String s); 先贴一个工具方法, ...
- iOS开发200个tips总结(一)
tip 1 : 给UIImage添加毛玻璃效果 func blurImage(value:NSNumber) -> UIImage { let context = CIContext(opti ...
- 网络初见&网络监测
这里需要下载一个第三方 Reachability-master 大家可以百度一下 下载之后把 Reachability拖进来 具体代码如下 #import "ViewController.h ...
- 【原】ios打包ipa的四种实用方法(.app转.ipa)
总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选 ...
- MicroStation VBA 可视化界面
第十章 可视界面 Private Sub UserForm_Initialize() Dim ViewCen As Point3d Dim MyView As View For Each MyView ...