#P10208. [JOI 2024 Final] 礼物交换 / Gift Exchange
[JOI 2024 Final] 礼物交换 / Gift Exchange
Problem Description
There are students at JOI Academy, and each student has an ID number from to .
JOI Academy is planning to hold a gift exchange party soon. Each student will bring one gift to the venue, and the value of the gift brought by student is . Students do not like to receive a gift whose value is much lower than the one they brought. More specifically, student will be dissatisfied if they receive a gift with value less than . It is guaranteed that .
However, not all students will actually participate in the gift exchange party. The principal K of JOI Academy is considering possible groups of students who may participate. The -th group consists of students .
For a group consisting of at least students, if they can exchange gifts within the group such that nobody receives their own gift and nobody receives an unsatisfying gift, then the group is feasible. More precisely, a group consisting of students is feasible if and only if there exists a sequence obtained by permuting , where denotes the ID of the student who gives a gift to student , such that the following conditions are satisfied:
- For all , .
- For all , .
Principal K wants the gift exchange party to be successful, so he wants to know which of these groups are feasible.
Given the information about the students and the groups, determine for each group whether it is feasible, and write a program to output the results.
Input Format
The first line contains an integer .
The second line contains space-separated integers .
The third line contains space-separated integers .
The fourth line contains an integer .
Each of the next lines contains two integers .
Output Format
Output lines. On the -th line, output Yes if the -th group is feasible; otherwise output No.
4
3 8 5 7
2 6 1 4
3
3 4
1 3
1 4
Yes
No
Yes
Hint
Explanation of the samples
The first group consists of 2 students 3 and 4. If student 3 receives student 4's gift and student 4 receives student 3's gift, then since and , neither student will be dissatisfied. Therefore, this group is feasible, so output Yes on the first line.
The second group consists of 3 students 1, 2, and 3. Since and , student 2 will be dissatisfied no matter whether they receive the gift from student 1 or student 3. Therefore, this group is not feasible, so output No on the second line.
The third group consists of 4 students 1, 2, 3, and 4. For example, if student 1 receives student 2's gift, student 2 receives student 4's gift, student 3 receives student 1's gift, and student 4 receives student 3's gift, then nobody will be dissatisfied. Therefore, this group is feasible, so output Yes on the third line.
This sample satisfies the constraints of subtasks 1, 2, 4, 7, and 8.
Constraints
For all input data, the following hold:
- are all distinct.
The additional constraints and scores for subtasks are shown in the table below.
| Subtask | Additional Constraints | Score |
|---|---|---|
| 1 | 4 | |
| 2 | 5 | |
| 3 | $N \leq 10^5, A_{1} \geq 2 N-2, B_{1}=1, Q=1, L_{1}=1, R_{1}=N$ | 10 |
| 4 | 31 | |
| 5 | $N \leq 10^5, A_{i}<A_{i+1}, B_{i}<B_{i+1}\ (1 \leq i \leq N-1)$ | 8 |
| 6 | 12 | |
| 7 | 18 | |
| 8 | No additional constraints. | 12 |
Translated by ChatGPT 5