No.284 Kadomatsu and Magic (2)
Problem Statement
Once upon a time, there were only 3 bamboo stalks in kamipeipaa's garden.
Now there is a bamboo grove consisting of $N$ bamboo stalks lined up in a row. The height of the $i$-th bamboo stalk is $A_i$.
kamipeipaa is considering the following two conditions:
・The number of bamboo stalks is $M(3 \le M)$, and, where $B_i$ is the height of the $i$-th bamboo stalk, for $1 < i < M$, the values $B_{i-1}$,$B_i$,$B_{i+1}$ are all distinct and $B_i$ is the maximum or the minimum of the three.
・There are no bamboo stalks at all.
He wants to make either of these conditions hold.
He intends to achieve this by repeatedly using magic that "chooses one bamboo stalk and makes it vanish".
When 0 or more bamboo stalks are made to vanish so that the condition is satisfied, tell kamipeipaa the maximum number of bamboo stalks that can remain.
Input
$N$ $A_1$ $A_2$ ... $A_N$
The first line contains an integer $N (1 \le N \le 10^{5})$, the number of bamboo stalks.
The second line contains integers $A_i (1 \le A_i \le 10^{6})$, the height of the $i$-th bamboo stalk, separated by spaces.
Output
Output the maximum number of bamboo stalks that can remain.
Sample
Sample 1
Input
3 5 1 4
Output
3
The condition is satisfied without using any magic.
Sample 2
Input
6 4 4 4 5 1 4
Output
4
The condition can be satisfied by using magic so that {4, 5, 1, 4} remains.
Sample 3
Input
5 1 2 3 4 5
Output
0
The condition can be satisfied by making all the bamboo stalks vanish.
Sample 4
Input
2 9 3
Output
0
The condition can be satisfied by making all the bamboo stalks vanish.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
紙ぺーぱー