#P16725. [GKS 2019 #A] Contention
[GKS 2019 #A] Contention
Problem Description
You are selling tickets for the front row of seats at a movie theater. The front row has seats, numbered to from left to right. You have been out of the office the last week, and upon your return, bookings for seats have piled up! The -th booking requests all the seats from to inclusive. You now have the boring job of entering each booking into the system, one at a time.
Since some of the bookings may overlap, the system might not be able to fulfill each booking entirely. When you enter a booking into the system, it will assign every seat requested by the booking that hasn't already been assigned to a booking entered into the system earlier.
What is the largest integer where there exists an order that you can enter the bookings into the system, such that each booking is assigned at least seats?
Input Format
The first line of the input gives the number of test cases, . test cases follow. Each test case starts with a line containing two integers and , the number of seats and the number of bookings, respectively. Then, there are more lines, the -th of which contains the two integers and , indicating that the -th booking would like to book all the seats from to , inclusive.
Output Format
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the largest value , as described above.
3
5 3
1 2
3 4
2 5
30 3
10 11
10 10
11 11
10 4
1 8
4 5
3 6
2 7
Case #1: 1
Case #2: 0
Case #3: 2
Hint
In Sample Case #1, there are seats and bookings. One possible order is:
- Put in the second booking, where the system will book seats ( and ).
- Put in the first booking, where the system will book seats ( and ).
- Put in the third booking, where the system will book seat (only seat , since seats , , and are already booked).
Each booking is assigned at least seat, and there is no order that assigns at least seats to each booking, so the answer is .
In Sample Case #2, there are seats and bookings. No matter what order you assign the seats in, at least one booking will have no seats assigned to it. So the answer is . Notice that there can be seats that are not part of any bookings!
In Sample Case #3, there are seats and bookings. One possible order is:
- Put in the second booking, where the system will book seats ( and ).
- Put in the third booking, where the system will book seats ( and , since and are already booked). Notice that the seats booked are not necessarily adjacent to each other.
- Put in the fourth booking, where the system will book seats ( and ).
- Put in the first booking, where the system will book seats ( and ).
Each booking is assigned at least seats, and there is no order that assigns at least seats to each booking, so the answer is .
Note: We do not recommend using interpreted/slower languages for the Large dataset of this problem.
Limits
.
.
.
Test set 1 (Visible)
.
Test set 2 (Hidden)
.
For at least of the test cases, .