아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Interval

시간 제한3초메모리 제한1024 MB

요약
n개의 닫힌 구간에서 공통점을 가지는 m개를 골라 선택한 구간 길이의 최댓값과 최솟값의 차이를 최소로 만들고, 불가능하면 -1을 출력한다.
난이도

보통10점 중 7점

유형
정렬, 슬라이딩 윈도우, 구간, 그리디
정답자
아직 제출이 없습니다

문제

There are nn closed intervals \[l_1,r_1],\[l_2,r_2],…,\[l_n,r_n]\[l\_1,r\_1], \[l\_2,r\_2], \ldots, \[l\_n,r\_n]. You need to choose mm intervals such that the mm intervals have nonempty intersection. In other words, there exists some xx such that for any selected interval \[l_i,r_i]\[l\_i,r\_i], l_i≤x≤r_il\_i \leq x \leq r\_i.

The cost of selecting a collection of mm intervals with nonempty intersection is the maximum length of the mm intervals minus the minimum length of the mm intervals. The length of interval \[l_i,r_i]\[l\_i, r\_i] is r_i−l_ir\_i - l\_i.

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

입력

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

In the following nn lines, each line consists of two integers l_i,r_il\_i,r\_i separated by a space denoting the left and right endpoints of an interval.

출력

Output a line with an integer denoting the minimum cost.

제한

Test casen=n =m=m=l_i,r_il\_i,r\_i
12020990≤l_i≤r_i≤1000 \leq l\_i \leq r\_i \leq 100
21010
3199199330≤l_i≤r_i≤100,0000 \leq l\_i \leq r\_i \leq 100\\,000
4200200
51,0001\\,00022
62,0002\\,000
719919960600≤l_i≤r_i≤5,0000 \leq l\_i \leq r\_i \leq 5\\,000
82002005050
90≤l_i≤r_i≤1090 \leq l\_i \leq r\_i \leq 10^9
101,9991\\,9995005000≤l_i≤r_i≤5,0000 \leq l\_i \leq r\_i \leq 5\\,000
112,0002\\,000400400
125005000≤l_i≤r_i≤1090 \leq l\_i \leq r\_i \leq 10^9
1330,00030\\,0002,0002\\,0000≤l_i≤r_i≤100,0000 \leq l\_i \leq r\_i \leq 100\\,000
1440,00040\\,0001,0001\\,000
1550,00050\\,00015,00015\\,000
16100,000100\\,0002000020000
17200,000200\\,0000≤l_i≤r_i≤1090 \leq l\_i \leq r\_i \leq 10^9
18300,000300\\,00050,00050\\,000
19400,000400\\,00090,00090\\,000
20500,000500\\,000200,000200\\,000

예제1

  1. 예제 1

    입력
    6 3
    3 5
    1 2
    3 4
    2 2
    1 5
    1 4
    
    예상 출력
    2