The problem of geographical division

Source: Internet
Author: User


Title Description: 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" j_0003.gif "/>


Now there is a long strip of land that we can consider to be connected by n squares (these squares can be numbered from 1 to n). And we need to divide it into two parts, planted in different crops (i.e. crops A and b), the division must be between a two small squares, or at the far left or right end of the land, if the division between block I to block i+1, then divide, 1th to block I to plant a, the remaining land species B. Now some experts have tested the land and each of them has assessed the crops that are suitable for each piece of land. Please find a suitable division that best matches the assessment of all experts, that is, the number of times you divide A and the expert evaluates to B and the number of times you divide to B and the expert evaluates to a is the smallest.


Input Description:
Each set of data is given an expert assessment table land (where 0 is evaluated a,1 for evaluation B), and the number of small blocks n (1≤n≤300), the number of expert evaluations m (1≤m≤300)



Output Description:
Please return to your division, namely I and i+1. If at the leftmost end, the output is 0, 1, and the n,n+1 is not matched at the right end. If there is more than the output of the left-most division.


Input Example:
[[1,1,1,1],[0,0,0,0],[1,0,1,1]],4,3


Output Example:
[0,1]


Method declaration:

Vector<int> getpartition (const vector<vector<int> >& Land, int n, int m) {//write code here}



Analysis:

According to the topic, we need to find a boundary, the left side of the boundary is 0 , the right side is 1 , and this division must be the same as the expert evaluation of the results of different land (that is, squares) The least number,


The following example:

Input:

[[1,1,1,1],[0,0,0,0],[1,0,1,1]],4,3

That is, 4 plots of land, 3 evaluations.

Then all the cases are as follows:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/7F/7D/wKioL1cgjIuC_7uAAAAaUY0ul40018.jpg "title=" 1.jpg " alt= "Wkiol1cgjiuc_7uaaaaauy0ul40018.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7F/7F/wKiom1cgjB-Aejd5AAAaPnSBzW0900.jpg "title=" 2.jpg " alt= "Wkiom1cgjb-aejd5aaaapnsbzw0900.jpg"/>

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7F/7F/wKiom1cgjFyhC-QHAAAaOlo0zSw705.jpg "title=" 3.jpg " alt= "Wkiom1cgjfyhc-qhaaaaolo0zsw705.jpg"/>


Add up the number of non-matching land experts to assess each case:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7F/7F/wKiom1cgjX2zTfH5AAAT5OsdIzw326.jpg "title=" 4.jpg " alt= "Wkiom1cgjx2ztfh5aaat5osdizw326.jpg"/>

In the case of the same number of land that does not match (as above 1111 and 0011), the leftmost division , or 1111, is divided into 0,1


One more example:

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/7F/7D/wKioL1cgkWeQPK7nAAAH639wOuY448.jpg "title=" 6.jpg " alt= "Wkiol1cgkweqpk7naaah639wouy448.jpg"/>

All cases are as follows:

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7F/7D/wKioL1cgkYyxZTdbAAAwDdHBbiE460.jpg "title=" 7.jpg " alt= "Wkiol1cgkyyxztdbaaawddhbbie460.jpg"/>

650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/7F/80/wKiom1cgk-zylNeKAAAyrIZxEYg621.jpg "title=" 8.jpg " alt= "Wkiom1cgk-zylnekaaayrizxeyg621.jpg"/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7F/80/wKiom1cgkUny5xONAAAjJiCLBP8981.jpg "title=" 9.jpg " alt= "Wkiom1cgkuny5xonaaajjiclbp8981.jpg"/>

So the division is: 1, 2


According to the above thought, writing code is much easier: 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0016.gif "alt=" J_0016.gif "/>


Vector<int> getpartition (Const vector<vector<int> >& land, int  n, int m) {int* a = new int[n];for  (int i = 0; i  < n; ++i)   //all 1, starting from the full  1  case {a[i] = 1;} int left = 0;//division of the left boundary int right = 1;      // The right boundary of the plan points int mindif = n*m;   //the fewest number of non-coincident lands int difcount = 0;    //is used to record the number of int zerocount = 0;  //in a sequence that does not match the number of  0  int dif  = 0;        //number of for  (int index = 0;) For each case of non-matching land  index < n + 1; ++index)   //Altogether there are n+1 species possible combinations {for  (int i =  0; i < m; ++i) {for  (int j = 0; j < n;  ++J) {if  (Land[i][j] != a[j]) + +Difcount;} dif += difcount;difcount = 0;} if  (DIF&NBSP;&LT;=&NBSP;MINDIF) {if  (dif < mindif) {mindif = dif;left =  zerocount;right = zerocount + 1;} else if  (Zerocount < left) {left = zerocount;right = zerocount  + 1;}} dif = 0;if  (Zerocount < n) a[zerocount++] = 0;} Delete[] a;a = null;vector<int> ret;ret.push_back (left); Ret.push_back (right); return  ret;}


This article from the "11408774" blog, reproduced please contact the author!

The problem of geographical division

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.