CC (CodeChef) STARTERS 48 Solutions
The content below was generated entirely by machine translation. Please verify its accuracy. If anything is unclear, consult the Chinese source version.
Accurate XOR
Approach
This problem uses a property of XOR. When XORing multiple consecutive 0s or 1s, only an odd number of 1s makes the result 1.
If there are an even number of 1s, every 1 can always be paired with another 1 so that their XOR becomes 0. The occurrence of 0 does not affect the final result, so if there are an even number of 1s, the final result is always 0.
The Xor-value of a node is defined as the bitwise XOR of all the binary values present in the subtree of that node.
This sentence in the statement says that the XOR value of a tree is the XOR sum of every node under that tree.
In other words, let the current tree’s root be , and let have child nodes (including indirect children, such as children in its subtrees), whose values are . Then the XOR value of is:
Because every child node has a value of either 1 or 0, the property above tells us that if the current tree’s XOR value is 1, then an odd number of nodes in all its subtrees have value 1, and vice versa.
That is, if the XOR value of tree is 1:
The problem requires subtrees to have an XOR value of 1. Therefore, for every child node in these subtrees, the sum of their values must be odd.
Let be the number of child nodes with value 1 in tree . Let the current tree be , and suppose we still need trees to have an XOR value of 1 (that is, some trees already have an XOR value of 1).
If and , meaning that an even number of its child nodes have value 1, we should set the value of this node to 1.
This is because , so we need more trees with XOR value 1. Since this tree has an even number of child nodes with value 1, its XOR value is not 1. Changing the value of the tree itself to 1 changes its XOR value to 1, achieving our goal.
Conversely, if , we do not need more trees with XOR value 1. However, if , meaning the sum of the values of all its child nodes is odd, we should set to 1.
This is because we do not want to produce more trees with XOR value 1. Setting to 1 makes the sum of the values of all its nodes even, and the XOR value of becomes .
With these two conclusions, we can use DFS to find the answer.
Code
1 | // tzyt |
Strict Permutation
Approach
My original idea was to sort every constraint by position, and then by value if the positions were equal.
Then I would traverse every constraint and alternately insert each constraint and each unrestricted value (according to their values, because the problem asks for the lexicographically smallest result). The explanation here is probably unclear; the following was my previous code:
1 | /*Date: 22 - 07-20 20 10 |
This reckless approach causes a problem. Suppose we sort the constraints as described above and call them .
Then the number in can only appear in the interval , which does not satisfy the problem’s requirements. This is why it received so many WAs.
The correct solution is to calculate from back to front.
We maintain a max-heap and traverse every position (the positions in the permutation) from back to front.
If the position of some constraint is the current position, we add the value of that constraint to . For every position traversed, we can then directly take the top element from and put it into the answer.
Therefore, we can take the value of a constraint from only when the current position is smaller than the constraint’s position, so every element taken from is valid.
At the same time, these elements are also the largest possible. Since we traverse from back to front, this ensures that the resulting permutation is lexicographically smallest.
Finally, we need to consider when to output -1. Since stores every element that is valid for the current position, if nothing can be taken from , then no valid permutation can be produced.
One final point: for numbers without any constraints, we can add them to at the beginning, or equivalently, their constraint position is .
Code
1 | // tzyt |






![[Stanford CS144] Lab 4 Record](/img/CS144/tcp%E7%8A%B6%E6%80%81%E6%B5%81%E8%BD%AC%E5%9B%BE.jpg)