1. 程式人生 > >【Codeforces 375B】Maximum Submatrix 2

【Codeforces 375B】Maximum Submatrix 2

builder cep 找到 runt ioe maximum sel codeforce more

【鏈接】 我是鏈接,點我呀:)
【題意】


如果任意行之間可以重新排序。
問你最大的全是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