Valentine's Day '19 S4 - A Greedy Spouse

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 0.6s
Java 1.0s
Memory limit: 128M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

We all know the famous interval-scheduling problem. You have been given N tasks, each of which will run between time l_i and r_i, inclusive, that you must tell your computer to run. You want to schedule the maximum number of tasks, under the condition that only one task can be run at any given point in time.

Your beloved spouse has gone ahead and assigned the tasks greedily. In particular, they will sort the tasks in order of increasing l_i, and then increasing r_i. Then, they will loop through tasks, and schedule the computer to run the current task if the previous assigned task does not overlap. If it does overlap, the current task is discarded and the next task will be tried.

You come along, and notice that your spouse has made a horrible mistake. Greedily assigning the tasks is not always optimal in trying to maximize the tasks ran!

Wanting to fix their mistake, but not having a lot of time to reprogram the computer, you want to remove at most (including) K tasks that your spouse assigned the computer, and fill in the empty slot with a subset of the discarded tasks.

You want to maximize the number of tasks to be run after this change. Please output the maximum possible number of tasks to be run!

Input Specification

The first line will contain two integers, N, K\ (1 \le N \le 10^5, 1 \le K \le 2).

The next N lines will each contain two integers, l_i, r_i\ (1 \le l_i \le r_i \le 10^9).

Output Specification

Output the maximum number of tasks to be run after your change.

Subtasks

Subtask 1 [5%]

N \le 100, K = 1

Subtask 2 [25%]

K = 1

Subtask 3 [70%]

No further constraints.

Sample Input 1

6 1
1 3
4 8
9 10
5 5
6 7
8 9

Sample Output 1

4

Explanation For Sample 1

Your spouse assigned tasks 1, 2, 3. You replace task 2 with task 4 and task 5, for a total of 4 tasks run.

Sample Input 2

9 2
1 4
5 8
9 11
2 2
3 3
4 7
6 6
7 7
8 9

Sample Output 2

5

Sample Input 3

3 1
4 5
1 2
2 4

Sample Output 3

2

Comments

There are no comments at the moment.