Balanced Photo
Time limit2sMemory limit512 MB
Count cows whose taller neighbors on the left and right differ by more than a factor of two.
- Level
Medium7 of 10
- Topics
- Binary search, Sorting, Bit manipulation
- Solved
- No attempts yet
Problem
Farmer John is arranging his cows in a line to take a photo (). The height of the -th cow in the line is , 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. Let and be the number of cows taller than cow on her left and on her right, respectively. Cow looks "unbalanced" if and differ by more than a factor of 2. That is, cow is unbalanced if the larger of and is strictly more than twice the smaller of the two. FJ hopes that not too many of his cows are unbalanced.
Compute the total number of unbalanced cows.
Input
The first line contains . Each of the next lines contains one of , in order. Each value is a nonnegative integer at most .
Output
Print the number of unbalanced cows.
Hint
In the sample, the cows of heights 34, 5, and 2 are unbalanced.