Hoofball
InterviewTime limit2sMemory limit512 MB
Sort cows by position, then find the minimum number of starting balls so every cow receives the ball at least once under the nearest-cow passing rule.
Problem
In preparation for the upcoming hoofball tournament, Farmer John is drilling his cows in passing the ball. The cows are numbered through , where . They all stand along a very long line on one side of the barn, and cow stands units away from the barn (). No two cows stand at the same location.
When cow receives a ball, either from Farmer John or from another cow, she passes it to the cow nearest her. If several cows are equally near, she passes it to the leftmost of them. So that every cow gets at least a little passing practice, Farmer John wants each cow to hold a ball at least once. Assuming he can hand the balls to any set of cows he likes at the start, find the minimum number of balls he must distribute.
Input
The first line contains . The second line contains space separated integers, where the th integer is .
Output
Print the minimum number of balls Farmer John must distribute at the start so that every cow holds a ball at least once.
Hint
Take the case with the cows standing at . Farmer John gives one ball to the cow at and one ball to the cow at . The cow at passes to the cow at , and from then on that ball moves back and forth between the cows at and . The cow at passes to the cow at , who passes to the cow at , and that ball also ends up moving between and . Every cow then holds a ball at least once, whether from Farmer John or from another cow.
There is no single cow such that giving her one ball makes every cow hold a ball.