Problem list
Log in
#usaco0261 · USACO 2016-2017 January Gold

Balanced Photo

Platinum V 2.0 s 256 MB

Farmer John is arranging his $N$ cows in a line to take a photo ($1 \leq N \leq 100,000$). The height of the $i$th cow in sequence is $h_i$, and the heights of all cows are distinct.

As with all photographs of his cows, FJ wants this one to come out looking as nice as possible. He decides that cow $i$ looks "unbalanced" if $L_i$ and $R_i$ differ by more than factor of 2, where $L_i$ and $R_i$ are the number of cows taller than $i$ on her left and right, respectively. That is, $i$ is unbalanced if the larger of $L_i$ and $R_i$ is strictly more than twice the smaller of these two numbers. FJ is hoping that not too many of his cows are unbalanced.

Please help FJ compute the total number of unbalanced cows.

Input

The first line of input contains $N$. The next $N$ lines contain $h_1 \ldots h_N$, each a nonnegative integer at most 1,000,000,000.

Output

Please output a count of the number of cows that are unbalanced.

Example 1

Input
7
34
6
23
0
5
99
2
Output
3

In this example, the cows of heights 34, 5, and 2 are unbalanced.

Problem credits: Brian Dean

Source
USACO 2016-2017 January Gold
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code