Taming the Herd
InterviewTime limit2sMemory limit512 MB
Given a partial daily log of days-since-breakout values with a breakout on day 1, find the minimum and maximum possible breakouts.
- Level
Medium4 of 10
- Topics
- Greedy, Array, Implementation, Simulation
- Solved
- No attempts yet
Problem
Early in the morning, Farmer John woke up to the sound of splintering wood. The cows were breaking out of the barn again.
Farmer John was sick of the morning breakouts and decided it was time to get tough. He nailed a counter to the barn wall that tracks the number of days since the last breakout. If a breakout happens in the morning, the counter reads that day. If the most recent breakout was days ago, the counter reads . Farmer John wrote the counter down every single day.
The end of the year has come and Farmer John is ready to do some accounting. The cows will pay, he says. But some entries of his log are missing.
Farmer John is sure that he started the log on the morning of a breakout. Over all sequences of events that agree with the log entries that remain, find the minimum and the maximum number of breakouts that may have taken place during the logged period.
Input
The first line contains one integer (), the number of days Farmer John logged the counter.
The second line contains space separated integers. The th integer is either , meaning the entry for day is missing, or a non-negative integer (at most ), meaning the counter read on day .
Output
If no sequence of events agrees with both the remaining log entries and the fact that the cows broke out on the morning of day , print the single integer . Otherwise print two space separated integers and , where is the smallest number of breakouts over all consistent sequences and is the largest.
Note
In the sample the counter reads on day 4, so a breakout had to occur on day 3. A breakout also occurred on day 1, so the only uncertainty left is day 2. The total is therefore between and .