Ascending Photo
Time limit3sMemory limit512 MB
Given a sequence of n heights, find the minimum number of cuts so the pieces can be reordered into a nondecreasing sequence.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, Dynamic programming, Array
- Solved
- No attempts yet
Problem
An amateur climbing club finished its 100th summit today. To mark the occasion, every member lined up in a single row for one photo.
The row is a mess, because the members stood wherever they felt like standing. We want to rearrange the photo so that the heights never decrease from left to right.

Figure: the photo after it was cut up and pasted back together to produce the answer to the first example.
The only thing we can do is cut the printed photo vertically between two neighbouring people and paste the resulting strips back together in any order. The people inside one strip keep their order.
Find the minimum number of cuts needed to paste the strips into one row whose heights never decrease from left to right.
Input
- One line with the number of people in the photo, ().
- One line with integers , the heights of the people from left to right ().
Output
Output the minimum number of cuts needed to paste the strips into one row whose heights never decrease from left to right.