Mock CCC '14 S2 - Sleep Cycle

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 16M

Author:
Problem type
2014 Mock CCC by Alex and Timothy

Alice's obsession with her friend Bob is so tenacious that she cannot fall asleep at night. She decided to come up with a plan to secretly visit Bob in his house while he is sleeping. To not get caught, she must go only when she knows that Bob will be sound asleep for a while. Alice knows that people have sleep cycles. Below is an example of a hypnogram, demonstrating the human sleep cycle.

However, every individual's sleep pattern is different and can be affected by many different factors. For instance, Bob's sleep cycle can be different due to his habit of staying up late to watch movies, or due to him being a heavier sleeper than most. The only thing Alice can truly rely on is the cold, hard data she has collected from a high-quality microphone, previously planted outside of Bob's window to monitor his sleeping patterns.

Given N (3 \le N \le 1\,000) measurements of Bob's awakeness level recorded at fixed time intervals, Alice would like to know the length of the longest dip in the data. A dip is a consecutive sequence of length three or more that starts at a value, progresses to the minimum value of the sequence in a strictly decreasing pattern, remains at the minimum value for one or more iterations, and progresses back up in a strictly increasing pattern. More specifically, a consecutive subsequence from i to j of a sequence of numbers is a dip if and only if

\displaystyle x_i > x_{i+1} > \dots > x_k\ (= x_{k+1} = x_{k+2} = \dots) < \dots < x_{j-1} < x_j

For example, the sequence \{7, 5, 4, 2, 1, 2, 8, 10\} is a dip of length 8, the sequence \{3, 2, 2, 3\} is a dip of length 4, but the sequences \{1, 1, 1\}, \{5, 4, 4, 2, 3, 5\} and \{5, 2, 1, 9, 8, 10\} are not dips. Knowing the length of the longest dip, Alice will then be able to determine whether she has an opportunity to break in and watch Bob sleep.

Input Specification

Line 1 contains the positive integer N, the number of data points to follow.
Line 2 contains N positive integers each no greater than 1 billion, the measurements of Bob's awakeness levels that Alice has recorded.

Output Specification

One integer - the length of the longest dip, or 0 if the input does not contain a dip.

Sample Input

12
21 5 4 10 8 4 3 6 12 17 4 17

Sample Output

7

Explanation

The longest dip in the data is 10, 8, 4, 3, 6, 12, 17.


Comments

There are no comments at the moment.