1.3 issues with one heap of pancakes and 1.3 heap of pancakes

Source: Internet
Author: User

1.3 issues with one heap of pancakes and 1.3 heap of pancakes

The question is as follows:

On Friday evening, a group of colleagues drank a few more drinks at the hard drive bar near the higemma building. What do programmers talk about after a few more drinks? 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 pie in the store is different.I used to set a pile of cakes in order of size before arriving at the customer's dinner table-small and big. Because I held the plate with one hand, I had to use the other hand. I grabbed the top cakes and turned them upside down. After several times, the order is arranged.
I later thought that this is actually an interesting Sorting Problem:Assume thatNFor a pancake of different sizes, How many times should it be turned over to achieve the final size order?


Thoughts:

The method provided in the book is to find the optimal solution through recursion.

There are two recursive end conditions:

1) The number of recursive steps exceeds the maximum;

2) It has been ordered.

However, because there are too many steps in the recursive process, You Need To pruning it:

1) Upper Bound: assume that only the maximum value is pushed to the bottom (similar to the bubble) at a time, and at most two flipped s are required. A total of N-1 ones need to be flipped (the last one does not need to be flipped ), therefore, a maximum of 2 * (n-1) flipped steps are required. Of course, to reduce unnecessary traversal, each time you find a step that achieves an ordered number of steps, update max to this step.

2) lower bound: calculates the minimum number of times that the current State needs to be flipped during each recursion and is set to lowerbound, if the number of currently flipped steps step + lowerbound (number of flipped steps + at least the number of required steps in the current State) exceeds the upper limit, this recursion can be ended.


Code:

