Discussion on a pile of pancakes in the beauty of Programming

Source: Internet
Author: User
Discussion on a pile of pancakes in the beauty of Programming

This type of problem I first encountered was a problem where the chef had to arrange the dishes on a shelf from big to small, and I had to flip the dishes as I did in this article, at that time, I gave the following answer:

Int findmaxidx (int * pdishes, int nbegin, int nend)
{
Int I, maxidx = nbegin;
For (I = nbegin + 1; I <= nend; I ++)
{
If (pdishes [I]> pdishes [maxidx])
{
Maxidx = I;
}
}
Return maxidx;
}

Void revert (int * pdishes, int nbegin, int nend)
{
Int I, j, TMP;
Assert (nend> nbegin );
For (I = nbegin, j = nend; I <j; I ++, j --)
{
TMP = pdishes [I];
Pdishes [I] = m_cakearray [J];
Pdishes [J] = TMP;
}
}

Int prefixsort (int * pdishes, int fjcount)
{
Int I, curmax;
Int swapcount = 0;
For (I = fjcount-1; I> 0; I --)
{
Curmax = findmaxidx (pdishes, 0, I );
If (curmax = 0)
{
Revert (pdishes, 0, I );
Swapcount ++;
}
Else if (curmax! = I)
{
Revert (pdishes, 0, curmax );
Revert (pdishes, 0, I );
Swapcount + = 2;
}
Else
{
;
}
}

Return swapcount;
}

The prefixsort () function sorts the array and returns the number of flipped ings. The idea of this Code is to first locate the position of the largest tray before the position, then, use one or two flip operations to change the largest plate to this position, move the plate one or two times, and repeat the above process. For a relatively long period of time, I thought this was the optimal solution, because it conforms to the limit of less than 2 * (fjcount-1), and can avoid flip for partial ordered dishes, what if my classmates tell me that the direction of partial ordered dishes is reversed? That is to say, how can we take advantage of the fact that there are already several dishes in order of size, but the order is the opposite of the order? Is there a way to reduce the number of flipped ings in partial order but reverse order? Later, I didn't think of a good method, so I had to solve this problem by using the exhaustive method. The introduction to algorithms calls this method of exhaustion + conditional judgment as the branch boundary method. Later I saw a doctoral thesis, saying that dynamic planning can be used to solve this optimization problem. I found the description of dynamic planning from introduction to algorithms, using Dynamic Programming to solve the problem requires the problem domain to have the optimal subproblem. Using the optimal subproblem's local optimal solution heap the optimal solution of the entire problem domain, however, the method for decomposing the optimal subproblem has not been found, let's stop. When I read this book in the bookstore, I was very much looking forward to seeing that I could use the dynamic planning method. But I was very disappointed to go home and see that the final solution still adopted the method of trying to solve all the problems, I don't know who can give pointers on how to use dynamic planning to solve this problem.

As mentioned above, the number of errors in this book is remarkable, especially for the code in this example. m_arrswap and M_n can be understood as printed errors, but Init () in the function, I refreshed four new memories and never saw any space for releasing them. I couldn't figure out what happened. Isn't it Java's habit to write this code? Although the official team provided an error description and added an destructor to release the memory, it can be seen that the author is not confused, should these four pointers be initialized to null in the constructor at least?

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.