No.365 Jenga Sort
Problem Statement
$N$ blocks are lined up in a horizontal row. The blocks have lengths $1, 2, ..., N$, but they are in no particular order.
yurahuna has decided to rearrange the blocks so that they are in ascending order of length when viewed from the left.
However, the only operation yurahuna can perform is "choose one block and move it to the leftmost position".
(If the leftmost block is chosen, the arrangement of the blocks does not change after the operation, but this still counts as one operation.)
What is the minimum number of operations yurahuna needs to rearrange the blocks?
Input
$N$ $a_1$ $a_2$ ... $a_N$
- The first line contains the number of blocks $N$ $(1 \leq N \leq 100000)$.
- The second line contains the block lengths $a_i$ $(1 \leq i \leq N, 1 \leq a_i \leq N)$ as space-separated integers.
$a_i$ represents the length of the $i$-th block from the left. - All block lengths are distinct. That is, $a_i \neq a_j$ for all $i, j$ $(1 \leq i < j \leq N)$.
Output
Output on one line the minimum number of operations yurahuna needs to rearrange the blocks.
Print a newline at the end.
Sample
Sample 1
Input
5 3 1 4 2 5
Output
2
First choose the block of length 2 and move it to the leftmost position, then choose the block of length 1 and move it to the leftmost position; the blocks are then in ascending order of length when viewed from the left. This is a rearrangement that uses the minimum number of operations.
3 1 4 2 5
↓
2 3 1 4 5
↓
1 2 3 4 5
Sample 2
Input
4 4 3 2 1
Output
3
Choosing and operating on the blocks of length 3, 2, 1 in this order rearranges them in the minimum number of operations.
Sample 3
Input
4 1 2 3 4
Output
0
The blocks are already in ascending order of length when viewed from the left, so yurahuna does not need to perform any operations.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
🍡yurahuna
btk