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 a1,a2an, an increasing subsequence with non-negative gap G is a sequence of indices 1i1<i2<<ikn such that for all 1j<k we have that aij+1aijG.
Note that Lucas does not need the sequence to be strictly increasing. Specifically, he is fine if G=0 and aij+1=aij.

In particular, given a sequence of N integers ai 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 ai 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 bi that matches ai in all uncorrupted locations and has an increasing subsequence with gap G. Lucas wants you to consider bi that contain negative (and zero) integers, even though he is sure that his original uncorrupted ai contained only positive integers.

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

Constraints

2KN2×105

ai=1 or 1ai109

Subtask 1 [20%]

K=N

Subtask 2 [30%]

N2000

Subtask 3 [50%]

No additional constraints.

Input Specification

The first line contains two integers: N and K.

The second line contains n integers: ai for 1in.

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

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

Sample Output 1

Copy
3

Sample Input 2

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

Sample Output 2

Copy
35

Sample Input 3

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

Sample Output 3

Copy
Infinity

Sample Input 4

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

Sample Output 4

Copy
0

Sample Input 5

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

Sample Output 5

Copy
-1

Comments

There are no comments at the moment.