// Stacks of flapjacks (pancake heap) // PC/Ultraviolet IDs: 110402/120, popularity: B, success rate: high level: 2 // verdict: accepted // submission date: 2011-05-22 // UV Run Time: 0.028 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // sorts data first, and then changes the original sequence to the final sequence based on the sorted result. During sorting, because there may be two/s with the same diameter, You need to record the data sequence number. The restoration method is to find the X that is not yet sorted, first flip it to the top, // then flip it to its position after the final sorting. # Include <iostream> # include <sstream> # include <algorithm> using namespace STD; # define maxsize 30 struct pancake {int diameter; int index ;}; pancake pancakes [maxsize]; pancake original [maxsize]; bool CMP (pancake X, pancake y) {return X. diameter <Y. diameter;} void flip (INT POs, int size) {pancake TMP; int I = 0, j = size-Pos; For (; I <j; I ++, J --) if (original [I]. diameter! = Original [J]. diameter) {TMP = original [I]; original [I] = original [J]; original [J] = TMP ;}} int main (INT AC, char * AV []) {string line; while (Getline (CIN, line) {// echo. Cout <line <Endl; // read data. Int capacity = 0; istringstream ISS (line); While (ISS> pancakes [capacity]. diameter) {pancakes [capacity]. index = capacity; original [capacity] = pancakes [capacity]; Capacity ++;} // sort. Sort (pancakes, pancakes + capacity, CMP); // perform the flip operation. If the I-th element is not on the I-th, locate its serial number first, then // flip it to the top, and then flip it to position I. For (INT I = capacity-1; I> = 0; I --) {// locate this element in the current sequence. // If the original number does not match the current number, you need to flip it. Int marker; For (Int J = 0; j <capacity; j ++) if (original [J]. index = pancakes [I]. index) {marker = J; break;} If (marker! = I) {If (marker! = 0) {cout <(Capacity-marker) <""; flip (Capacity-marker, capacity);} cout <(Capacity-I) <""; flip (Capacity-I, capacity) ;}} cout <"0" <Endl;} return 0 ;}