Ultraviolet () problem 120 stacks of flapjacks (pancake heap)

Source: Internet
Author: User
Tags dot net
// 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 ;}

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.