Non-increasing sequence
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk fulfilled another of Marichka’s whims — he wrote down a sequence of \(n\) numbers \(a_1\), \(a_2\), ..., \(a_n\). Just as Marichka wanted, the numbers in this sequence form a non-decreasing sequence, i.e., each next element is greater than or equal to the previous one. Happy Zenyk ran to his Marichka to present her with this sequence.
Suddenly, Zenyk broke into a cold sweat. Everything stopped inside his soul. He remembered that Marichka wanted to get a non-increasing sequence, not a non-decreasing one. That is, in his sequence, each next element must be less than or equal to the previous one. Now Zenyk wants to fix his mistake as soon as possible. In one minute, Zenyk can choose any number from the current sequence and replace its value with any other. What is the minimum amount of time in which Zenyk can fix his mistake?
Input
The first line contains a single integer \(n\) — the number of elements in the sequence.
The second line contains the non-decreasing sequence \(a_1\), \(a_2\), ..., \(a_n\).
Output
In a single line, print one integer — the minimum number of minutes in which Zenyk can change the sequence to a non-increasing one.
Constraints
\(1 \le n \le 10^5\),
\(0 \le a_i \le 10^9\),
\(a_{i+1} \ge a_i\),
for 40% of the tests, an additional constraint holds: \(n, a_i \le 10^3\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 1 4 4 7 | 2 |
Notes
In the example, you can replace two elements: the first with 7, the last with 2. This forms the sequence [7, 4, 4, 2], which is non-increasing.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Non-increasing sequence - Editorial
It never pays to change a number more than once, since we can change it to its final value in one operation. So the minimum number of changes is \(n\) minus the maximum number of elements that stay unchanged. It is easy to see that Zenyk cannot keep both numbers \(a\) and \(b\) unchanged if they are different, that is, he can keep only equal elements unchanged. So we need to find the maximum number of equal elements and subtract it from \(n\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|