Roller Coaster Scheduling (Small)
Time limit5sMemory limit512 MB
Given tickets for specific seats and customers, find the minimum rides needed after freely moving tickets to smaller-numbered seats, and the minimum promotions achieving it.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Simulation, Implementation
- Solved
- No attempts yet
Problem
A new roller coaster is about to open. Its train is a single row of seats, numbered 1 through from front to back. Seats closer to the front are worth more. Customers already bought tickets for opening day. One ticket lets one specific customer take one ride in one specific seat, and a customer who bought several tickets expects one ride per ticket.
You decide how many rides run on opening day. On a single ride each seat holds at most one customer, and seats may stay empty. A customer cannot sit in two seats on the same ride, and two customers cannot sit in the same seat on the same ride.
Fewer rides cost less, so you want the smallest possible number of rides. To lower that number you may promote any number of tickets. Promoting a ticket means handing the customer a ticket for a seat closer to the front, that is, a seat with a smaller number. You also want as few promotions as possible, because customers who get promoted ask for more promotions later. Moving one ticket from seat 4 to seat 2 counts as one promotion, not two.
Given every ticket that was sold, report the smallest number of rides that honors all tickets when you promote as much as needed and schedule the rides optimally, together with the smallest number of promotions that reaches that number of rides.
Input
The first line has the number of test cases . Each test case starts with one line holding three integers: , the number of seats in the train, , the number of potential customers, and , the number of tickets sold. Customers are numbered 1 through . Then lines follow, the -th of them holding two integers , the seat assigned to the -th ticket, and , the customer who bought that ticket.
Output
For each test case, print one line in the form Case #x: y z, where x is the test case number starting from 1, y is the smallest number of rides that honors all tickets with optimal promotions and scheduling, and z is the smallest number of promotions needed to honor all tickets in y rides.
Note
In the first sample case both customers hold a ticket for seat 2. A single ride cannot honor both tickets, but promoting either ticket to seat 1 puts both customers on the same ride.
The second sample case is similar, except that both tickets are for seat 1. No seat sits in front of seat 1 and a ticket is never downgraded, so the two customers ride separately.
In the third sample case one customer holds both tickets. That customer rides twice no matter how the seats are arranged, so no promotion helps.
The fourth sample case shows that some seats and some customers may hold no ticket at all. Three tickets are sold for seat 3. Promote customer 2 to seat 2, and one ride can carry customer 1 in seat 2 and customer 3 in seat 3, while a second ride carries customer 2 in seat 2 and customer 1 in seat 3. More promotions cannot lower the ride count, because customer 1 holds two tickets and those two tickets go on different rides whatever the seats are.
In the fifth sample case, one optimal choice is to promote one of the two 3 1 tickets to seat 1.