[Mock] November August 8

Source: Internet
Author: User

The first question is the square matrix of integers, and the sub-square matrix and the maximum. Returns the largest sum and the width of the sub-square matrix. Because of the topcoder question, it is quite easy to understand the complexity of O (N ^ 3.

Pair <int, int> maxisum (vector <int> & A) {// first is n Second is sum int n =. size (); int retval = int_min; int n = 1; // fix two columns, I, j For (INT I = 0; I <n; I ++) {vector <int> sum (n); For (Int J = I; j <n; j ++) {// calc the sum per line for (int K = 0; k <n; k ++) {sum [k] = sum [k] + A [k] [J];} // calc the sum of the squere with I, j int tmpsum = 0; int W = J-I; for (INT m = 0; m <n; m ++) {If (M <W) {tmpsum + = sum [m];} else if (M = W) {tmpsum + = sum [m]; If (tmpsum> retval) {retval = tmpsum; N = W + 1 ;}} else {tmpsum + = sum [m]; tmpsum-= sum [M-w-1]; If (tmpsum> retval) {retval = tmpsum; n = W + 1 ;}}} return make_pair (n, retval );}

the second question is that there is a matrix of 0 and 1, and the distance from each position to the nearest 0 (similar to the distance from Manhattan) is obtained ). This question is not suitable for dynamic planning at the beginning. It is more like a flood, but if we start from every 0 or 1, the efficiency is very low. N ^ 4 is the same as full enumeration. Later, I thought it was quite similar to the previous topcoder question. I could put all 0 in the queue first, and then expand it out gradually.

Void make (vector <int> & matrix) {int n = matrix. size (); vector <int> Dist; vector <bool> visited; Dist. resize (n); visited. resize (n); For (INT I = 0; I <n; I ++) {Dist [I]. resize (n); visited [I]. resize (n);} queue <pair <int, int> que; For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {If (Matrix [I] [J] = 0) {Dist [I] [J] = 0; visited [I] [J] = true; que. push (make_pair (I, j) ;}} while (! Que. empty () {pair <int, int> PP = que. front (); que. pop (); If (valid (pp. first-1, pp. second, visited) {Dist [pp. first-1] [pp. second] = DIST [pp. first] [pp. second] + 1; que. push (make_pair (pp. first-1, pp. second); visited [] [] = true;} If (valid (pp. first, pp. second-1, visited) {} If (valid (pp. first + 1, pp. second, visited) {} If (valid (pp. first, pp. second + 1, visited) {}} // copy to matrix = DIST;} bool valid (int I, Int J, vector <bool> & visited) {// return whether we need to process this ELEM int n = visited. size (); if (I <0 | j <0 | I> = n | j> = n | visited [I] [J]) {return false;} return true ;}

Code traversing in four directions should be simpler, as shown below:

 
DX [] = {-,} dy [] = {, 0,-1} X + dx [I] Y + dy [I]

The third question was not come up at the time. It is to find the absolute minimum value of the neutron segment and the absolute value of an array. For example, the following array is the smallest absolute value of 1 + 2 +-3.

 
{1, 2,-3,-100,3}

This topic will naturally come up with the best sub-section and practice, but (should) cannot be similar to dynamic planning to O (n ). The key is that it does not meet the characteristics of the optimal subproblem. For example, the optimal solution of the first three elements in the array is 0, and the optimal solution of the first two elements is 2, 0, and the original optimal solution is not used.

The correct method is to record the prefix sum and then sort it to obtain it. Because: | sum [I .. j] | = | P [J]-P [I-1] |, p is the prefix and, after sorting out the order, the adjacent two can obtain the minimum absolute value, note that if I starts from 0, P [-1] does not include 0, that is, 0, and you must add 0 to the prefix and array.

The prefix and array sorting in this question are as follows. There are two zeros in it. One is P [-1] before the start, and the other is P [2]. Then sum [0 .. 2] = 0, the absolute value is the smallest.

 
{-97,-94, 0, 0, 1, 3}

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.