Speed solve contest... as usual?
A: Minutes Before the New Year (00:00)
Time to new year .
B: Candies Division (00:02)
We always have to give some people candies, and we can give at most peole candies, so the answer is .
C: Friends and Gifts (00:09)
As we know that the given information is consistent, the information consists of closed loops, unclosed loops and individual nodes. We can compute the list of heads and tails of unclosed loops and the list of individual nodes, then attempt to connect all individual nodes into one of the unclosed loops, then close all the loops. If there are no unclosed loops, we can build a loop from the list of individual nodes.
D: Christmas Trees (00:25, +1)
We should maintain a set of used locations, and a queue of pairs of location and distance from closest tree that can be used. For each Christmas tree, try to push the two locations adjacent to it into the queue, checking whether it is already pushed or not. Then, repeat times the following process:
Pop a pair of location and distance from the queue. Attempt to push the two locations adjacent to the location of the pair into the queue, checking whether they are already used or not. Add the distance to a counter.
Finally, output the total distance and the list of locations popped from the queue.
During the contest, I used
priority_queueat the beginning, but it is not required as the distance pushed into the queue is monotonic. All possible location pushed at the beginning has distance and only distance can be pushed after popping a possible location with distance .
E: New Year Parties (00:40)
We should perform two separate greedy for minimum and maximum number of occupied houses. Before that, we should sort the array first.
For minimum number of occupied houses, we know that houses can be combined into one if . We can compute the list of used coordinates and greedily calculate the number of houses required, scanning from the left.
For maximum number of occupied houses, note that for each house we can move it to any integer in , meaning that we have a list of segments. We can try to greedily put the house on the leftmost position in if possible, and ignore the segment if it is not possible. We only need to maintain the last position we put a house at while scanning the array.
F: DIY Garland (01:03)
This is a construction task. We should note the observation that:
- Wire connecting to a subtree with a larger maximum node ID must have larger importance than wire connecting to a subtree with a smaller maximum node ID, as
Then we can try to emulate the process by hand, using the given example. We notice that appeared two times, so that leads us to think that numbers between the same node describes a subtree. At the end, a constructive algorithm as below is derived:
- is the root of the tree.
- For each , if is not yet in the tree, add the edge .
- Otherwise, add the edge where is the largest node ID not already in the tree. Note that may appear later in the array .
While performing step 3, check that the list of unused node IDs isn't empty. (Not sure if required or not.)
At the end, perform a DFS to check that all nodes are connected into the tree. If it is, then we have successfully constructed the required solution. We can output the solution constructed.