【Codeforces 375B】Maximum Submatrix 2
【链接】 我是链接,点我呀:)
【题意】
如果任意行之间可以重新排序。
问你最大的全是1的子矩阵中1的个数
【题解】
设cnt[i][j]
表示(i,j)这个点往右连续的1的个数
我们枚举列j
然后对于第j列的cnt[1..n][j]
我们把这n个数字排个序(升序)
然后顺序枚举这n个数字
假设我们枚举到了第i个数字,显然第i~n这n-i+1个数字是能组成一个宽为cnt[i][j]长为n-i+1的矩形的
且这些矩形的左边界都是i
这就是这道题的技巧所在了
找到最短的宽度,让比它长的都和它适应
秒啊
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)5000;
static class Task{
int n,m;
String s[] = new String[N+10];
int cnt[][] = new int[N+10][N+10];
int col[][] = new int[N+10][N+10];
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();m = in.nextInt();
for (int i = 1;i <= n;i++) {
s[i] = in.next();
StringBuilder sb = new StringBuilder(s[i]);
sb.insert(0, ' ');
s[i] = sb.toString();
}
for (int i = 1;i <= n;i++) {
for (int j = m;j >= 1;j--) {
if (s[i].charAt(j)=='0') {
cnt[i][j] = 0;
}else {
cnt[i][j] = cnt[i][j+1]+1;
}
}
}
for (int j = m;j >= 1;j--) {
for (int i = 1;i <= n;i++)
{
col[j][i] = cnt[i][j];
}
}
long ans = 0;
for (int i = 1;i <= m;i++) {
Arrays.sort(col[i],1,n+1);
for (int j = 1;j <= n;j++) {
ans = Math.max(ans,col[i][j]*(n-j+1));
}
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
【Codeforces 375B】Maximum Submatrix 2的更多相关文章
- 【codeforces 509A】Maximum in Table
[题目链接]:http://codeforces.com/contest/509/problem/A [题意] 给你一个递推式f[i][j] = f[i-1][j]+f[i][j-1]; 让你求f[i ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【39.66%】【codeforces 740C】Alyona and mex
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.33%】【codeforces 557B】Pasha and Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【55.70%】【codeforces 557A】Ilya and Diplomas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【36.86%】【codeforces 558B】Amr and The Large Array
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【24.17%】【codeforces 721D】Maxim and Array
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- G41
https://downloadcenter.intel.com/zh-cn/product/81517/-G41-
- 13、git
安装Git 网上有很多Git安装教程,如果需要图形界面,windows下建议使用TortoiseGit,linux建议使用Git GUI或者GITK.(windows下载exe安装包,linux可以使 ...
- python自动化学习笔记10-数据驱动DDT与yml的应用
在测试工作中,针对某一API接口,或者某一个用户界面的输入框,需要设计大量相关的用例,每一个用例包含实际输入的各种可能的数据.通常的做法是,将测试数据存放到一个数据文件里,然后从数据文件读取,在脚本中 ...
- 一行python能做什么!
主要收集了平常遇到的代码和网上的简单题目,然后尝试将代码压缩到一行,仅仅是娱乐一下~~~ −−−−−(1)−−−−−−−−−−−(1)−−−−−− 用一行python写出一个嵌套的字符串. def p ...
- native2ascii命令详解
1.native2ascii简介: native2ascii是sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt,*.ini,*.properties,*.java等等 ...
- 震惊!double输入输出的秘密竟然是~
遇到了一个神奇的事情: double r = 3.0; printf("%lf", r);//0.000000 double遇到printf函数竟然是用%f输出的! scanf函数 ...
- RabbitMQ~广播消息
定义 广播消息是指生产者产生的消息将分发给所有订阅这个消息的消费者,而普通的模式是:一批消息可以被多个人共同消费,如consumer1可能消费1,3,5记录,而consumer2可能消费的是2,4,6 ...
- CentOS 7.4 下搭建 Elasticsearch 6.3 搜索群集
上个月 13 号,Elasticsearch 6.3 如约而至,该版本和以往版本相比,新增了很多新功能,其中最令人瞩目的莫过于集成了 X-Pack 模块.而在最新的 X-Pack 中 Elastics ...
- java多线程(线程通信-等待换新机制-代码优化)
等待唤醒机制涉及方法: wait():让线程处于冻结状态,被wait的线程会被存储到线程池中. noticfy():唤醒同一个线程池中一个线程(任意也可能是当前wait的线程) notifyAll() ...
- nw.js开发第一个程序(html开发桌面程序exe)
一.环境配置 windows系统 cnpm install node 下载nw.js https://github.com/nwjs/nw.js 找到download下载合适的版本 二.开发 项目目录 ...