UVa120 Stacks of Flapjacks (construction method)

Source: Internet
Author: User

Links: http://vjudge.net/problem/18284

Analysis: The problem of pancake-sharing. In order to make each number in the correct position from large to small, such as when dealing with the big I pancake, it will not affect the size of the 1,2,3,...,i-1 pancake (they have correctly turned to the bottom of the pancake heap i-1 position), the way to turn the pancake is to first turn to the top, and then turn to the right position, After the pancake piled bottom I position on the pancake has been properly put out of the tube, the next is to find the i+1 big pancake placed on the next number on the i+1 position, but for the current maximum pancake position to be classified discussion, if already in its correct position on nothing to do, if at the top, Just turn it over to the right position, or turn to the top and then turn to the right position.

1#include <string>2#include <iostream>3#include <sstream>4 using namespacestd;5 6 Const intMAXN = -+5;7 8 intN, A[MAXN];9 Ten voidFlipintp) { One      for(inti =0; i < p-i; i++) ASwap (A[i], a[p-i]); -printf"%d"Np); - } the  - intMain () { -     strings; -      while(Getline (CIN, s)) { +cout << S <<Endl; - StringStream SS (s); +n =0; A          while(SS >> A[n]) n++; at          for(inti = n-1; i >0; i--) { -             intMAXV = a[i], index =i; -              for(intj = i-1; J >=0; j--) -                 if(A[j] >MAXV) { -MAXV =A[j]; -index =J; in                 } -             if(index = = i)Continue; to             if(Index >0) Flip (index); + Flip (i); -         } theprintf"0\n"); *     } $     return 0;Panax Notoginseng}

UVa120 Stacks of Flapjacks (construction method)

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.