【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());
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/10474074.html