It is New Year's Day and people are in line for the Wonderland rollercoaster ride. Each person wears a sticker indicating their initial position in the queue. Initial positions increment by from at the front of the line to at the back.
Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others. For example, if and bribes , the queue will look like this: .
Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state. If anyone has bribed more than two people, the line is too chaotic to compute the answer.
Complete the function minimumBribes in the editor below.
minimumBribes has the following parameter(s):
int q[n]: the positions of the people after all bribes
No value is returned. Print the minimum number of bribes necessary or Too chaotic if someone has bribed more than people.
The first line contains an integer , the number of test cases.
Each of the next pairs of lines are as follows:
- The first line contains an integer , the number of people in the queue
- The second line has space-separated integers describing the final state of the queue.
2 1 5 3 4
2 5 1 3 4
Test Case 1
The initial state:
After person moves one position ahead by bribing person :
Now person moves another position ahead by bribing person :
And person moves one position ahead by bribing person :
So the final state is after three bribing operations.
Test Case 2
No person can bribe more than two people, yet it appears person has done so. It is not possible to achieve the input state.