On Friday evening, a group of colleagues drank a few more cups at the hard drive bar near the higemma building. What do programmers talk about after they drank a few more cups? Naturally, it is an algorithm.
Problem. A colleague said:
"I used to work in a restaurant and customers often ordered a lot of pancakes. The size of the pancakes in the store is different. before arriving at the customer's dinner table
Set the order --- small and big. Because I held the plate with one hand, I had to use the other hand to grab the top few cakes at a time and put them
Turn it upside down. After several times, the order of the pancakes will be arranged. I later thought that this is actually an interesting Sorting Problem: assume there are n pieces of size.
If there are different slices of cake, How many times should we flip at least to achieve the ordered results ?"
From this section, we can easily abstract the problem: Give you an array consisting of n consecutive integers, the array is unordered, now you need
This array is sorted in ascending order, but only one operation can be performed on the array, that is, it can only start from the first element of the array to any element of the array
All the elements of the image are flipped. In this case, the problem is not very troublesome. We also need to write the program to output the optimal flip process.
# Include <stdio. h> # include <stdlib. h> # include <assert. h> class cprefixsorting {public: int * m_swaparray; int * m_swaptemparray; int * m_pietemparray; int m_npiecount; int * m_piearray; int m_nmaxswapcount; public: cpresorfixting () {} virtual ~ Cprefixsorting () {} public: void output () {for (INT I = 0; I <m_nmaxswapcount; I ++) {printf ("% d \ n ", m_swaparray [I]) ;}/ * traverse all solutions */bool search (INT nstep) {/* pruning: If it is worse than the most common solution, simply discard */INT nmintimes = lowerbound (m_pietemparray, m_npiecount); If (nstep + nmintimes> m_nmaxswapcount) {return false;} If (issorted ()) {/* If a better solution is found, save this better solution */If (nstep <m_nmaxswapcount) {m_nmaxswapcount = nstep; For (INT I = 0; I <m _ Nmaxswapcount; I ++) {m_swaparray [I] = m_swaptemparray [I] ;}} return true ;}/ * after reversing this pancake, traverse all possible upside down situations next */For (INT I = 1; I <m_npiecount; I ++) {reverse (0, I); m_swaptemparray [nstep] = I; search (nstep + 1); reverse (0, I) ;}} bool issorted () {for (INT I = 0; I <m_npiecount-1; I ++) {If (m_pietemparray [I]> m_pietemparray [I + 1]) {return false ;}return true ;} void Init (int * ppiearray, int npiecount) {assert (null! = Ppiearray); Assert (0 <npiecount); m_piearray = new int [npiecount]; Assert (null! = M_piearray); m_pietemparray = new int [npiecount]; Assert (null! = M_pietemparray); m_npiecount = npiecount; For (INT I = 0; I <npiecount; I ++) {m_piearray [I] = ppiearray [I]; m_pietemparray [I] = ppiearray [I];} m_nmaxswapcount = upperbound (npiecount); m_swaparray = new int [m_nmaxswapcount]; Assert (null! = M_swaparray); m_swaptemparray = new int [m_nmaxswapcount]; Assert (null! = M_swaptemparray);} void reverse (INT nbegin, int nend) {assert (nbegin <nend); int I, j, t; for (I = nbegin, j = nend; I <j; I ++, j --) {T = m_pietemparray [I]; m_pietemparray [I] = m_pietemparray [J]; m_pietemparray [J] = T ;}} void run (int * ppiearray, int npiecount) {Init (ppiearray, npiecount); search (0); output () ;}int upperbound (INT npiecount) {return (npiecount-1) * 2;} int lowerbound (int * ppiearray, int npiecount) {int ret = 0, T; For (INT I = 0; I <m_npiecount-1; I ++) {T = ppiearray [I]-ppiearray [I + 1]; If (t = 1 | T =-1) {continue;} RET ++;} return ret ;}}; int main () {cprefixsorting stsort; int ppiearray [10] =, 7,9}; stsort. run (ppiearray, 10); System ("pause"); Return 0 ;}