Algorithm analysis experiment of turning pancakes.

Title Description

McDull favorite food is pancakes, each time in the street to see the pancake stalls will stay there a few minutes. McDull most attractive or pancake chef that single-handedly turn the pancake skilled technique, where a pile of pancakes, master just use a shovel turning a few, let pancake neatly stacked together. That day, in order to celebrate McDull walked to graduate school, where he bought back some of the master from pancakes pancake dinner. But McDull buy pancakes of different sizes, too McDull eat pancakes, and he wanted to eat these pancakes in the largest one. McDull also know the students also like pancakes, in order to show his sincerity, he wanted the students to eat, eat McDull Finally, therefore, McDull want pancakes in ascending order stacked together, big at the bottom . McDull so you can get the biggest slice of that pie pancakes at the last. Now you please help turn the pancake method McDull with McDull pancake master to buy pancakes stacked from small to large. The method of pancake master is inserted with a spatula between two pancakes, the pancake and then turn on a switch shovel, a shovel so that the first turn of the top of the pancake was, and on top of the original turn of the pancakes were just inserted local shovel. McDull want to turn this minimum number of pancakes.

Entry

Input consists of two lines, the first line is an integer n (1 <= n <= 1000), indicates the number of pancakes, the next line is not identical with n integer, with a space between integers, each integer pancake represents the size (diameter), represents the top left, bottom right side represents.

Export

Output line, the minimum number of turning pancakes

Sample input Copy

5
5 4 2 3 1

Sample Output

4 


for the analysis of the sample:
54 231 (maximum pancake at the top, at the bottom of it once inverted, the entire array must reverse) The number of turns: 0
. 1. 3 2 4 5 (maximum pancake has a bottom and then find the second large pancake, also found in the bottom, the bottom of the here refers to the position n-1, followed by the third pancake 3) flip: 1
3 2. 4. 1 5 (maximum pancakes top, a reverse it at the bottom, the entire array must be 3-2 reverse) flip: 2
2 1. 4. 3 5 (maximum at this time becomes a pancake 2, inserted between the shovel 2 and 1, flip a) flip times: 3
. 1 2 4 fifth turnover. 3: 4

Analysis: title marked place for solving the key part, the key question is the position of maximum pancake.
  It may have three positions:
    1, in the top
    2 and the bottom
    3 in the middle
, however, when it is in the middle, we can flip it to the top position, reducing the amount of codes;

#include<iostream>
using namespace std;
int n;
int sum=0;
int a[1005];
int J()
{
    if(n==0) return 0;
    int j=0;int max=0;
    for(int i=0;i<n;i++)
    if(a[i]>=max)
    {
        max=a[i];
        j=i;
    }
    
     IF (J == N- . 1 ) // maximum at the bottom of the pancake 
    { 
        n- - ; 
        J (); 
    } 
    the else  IF (J == 0 ) { // maximum at the top of the pancake, the whole reverse array 
        SUM ++ ;
         for ( int I N- = . 1 ; I> J; i-- ) 
        { 
            int T = A [J]; 
            A [J] = A [I]; 
            A [I] = T; 
            J ++ ; 
        } 
        n- - ;  
        J ( );
    } 
    the else { // maximum pancakes is not at the top, not at the bottom, inverted once, the array part in reverse, so that the maximum pancakes to the top, a step is performed 
        SUM ++ ;
         for ( int I = 0 ; I <J; I ++ ) 
        { 
            int T = A [ I]; 
            A [I] = A [J]; 
            A [J] = T; 
            J - ; 
            
        } 
        J (); 
    } 
} 
int main () { 
    CIN >> n-; 

    for ( int I = 0 ; I < n-; I ++) CIN >> A [I]; 
    J (); 
    COUT << << SUMendl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/solititude/p/12535813.html