Topcoder SRM 610 div2 thematrix

Source: Internet
Author: User

The question is to give a string array of 01 so that you can solve the maximum board meeting the Board conditions.

The conditions for the Board are:

Values of adjacent elements cannot be the same

This question is a bit like the largest sub-matrix of the entire 1. At that time, the largest sub-matrix of the entire 1 was solved using a histogram.

Histogram can be used to solve this question.

First, find the two vertex coordinates of the Child matrix (x0, y0), (x1, Y1)

We can traverse the start and end columns, Y0 = I, Y1 = J. We can find the largest chessboard that meets the conditions between y0 and Y1, that is, find the child matrix with the highest height meeting the condition between y0 and Y1.

Then traverse I and j to get the maximum value.

For a child row that meets the condition, there are two cases: 010101 and 10101010 .....,

Define 0 as 010101... rows in this way, define 1 as 1010101... rows in this way, define x as other methods that do not meet the conditions

In this way, the matrix of each I and j can be expressed as row [] = "010xx10101x11000 ",

Row [0] = '0', representing 0th rows as 01010101...

Row [1] = '1', representing 1st rows as 10101010 .....

Row [3] = 'x', indicating that 3rd rows have adjacent elements, that is .... 00 ..... or ..... 11 ....... status

In this way, we only need to find the highest adjacent element in the row, the height of the different sequence above is 10101, that is, the maximum height is 5, then multiply by the width (J-I + 1), that is, the current maximum board between I and j

Then find the largest of all the boards.

    int MaxArea(vector <string> board) {        int res = 0 , w = board[0].length(), h = board.size();        for(int i = 0 ; i < w; ++ i){            for(int j = i ; j < w; ++ j){                char row[h];                for(int k = 0; k < h; ++ k){                    bool flag = true;                    for(int r = i + 1; r <= j ; ++ r){                        flag = flag && (board[k][r]!=board[k][r-1]);                    }                    row[k] = (flag ? board[k][i] : ‘X‘);                }                int maxHeight = 0, cntHeight = 0;                for(int k = 0 ; k < h ; ++ k){                    if(row[k] == ‘X‘) cntHeight =0;                    else if((cntHeight > 0) && row[k] != row[k-1]) cntHeight ++;                    else cntHeight = 1;                    maxHeight = max(cntHeight,maxHeight);                }                res = max(res,maxHeight*(j-i+1));            }        }        return res;    }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.