Class CPrefixSorting {public: CPrefixSorting () {m_nCakeCnt = 0; m_nMaxSwap = 0 ;}~ CPrefixSorting () {if (m_CakeArray! = NULL) delete m_CakeArray; if (m_SwapArray! = NULL) delete m_SwapArray; if (m_ReverseCakeArray! = NULL) delete m_ReverseCakeArray; if (m_ReverseCakeArraySwap! = NULL) delete m_ReverseCakeArraySwap;} // pCakeArray storage pancake Index Array // nCakeCnt pancake count void Run (int * pCakeArray, int nCakeCnt) {Init (pCakeArray, nCakeCnt ); m_nSearch = 0; Search (0);} // number of times the pancake is flipped void Output () {for (int I = 0; I <m_nMaxSwap; ++ I) cout <m_SwapArray [I] <"; cout <" Search times: "<m_nSearch; cout <" Total swap times: "<m_nMaxSwap;} private: // pCakeArray storage pancake Index Array // nCakeCnt pancake count void Init (I Nt * pCakeArray, int nCakeCnt) {assert (pCakeArray! = NULL); assert (nCakeCnt> 0); m_nCakeCnt = nCakeCnt; // initialize the pancake array m_CakeArray = new int [nCakeCnt]; assert (m_CakeArray! = NULL); for (int I = 0; I <nCakeCnt; ++ I) m_CakeArray [I] = pCakeArray [I]; // set the maximum number of exchanges m_nMaxSwap = UpperBound (m_nCakeCnt); // initialize the exchange result array m_SwapArray = new int [m_nMaxSwap + 1]; assert (m_SwapArray! = NULL); // initialize the intermediate exchange result information m_ReverseCakeArray = new int [m_nCakeCnt]; for (int I = 0; I <m_nCakeCnt; ++ I) m_ReverseCakeArray [I] = m_CakeArray [I]; reverse = new int [m_nMaxSwap];} // search for the upper bound int UpperBound (int nCakeCnt) {return nCakeCnt * 2 ;} // find the lower bound int LowerBound (int * pCakeArray, int nCakeCnt) of the current flip {int ret = 0; // determine the minimum number of for (int I = 1; I <nCakeCnt; ++ I) based on the sorting information of the current array) {// determine whether the two adjacent pancakes are sorted by the size of the adjacent int t = pCakeArray [I]-pCakeArray [I-1]; if (t = 1 | t =-1) {} else ++ ret;} return ret;} void Search (int step) {m_nSearch ++; // estimate the minimum number of exchanges required for this search int nEstimate = LowerBound (m_ReverseCakeArray, m_nCakeCnt); if (step + nEstimate> m_nMaxSwap) return; // if the order has been sorted, that is, if (IsSorted (m_ReverseCakeArray, m_nCakeCnt) {if (step <m_nMaxSwap) {m_nMaxSwap = step; for (int I = 0; I <m_nMaxSwap; ++ I) m_SwapArray [I] = m_ReverseCakeArraySwap [I];} return ;}// recursively flip for (int I = 1; I <m_nCakeCnt; ++ I) {Reverse (0, I); m_ReverseCakeArraySwap [step] = I; search (step + 1); Reverse (0, I) ;}} bool IsSorted (int * pCakeArray, int nCakeCnt) {for (int I = 1; I <nCakeCnt; ++ I) {if (pCakeArray [I-1]> pCakeArray [I]) return false;} return true ;} // void Reverse (int nBegin, int nEnd) {assert (nBegin <nEnd); // flip the pancake information for (int I = nBegin, j = nEnd; I <j; ++ I, -- j) {int temp = m_ReverseCakeArray [I]; m_ReverseCakeArray [I] = m_ReverseCakeArray [j]; m_ReverseCakeArray [j] = temp ;}} private: int * m_CakeArray; // pancake information array int m_nCakeCnt; // Number of pancakes int m_nMaxSwap; // The maximum number of exchanges, based on the previous inference, here it can be m_nCakeCnt * 2int * m_SwapArray; // exchange result array int * m_ReverseCakeArray; // The current flat information array int * m_ReverseCakeArraySwap; // The current flat result array int m_nSearch; // current search times };


# Include <iostream> using namespace std; # define NMAX 1000int UpperBound (int nCakeCnt); void Search (int step); int LowerBound (int * nCakeArray, int nCakeCnt ); bool IsSorted (int * nCakeArray, int nCakeCnt); void Reverse (int nBegin, int nEnd); int nCakeCnt; // The number of moon cakes stored in int nCake [NMAX]; // The radius of the moon cake int nReverseCakeArray [NMAX]; // The intermediate result int nMaxSwap = UpperBound (nCakeCnt) of the moon cake turning process ); // maximum number of flipped times int nReverseCakeArraySwap [NMAX]; // records int totalAns = 0 in this array each time it is flipped; // total number of flipped times int nSwapArray [NMAX]; // void Search (int step) {totalAns ++; int nEstimate = LowerBound (nReverseCakeArray, nCakeCnt ); if (step + nEstimate> nMaxSwap) return; if (IsSorted (nReverseCakeArray, nCakeCnt) {if (step <nMaxSwap) {nMaxSwap = step; for (int I = 0; I <nMaxSwap; ++ I) nSwapArray [I] = nReverseCakeArraySwap [I];} return ;}for (int I = 1; I <nCakeCnt; ++ I) {Reverse (0, I); nReverseCakeArraySwap [step] = I; Search (step + 1); Reverse (0, I) ;}} int UpperBound (int nCakeCnt) {return 2 * nCakeCnt;} int LowerBound (int * nCakeArray, int nCakeCnt) {int ret = 0; for (int I = 1; I <nCakeCnt; ++ I) {int t = nCakeArray [I]-nCakeArray [I-1]; if (t = 1 | t =-1) {} else ++ ret;} return ret ;} bool IsSorted (int * nCakeArray, int nCakeCnt) {for (int I = 1; I <nCakeCnt; ++ I) if (nCakeArray [I] <nCakeArray [I-1]) return false; return true;} void Reverse (int nBegin, int nEnd) {for (int I = nBegin, j = nEnd; I <j; ++ I, -- j) {int temp = nReverseCakeArray [I]; nReverseCakeArray [I] = nReverseCakeArray [j]; nReverseCakeArray [j] = temp ;}} int main () {cin> nCakeCnt; for (int I = 0; I <nCakeCnt; ++ I) {cin> nCake [I]; nReverseCakeArray [I] = nCake [I];} Search (0 ); cout <totalAns <endl ;}




Ask a pancake question

The number of slices of a pie is divided by 5 by 2. If there is an remainder, it is regarded as 1 minute.
For example, divide 3 pie slices by 5 = 15 15 by 2 = 7 .... 1 7 + 1 = 8 points

Alimama's pancakes can be branded every minute. At one time, they can be branded on both sides. Four people can buy pancakes at the same time, respectively (you can add questions later)

Give two, four, three, and the last five.

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.