flipping pancake problem

McDull's favorite food is pancakes, and every time he sees a pancake stand on the street, he stops there for a few minutes. The most attractive thing about McDull is the skill of the pancake master. There are a bunch of pancakes, and the chef only needs to flip a few times with a shovel to make the pancakes neatly stacked together. On this day, in order to celebrate McDull being sent to graduate school, he bought some pancakes from the pancake master to treat him. But the pancakes McDull bought were different in size. McDull wanted to eat pancakes too much. He wanted to eat the biggest one among them. McDull also knew that his classmates also liked pancakes. In order to show his sincerity, he wanted the students to eat first and McDull last. Therefore, McDull wanted to stack the pancakes in order from small to large, with the bigger ones at the bottom. . This way McDull can get the biggest pancake at the end. Now, please help McDull to stack the pancakes McDull bought from small to large with the method of the pancake master turning the pancakes. The method of the pancake master is to insert a spatula between two pancakes, and then turn the pancake on the spatula, so that the first pancake on the spatula is turned to the top, and the pancake on the top is turned to the top that was just inserted. shovel place. McDull wants to turn the pancakes this way as little as possible.

 

enter

The input consists of two lines. The first line is an integer n (1<=n<=1000), which represents the number of pancakes. The next line contains n different integers. The integers are separated by spaces. Each integer Indicates the size (diameter) of the pancake, with the left side representing the top and the right side representing the bottom.

The output is one line, the minimum number of times to turn the pancake

 

 

Solution: Because the pancake is turned with a shovel, the order inside the pancake will change every time it is turned, which needs attention. If the pancake is on the top, then we only need to flip the pancake once, and turn it to the top, if the pancake is in the middle, then we need to flip it to the top first, and then flip it to the top. Turn it over once and turn it to the bottom. According to this idea, we should have two functions, the first is to find the position of the largest pancake, and the second is to flip the pancake.

So the code is

#include<iostream>
using namespace std;
int findMax(int a[], int n)
{
int max = 0,i;
for (i = 1; i < n; i++)
{
if (a[i]>a[max])
{
max = i;
}
}
return max;
}
void turnover(int a[], int k)
{
int *p = a, *q = a + k - 1;
while (p < q)
{
int t = *p;
*p = *q;
*q = t;
p++;
q--;
}
}

int main()
{
int n, i,sum=0,max=0;
cin >> n;
int *a = new int[n];//N must be written in the parentheses, otherwise a CE error will be reported. Otherwise just remove the parentheses and N

for (i = 0; i < n; i++)
{
cin >> a[i];
}
while (n>0)
{
max = findMax(a, n);
if (max != n - 1)
{
sum += (max == 0 ? 1 : 2);
turnover(a, max + 1);
turnover(a, n);
}
n--;
}
cout << sum << endl;
return 0;


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324977271&siteId=291194637