Submission #2529230


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
    int h, w;
    char[][] board;

    public static void main(String args[]) {
        new Main().run();
    }

    void run() {
        FastReader sc = new FastReader();
        h = sc.nextInt();
        w = sc.nextInt();
        board = new char[h][w];
        for (int i = 0; i < h; i++) {
            board[i] = sc.next().toCharArray();
        }
        solve();
    }

    void solve() {
        boolean[][] checkBoard = new boolean[h - 1][w - 1];
        for (int i = 0; i < h - 1; i++) {
            for (int j = 0; j < w - 1; j++) {
                int count = 0;
                if (board[i][j] == '#') {
                    count++;
                }
                if (board[i + 1][j] == '#') {
                    count++;
                }
                if (board[i][j + 1] == '#') {
                    count++;
                }
                if (board[i + 1][j + 1] == '#') {
                    count++;
                }
                if (count % 2 == 0) {
                    checkBoard[i][j] = true;
                }
            }
        }
        int[][] field = new int[board.length][board[0].length - 1];
        for (int i = 0; i < field[0].length; i++) {
            int same = 0;
            int ns = 0;
            for (int j = 0; j < field.length; j++) {
                if (board[j][i] == board[j][i + 1]) {
                    field[j][i] = j + 1 - same;
                    ns = j + 1;
                } else {
                    field[j][i] = j + 1 - ns;
                    same = j + 1;
                }
            }
        }
        System.out.println(largestRectangle(field, '_'));
    }

    static int largestRectangle(int[][] field, int blocked) {
        /*
        int[][] field = new int[board.length][board[0].length];
        for (int i = 0; i < field[0].length; i++) {
            int count = 0;
            for (int j = 0; j < field.length; j++) {
                if (board[j][i] == blocked) {
                    field[j][i] = 0;
                    count = 0;
                } else {
                    field[j][i] = ++count;
                }
            }
        }
        */
        int maximumArea = 0;
        for (int i = 0; i < field.length; i++) {
            Deque<RectPair> stack = new LinkedList<>();
            for (int j = 0; j <= field[0].length; j++) {
                RectPair rp =
                        new RectPair(j < field[0].length ? field[i][j] : 0, j);
                if (stack.isEmpty()) {
                    stack.push(rp);
                } else {
                    if (stack.peek().height < rp.height) {
                        stack.push(rp);
                    } else if (stack.peek().height > rp.height) {
                        int target = j;
                        while (!stack.isEmpty() &&
                                stack.peek().height >= rp.height) {
                            RectPair pre = stack.pop();
                            int area = pre.height * (j + 1 - pre.position);
                            maximumArea = Math.max(maximumArea, area);
                            target = pre.position;
                        }
                        rp.position = target;
                        stack.push(rp);
                    }
                }
            }
        }
        return maximumArea;
    }

    /*
    static int largestRectangle(char[][] board, boolean[][] checkBoard, char blocked) {
        int[][] field = new int[board.length][board[0].length];
        for (int i = 0; i < field[0].length; i++) {
            int count = 0;
            for (int j = 0; j < field.length; j++) {
                if (board[j][i] == blocked) {
                    field[j][i] = 0;
                    count = 0;
                } else {
                    field[j][i] = ++count;
                }
            }
        }
        int maximumArea = 0;
        for (int i = 0; i < board.length; i++) {
            Deque<RectPair> stack = new LinkedList<>();
            for (int j = 0; j <= board[0].length; j++) {
                RectPair rp =
                        new RectPair(j < board[0].length ? field[i][j] : 0, j);
                if (stack.isEmpty()) {
                    stack.push(rp);
                } else {
                    if (stack.peek().height <= rp.height) {
                        stack.push(rp);
                    }
                    if (stack.peek().height > rp.height || ) {
                        int target = j;
                        while (!stack.isEmpty() &&
                                stack.peek().height >= rp.height) {
                            RectPair pre = stack.pop();
                            int startI = i + 1 - pre.height;
                            int startJ = pre.position;
                            boolean flag = true;
                            outer:
                            for (int k = startI; k < i; k++) {
                                for (int l = startJ; l < Math.min(j, board[0].length - 1); l++) {
                                    if (!checkBoard[k][l]) {
                                        target = l + 1;
                                        int area = pre.height * (j - pre.position);
                                        System.out.println("rejected: " + startI + " " + i + " " + startJ + " " + j + " " + area);
                                        flag = false;
                                        break outer;
                                    }
                                }
                            }
                            if (flag) {
                                int area = pre.height * (j - pre.position);
                                System.out.println(startI + " " + i + " " + startJ + " " + j + " " + area);
                                maximumArea = Math.max(maximumArea, area);
                                //target = pre.position;
                            }
                        }
                        rp.position = target;
                        stack.push(rp);
                    }
                }
            }
        }
        return maximumArea;
    }
    */

    static class RectPair {
        int height;
        int position;

        RectPair(int height, int position) {
            this.height = height;
            this.position = position;
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
}

Submission Info

Submission Time
Task F - Flip and Rectangles
User ynish
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 7978 Byte
Status WA
Exec Time 493 ms
Memory 129984 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 38
WA × 2
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, 1.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 2.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 3.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 4.txt, 5.txt, 6.txt, 7.txt, 8.txt, 9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
1.txt WA 86 ms 19028 KB
10.txt AC 451 ms 123104 KB
11.txt AC 337 ms 70176 KB
12.txt AC 350 ms 107964 KB
13.txt AC 407 ms 111236 KB
14.txt AC 414 ms 109656 KB
15.txt AC 388 ms 103260 KB
16.txt AC 384 ms 110120 KB
17.txt AC 389 ms 113392 KB
18.txt AC 455 ms 124132 KB
19.txt AC 403 ms 115352 KB
2.txt AC 75 ms 18260 KB
20.txt AC 402 ms 112904 KB
21.txt AC 397 ms 115592 KB
22.txt AC 406 ms 113364 KB
23.txt AC 403 ms 113756 KB
24.txt AC 337 ms 109856 KB
25.txt AC 323 ms 107348 KB
26.txt AC 459 ms 121224 KB
27.txt AC 427 ms 123760 KB
28.txt AC 436 ms 118804 KB
29.txt AC 403 ms 106788 KB
3.txt WA 457 ms 122012 KB
30.txt AC 395 ms 108968 KB
31.txt AC 389 ms 107112 KB
32.txt AC 374 ms 109608 KB
33.txt AC 352 ms 108980 KB
34.txt AC 356 ms 107896 KB
4.txt AC 441 ms 125100 KB
5.txt AC 85 ms 23508 KB
6.txt AC 75 ms 18388 KB
7.txt AC 465 ms 129984 KB
8.txt AC 493 ms 125872 KB
9.txt AC 446 ms 125068 KB
sample1.txt AC 456 ms 17620 KB
sample2.txt AC 82 ms 18388 KB
sample3.txt AC 73 ms 19028 KB