Problem A
Ascending Photo

The members of the No-Weather-too-Extreme Recreational Climbing society completed their 100th successful summit today! To commemorate the occasion, we took a picture of all the members standing together in one row, to use for marketing purposes.

However, the photograph looks messy; as usual, the members refused to order themselves in any kind of aesthetically pleasing way. We will need to reorder the picture.

\includegraphics[width=0.8\textwidth ]{climbers.jpg}
Figure 1: This picture has been cut up and pasted back together to solve Sample Input 1.

Our research tells us that having the climbers in ascending (non-decreasing) height order from left to right will be most visually appealing. We must cut up the picture we have and somehow paste it back together in this order.

Find the minimum number of cuts you need to make to put the photograph into ascending order.


The input consists of:

  • One line with an integer $n$ ($1 \leq n \leq 10^6 $), the number of people in the photo.

  • One line with $n$ integers $h_1, \ldots , h_ n$ ($1 \leq h_ i \leq 2 \cdot 10^9$ for each $i$), the heights of the people in the photograph, from left to right.


Output the minimum number of cuts needed to rearrange the photograph into any one ascending (non-decreasing) height order from left to right.

Sample Input 1 Sample Output 1
3 6 12 7 7 7 7 8 10 5 5
Sample Input 2 Sample Output 2
5000000 5500000 7000000
Sample Input 3 Sample Output 3
1 2 2 3 3 1 2 3 4 1 2 3

Please log in to submit a solution to this problem

Log in