Under construction

Still being built. Things may change

Scheduling a Meeting

Time limit40sMemory limit1024 MB

Summary
Find the fewest scheduled meetings to cancel so that K of N Tech Leads are free for one X-hour slot within [0, D].
Level

Medium6 of 10

Topics
Intervals, Sorting, Implementation
Solved
No attempts yet

Problem

Scheduling meetings at Google is not an easy task. Even with Google Calendar, Ada has a hard time with it.

Ada is a Software Engineer at Google. To get approval for her new project, she must meet with at least K\mathbf{K} of the N\mathbf{N} Tech Leads.

Ada can see the calendars of all N\mathbf{N} Tech Leads, including every scheduled meeting. The timeline is D\mathbf{D} consecutive hours, and all meetings lie in the range [0,D][0, \mathbf{D}] with integer endpoints. Scheduled meetings can overlap, even for the same person.

Ada needs to schedule a meeting of X\mathbf{X} hours within [0,D][0, \mathbf{D}], with integer start and end times. At least K\mathbf{K} Tech Leads must be present for the whole meeting, which means their calendars must be completely free for its entire duration.

Sometimes no such slot exists. In that case, Ada must persuade some Tech Leads to cancel their existing meetings.

Find the minimum number of scheduled meetings that must be canceled so that Ada can meet with at least K\mathbf{K} Tech Leads.

Input

The first line contains the number of test cases T\mathbf{T}. Then T\mathbf{T} test cases follow.

The first line of each test case contains four integers N\mathbf{N}, K\mathbf{K}, X\mathbf{X}, and D\mathbf{D}. N\mathbf{N} is the number of Tech Leads, K\mathbf{K} is the minimum number of Tech Leads Ada needs to meet, X\mathbf{X} is the length of the meeting to schedule, and D\mathbf{D} is the upper bound of the range [0,D][0, \mathbf{D}]. No meeting can end after D\mathbf{D}.

The second line contains an integer M\mathbf{M}, the number of scheduled meetings.

Each of the next M\mathbf{M} lines contains three integers Pi\mathbf{P_i}, Li\mathbf{L_i}, and Ri\mathbf{R_i}. This means Tech Lead Pi\mathbf{P_i} has a scheduled meeting in the open interval (Li,Ri)(\mathbf{L_i}, \mathbf{R_i}), so the endpoints are not included. All M\mathbf{M} meetings are independent, even if some share the same start and end times.

Output

For each test case, output one line in the form Case #x: y. Here xx is the test case number starting from 1, and yy is the minimum number of scheduled meetings that must be canceled so that Ada can schedule an X\mathbf{X}-hour meeting with at least K\mathbf{K} Tech Leads.

Hint

The timeline can be seen as the interval [0,D][0, \mathbf{D}], that is, D\mathbf{D} consecutive hours. D\mathbf{D} can be larger than 24.

A meeting (L,R)(L, R) starts at the beginning of hour LL and ends at the beginning of hour RR, covering the whole time between them without gaps. The endpoints are not included. Ada's new meeting may border other non-canceled meetings of a Tech Lead: it can start right when another meeting ends, end right when another meeting starts, or both. A Tech Lead cannot attend Ada's meeting if any other non-canceled meeting overlaps with it at any point.

Examples1

  1. Example 1

    Input
    3
    3 2 2 6
    5
    1 3 5
    2 1 3
    2 2 6
    3 0 1
    3 3 6
    3 3 2 6
    5
    1 3 5
    2 1 3
    2 2 6
    3 0 1
    3 3 6
    3 2 3 6
    5
    1 3 5
    2 1 3
    2 2 6
    3 0 1
    3 3 6
    
    Expected output
    Case #1: 0
    Case #2: 2
    Case #3: 1