Sorting
InterviewTime limit1sMemory limit1024 MB
Given a sequence of N integers, find the minimum number of contiguous blocks that can be reordered as whole blocks to produce a sorted sequence.
Problem
A sequence is called sorted if holds for every .
Consider the following way to sort a sequence. First, split the sequence into blocks: the first elements form the first block, the next elements form the second block, and so on, until the last elements form the last block. Then you may swap whole blocks with each other, but you may not change the order of elements inside any block.
Depending on how the blocks are chosen, swapping blocks may or may not be enough to sort the sequence. For example, if the whole sequence is one block, only a sequence that is already sorted can be sorted. Splitting an -element sequence into blocks always works.
Find the minimum such that some split into blocks lets the whole sequence be sorted by swapping blocks.
Input
The first line contains the number of elements (). The second line contains the elements (), separated by spaces.
Output
Print one integer on a single line: the minimum number of blocks that lets the sequence be sorted by swapping blocks.