NOI '16 P4 - Interval

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 3.0s
Memory limit: 256M

Problem type

There are n closed intervals [l1,r1],[l2,r2],,[ln,rn]. You need to choose m intervals such that the m intervals have nonempty intersection. In other words, there exists some x such that for any selected interval [li,ri], lixri.

The cost of selecting a collection of m intervals with nonempty intersection is the maximum length of the m intervals minus the minimum length of the m intervals. The length of interval [li,ri] is rili.

Compute the minimum cost of choosing m intervals with nonempty intersection. If this is impossible, output -1.

Input Specification

The first line consists of two positive integers n,m separated by a single space where n is the total number of intervals and m is the number of intervals we are going to choose. It is guaranteed that 1mn.

In the following n lines, each line consists of two integers li,ri separated by a space denoting the left and right endpoints of an interval.

Output Specification

Output a line with an integer denoting the minimum cost.

Sample Input 1

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

Sample Output 1

Copy
2

Explanation for Sample 1

When n=6, m=3, the optimal solution is to choose intervals [3,5],[3,4],[1,4]. 4 is contained in all three intervals. The longest interval is [1,4] and the shortest interval is [3,4], so the cost should be (41)(43)=2.

Attachment Package

The samples are available here.

Additional Samples

For sample 2: see ex_interval2.in for sample input and ex_interval2.ans for sample output.

For sample 3: see ex_interval3.in for sample input and ex_interval3.ans for sample output.

Constraints

Test casen=m=li,ri
12090liri100
210
319930liri100000
4200
510002
62000
7199600liri5000
820050
90liri109
1019995000liri5000
112000400
125000liri109
133000020000liri100000
14400001000
155000015000
1610000020000
172000000liri109
1830000050000
1940000090000
20500000200000

Comments

There are no comments at the moment.