The Secret Room
InterviewTime limit2sMemory limit1024 MB
Given a sequence, find the length of the longest subsequence where every pair satisfies a_i - a_j < j - i.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Binary search, Array
- Solved
- No attempts yet
Problem
While solving another mystery of Gravity Falls, twins Dipper and Mabel had to enter a mysterious forest, where they came across an abandoned house deep in the woods. The house was very old, and the twins found nothing remarkable in it except a hidden room in the basement. The room was locked, and an intercom hung on the door.
Dipper was determined to open the room no matter what. This time he got lucky: on one page of his journal he found the key to the code that must be entered on the intercom. The journal recorded a sequence of numbers of length , and said that the code is the longest subsequence in which every pair of numbers satisfies . Help Dipper find the length of the code.
Input
The first line contains , the number of elements in the sequence ().
The next line contains the elements of the sequence, non-negative integers ().
Output
Print a single number: the number of values in the code that lets Dipper open the door of the secret room.
Hint
In the first test, . Checking all pairs shows that the inequality holds for each of them:
- .
- .
- .
In the second test, the inequality holds for the elements with indices 1, 2, 3, and 4. Any larger selection contains a pair for which the inequality fails.