#P10220. [省选联考 2024] 迷宫守卫
[省选联考 2024] 迷宫守卫
Problem Description
Alice owns a maze. The maze can be modeled as a perfect binary tree with leaf nodes, and the total number of nodes is . The nodes are numbered in order from to . Nodes numbered are leaf nodes, and nodes numbered are non-leaf nodes. For each non-leaf node , its left child is numbered , and its right child is numbered .
Each non-leaf node has a stone statue guard. Initially, all stone statue guards are asleep. Waking up the stone statue guard at node costs mana.
Each leaf node has a rune. The rune at leaf node is denoted by . It is guaranteed that form a permutation of .
An explorer initially holds an empty sequence and starts from node , acting according to the following rules:
- When arriving at a leaf node , append the rune to the end of sequence , then return to its parent node.
- When arriving at a non-leaf node :
- If the stone statue guard at this node has been awakened, the explorer must go to the left child first, then (after returning from the left child) go to the right child, then (after returning from the right child) finally return to the parent node.
- If the stone statue guard at this node is asleep, the explorer may choose either of the following:
- Go to the left child first, then the right child, and finally return to the parent node.
- Go to the right child first, then the left child, and finally return to the parent node.
When the explorer returns to node , the exploration ends. It can be proven that the explorer will visit each leaf node exactly once, so at this time the length of is .
Explorer Bob is about to enter the maze. He wants the final sequence to be as lexicographically small as possible. In contrast, Alice wants to be as lexicographically large as possible.
Before Bob sets off, Alice may choose some stone statue guards whose total mana cost does not exceed and wake them up. When Bob sets off, he knows exactly which statues Alice has awakened. If both sides play optimally, find the final value of sequence .
For two sequences of length , we say is lexicographically smaller than if and only if the following holds:
- such that:
- ;
- 。
Input Format
This problem has multiple test cases. The first line contains a positive integer , indicating the number of test cases.
Then follow test cases. For each test case:
- The first line contains two integers , representing the maze size and the upper limit of mana Alice can use to wake stone statue guards.
- The second line contains integers , representing the mana cost to wake each stone statue guard.
- The third line contains integers , representing the runes on each leaf node.
Output Format
For each test case, output one line containing integers , representing the final value of sequence when both sides play optimally.
3
1 0
1
2 1
1 1
1
2 1
3 3
3 2 1 2 1 2 1
4 2 6 3 7 1 5 8
1 2
2 1
2 4 6 3 5 8 7 1
Hint
[Sample 1 Explanation]
- In the first test case, Alice cannot wake any stone statue guard. Bob can choose to visit leaf node first and then leaf node , obtaining .
- In the second test case, Alice can wake the stone statue guard at node . Bob can only visit leaf node first and then leaf node , obtaining .
- In the third test case, Alice’s optimal strategy is to wake the stone statue guards at nodes .
[Sample 2]
See the attached maze2.in/ans.
This test case satisfies special property A.
[Sample 3]
See the attached maze3.in/ans.
This test case satisfies special property B.
[Sample 4]
See the attached maze4.in/ans.
[Sample 5]
See the attached maze5.in/ans.
[Subtasks]
Let denote the sum of over all test cases within a single test point. For all test cases, it is guaranteed that:
- ;
- ,;
- 。
- ,;
- form a permutation of .
| Test Point ID | Special Property | ||
|---|---|---|---|
| None | |||
| A | |||
| B | |||
| None | |||
| A | |||
| B | |||
| None | |||
| A | |||
| B | |||
| None |
Special property A: ,。
Special property B: ,。
Translated by ChatGPT 5