|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|3 초||512 MB||6||5||4||80.000%|
There is an esoteric sorting algorithm called StalinSort. It goes as follows.
Go through the elements of the given array from left to right, starting from the second. If the current element is not less than previous, then do nothing, otherwise erase it. In the end you get a sorted array.
One can implement StalinSort in O(n) time, which is pretty cool. There is a catch though: you can lose many elements in the process. To fix this I’ve come up with an improved nondeterministic version of this algorithm.
Go through the elements of the given array from left to right, starting from the second. If the current element is not less than previous, then do nothing, otherwise you have options. You can either erase it, or erase the previous element, but only if the current prefix still becomes non-decreasing. In the end you get a sorted array.
Depending on its choices, this algorithm can erase different number of elements. I wonder, what is the minimum number of erased elements I can get applying this improved version of StalinSort to the given permutation?
The first line contains one positive integer n (1 ≤ n ≤ 5 · 105) — the size of the permutation.
The second line contains n integers pi (1 ≤ pi ≤ n) — the permutation itself. It is guaranteed that all pi are distinct.
Print one number — the minimum number of elements improved StalinSort can erase.
6 6 1 2 3 4 5
6 5 6 1 2 3 4
6 6 4 5 1 2 3