#P16851. [GKS 2021 #D] Final Exam
[GKS 2021 #D] Final Exam
Problem Description
It's time for the final exam in algorithms and data structures!
Edsger prepared sets of problems. Each set consists of problems in an increasing difficulty sequence; the -th set can be described by integers and (), which denotes that this set contains problems with difficulties . Among all problems from all sets, it is guaranteed that no problems have the same difficulty.
This semester Edsger has to test students. He wants to test each student with exactly problem from of his sets. No students can get the exact same problem, so when Edsger tests a student with some problem, he cannot use this problem anymore. Through countless lectures, exercises, and projects, Edsger has gauged student number to have skill level , and wants to give that student a problem with difficulty . Unfortunately, this is not always possible, as Edsger may have not prepared a problem of this difficulty, or he may have already asked this problem to some other student earlier. Therefore, Edsger will choose for the -th student a problem of difficulty , in a way that is minimal and a question of difficulty was not already given to any of the students before the -th student. In case of ties, Edsger will always choose the easier problem. Note that the problem chosen for the -th student may affect problems chosen for all the students tested later, so you have to process students in the same order as they appear in the input.
As keeping track of all the problems can be fairly complicated, can you help Edsger and determine which problems he should give to all of his students?
Input Format
The first line of the input gives the number of test cases, . test cases follow.
Each test case begins with a line which contains integers and : the number of problem sets, and the number of students, respectively. lines follow, describing the problem sets.
Each of these lines consists of integers and denoting the easiest and the hardest problem in the -th problem set. Finally, the test case ends with a single line with integers denoting students' skill levels in the order they will be tested.
Output Format
For each test case, output one line containing Case #: , where is the test case number (starting from ) and is a difficulty of a problem that will be given to the -th student.
2
5 4
1 2
6 7
9 12
24 24
41 50
14 24 24 4
1 1
42 42
24
Case #1: 12 24 11 2
Case #2: 42
Hint
In Sample Case #, we have problem sets and students.
- For the first student, we are looking for a problem with the difficulty closest to their skill level . The problem with the minimum difference is problem with difficulty , which we can find in the third problem set, so .
- For the second student, we are looking for a problem with the difficulty closest to their skill level . Fortunately, we can find a problem of this exact difficulty in the fourth problem set, so .
- For the third student, we are once again looking for a problem with the difficulty closest to the skill level . As we already used the problem with difficulty , we cannot use this problem. The problem closest in difficulty is , as was already used as well. Therefore .
- Finally, for the fourth student, we are looking for the problem closest to his skill level . We have problems with the same difference: and . We choose the easier problem, so .
In Sample Case #, we have problem set and student. In the only problem set, there is only problem, so we have to use this problem to examine the first and only student, so .
Limits
.
Among all problem sets, no problems have the same difficulty.
The number of problems in total is greater than or equal to the number of students.
Test Set
.
.
for all .
for all .
Test Set
.
.
for all .
for all .