Job Scheduling
Time limit4sMemory limit1024 MB
Given N base jobs and Q queries that each add R jobs, find the maximum number of non-overlapping jobs after each query's additions.
- Level
Hard8 of 10
- Topics
- Intervals, Dynamic programming, Binary search, Segment tree
- Solved
- No attempts yet
Problem
There are jobs. Each job is given by two integers (), meaning the job starts at time and ends at time . If the time intervals of two jobs overlap, the factory cannot perform both. (If the end time of one job equals the start time of another, the two jobs do not overlap.)
The factory wants to process as many jobs as possible. Because it can handle only one job at a time, the number of jobs it can process is limited. Instead of expanding the factory, the factory adds more jobs to the schedule.
There are queries. In the -th query, additional jobs are given. For each query, output the maximum number of jobs that can be processed among the jobs.
For example, let and , and let the base jobs be . The two queries are and . For the first query, the optimal choice is the 4 jobs . For the second query, the optimal choice is the 3 jobs .
Input
The first line contains a natural number , the number of test cases (). The test cases follow.
The first line of each test case contains the number of jobs (). Each of the next lines contains the start time and end time of a job ().
The next line contains the number of queries (). Each query starts with a line containing the number of new jobs (, ). Each of the next lines contains the start time and end time of a new job ().
The sum of over all test cases is at most . The sum of over all test cases is at most .
Output
For each test case, print a line in the form Case #, where is the test case number. Then print lines, each containing the answer to one query.