Dilhan's Computing Contest 1 P4 - Increasing Sequence With Gap

View as PDF

Submit solution

Points: 17 (partial)
Time limit: 5.0s
Memory limit: 1G

Author:
Problem type

After reading an article on the Longest Increasing Subsequence problem, your friend Lucas is interested in other problems involving increasing subsequences.

Recently, he has started asking you about increasing subsequences with large 'gaps'. Given a sequence a_1, a_2 \dots a_n, an increasing subsequence with non-negative gap G is a sequence of indices 1 \leq i_1 < i_2 < \dots < i_k \leq n such that for all 1 \leq j < k we have that a_{i_{j+1}} - a_{i_j} \geq G.
Note that Lucas does not need the sequence to be strictly increasing. Specifically, he is fine if G = 0 and a_{i_{j+1}} = a_{i_j}.

In particular, given a sequence of N integers a_i he wants you to find the largest (non-negative) integer G such that there exists an increasing subsequence of a with gap G. As this is too easy if you can choose small subsequences, he wants the subsequence to have length at least K.

Unfortunately, when he wrote down his sequence a_i some of the numbers got corrupted and were changed to -1. He now wants you to find the largest non-negative value G such that you can find a sequence b_i that matches a_i in all uncorrupted locations and has an increasing subsequence with gap G. Lucas wants you to consider b_i that contain negative (and zero) integers, even though he is sure that his original uncorrupted a_i contained only positive integers.

If no valid G exist, instead output -1and if the set of valid G is unbounded, output Infinity.

Constraints

2 \leq K \leq N \leq 2 \times 10^5

a_i = -1 or 1 \leq a_i \leq 10^9

Subtask 1 [20%]

K = N

Subtask 2 [30%]

N \leq 2000

Subtask 3 [50%]

No additional constraints.

Input Specification

The first line contains two integers: N and K.

The second line contains n integers: a_i for 1 \leq i \leq n.

Output Specification

Output the maximum possible subsequence gap, or -1 if no such subsequence exists.

If the maximum possible gap is unbounded, output Infinity.

Sample Input 1

10 10
4 -1 -1 23 -1 54 -1 -1 63 -1

Sample Output 1

3

Sample Input 2

10 5
4 -1 74 23 643 54 33 5 63 -1

Sample Output 2

35

Sample Input 3

10 6
-1 5 -1 4 -1 3 -1 2 -1 1

Sample Output 3

Infinity

Sample Input 4

10 6
1 5 2 4 3 3 4 2 5 1

Sample Output 4

0

Sample Input 5

10 7
1 5 2 4 3 3 4 2 5 1

Sample Output 5

-1

Comments

There are no comments at the moment.