Problem K
Entertainment Box

Wikimedia, cc-by-sa

Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fights they have finally decided to buy a video tape recorder. This fabulous, new device can record $k$ different TV shows simultaneously, and whenever a show recorded in one the machine’s $k$ slots ends, the machine is immediately ready to record another show in the same slot.

The three friends wonder how many TV shows they can record during one day. They provide you with the TV guide for today’s shows, and tell you the number of shows the machine can record simultaneously. How many shows can they record, using their recording machine? Count only shows that are recorded in their entirety.


The first line of input contains two integers $n,k$ ($1\leq k < n \leq 100\ 000$). Then follow $n$ lines, each containing two integers $x_ i,y_ i$, meaning that show $i$ starts at time $x_ i$ and finishes by time $y_ i$. This means that two shows $i$ and $j$, where $y_ i = x_ j$, can be recorded, without conflict, in the same recording slot. You may assume that $0 \leq x_{i} < y_{i} \leq 1\ 000\ 000\ 000$.


The output should contain exactly one line with a single integer: the maximum number of full shows from the TV guide that can be recorded with the tape recorder.

Sample Input 1 Sample Output 1
3 1
1 2
2 3
2 3
Sample Input 2 Sample Output 2
4 1
1 3
4 6
7 8
2 5
Sample Input 3 Sample Output 3
5 2
1 4
5 9
2 7
3 8
6 10

Please log in to submit a solution to this problem

Log in