1062 is a div 4 contest, so it was a bit easier. the hardest problems were about 1600
Problem A
Simply checking a=b and b=c and c=d is sufficient (transitivity of equivalence relationships)
Problem B
We need to check if a string is a permutation of another string. This can be achieved by checking that the frequency of each character is the same in both strings. Since strings only comprise of 26 characters, we can use a simple array of size 26 to count the frequency of each character in both strings and compare them.
Problem C
The key to this problem is that the only way there can be no movement is if all are even or all odd.
Suppose WLOG (with respect to the parity of the majority of elements) that all are even, and there exists some element which is odd. (in particular not all even) Suppose we want to swap arbitrary even elements at positions a, b, with the odd element at c, with a != b != c. Then we can swap a and c, then b and c, then a and c again, which is equivalent to swapping a and b
Thus, the answer is the sorted list if there are both odds and evens, otherwise don't touch the list
Problem D
We know that are coprime. The problem can be stated as Let map to a set of it's prime factors. The problem can be rephrased through set theory as
It is relatively obvious that the that we are looking for is prime. If was not prime, then it would have a prime factor such that . Then, if for some , then , and if for some , then we can replace with and get a smaller number that satisfies the condition.
This problem can be solved by recognizing the the answer has to be one of the first 16 primes. We first observe that . Suppose for contradiction that the first 16 primes all fail the condition, namely In particular, we know that , hence This is clearly a contradiction, since the product of the first 16 primes is , and thus such that does not contain one of the first 16 primes.
The threshold for the number of primes required was checked using the primordial numbers (found here).
Hence, we can check for the presence of each of the first 16 primes in the prime factorization of each number in the list, and return the smallest prime that is not present in any of the numbers. This is O(n).
Problem E
Binary search can be used to find the shortest time taken for the friend to get to a teleporter. For this to be true, it must be shown that the maximum number of teleporters placed is monotonic with respect to the time. In particular, by reducing the shortest time taken for a friend to get to a teleporter, the maximum number of teleporters that can be placed can be increased. This is intuiatively true because by reducing the time, you can place more teleporters closer to the friends, and thus more teleporters can be placed.
Hence, binary search can be used to find the shortest time for which the number of teleporters placed is at least .
It is possible in time to determine if is a possible solution. We check each gap between friends, and place teleporters at least away from the endpoints of the gap. In particular, for a gap between friends at and , we can place teleporters in the gap. We can then check if the total number of teleporters placed is at least .
One note is that it is also important to check the edge cases where the first and last friends are at the edges of the line, and we can place teleporters before the first friend and after the last friend.
The final step is to reconstruct the solution. The reconstruction follows the same logic as the check, and we can place the teleporters greedily.
Problem F
I think the best way to have an intuiative understanding of the solution to this problem is to ensure you have an intuiative understanding of LCA. In particular, the most important property to know is that, given is a set of nodes, and is a node, then
Specifically, we are asking for the elements of to be in at least two different subtrees of . Both directions are relatively easy to prove. The forward direction is true because if all elements of are in the same subtree of , then the LCA of is in that subtree, and thus cannot be . The reverse direction is true because if there exists a child of such that all elements of are in the subtree of , then the LCA of is in the subtree of , and thus cannot be .
Using a DFS, we can keep track of how many children are in each subtree of a given node The final "subtree" is the subtree containing the parent of the node (trees are reversible in this way, we can think of it as a rerooting). The number of nodes in this final "subtree" is determinable by the number of nodes in the tree minus the number of nodes in the subtrees of the node.
Consider the contribution of a single node to the final result. We can consider rerooting this tree at some node in some subtree of . By rerooting at , the subtree cannot supply nodes towards , as they would be ancestors (and not descendants) of . However, all other subtrees area still in contention. Hence, as long as the other subtrees (at least two of them) provide enough nodes (at least ), then the LCA of is still . Note that if the above works for , then it works for all nodes in . In particular, this means that node is in all , which contributes 1 to , which contributes to the final answer.
As a minor edge case, we also need to consider if the tree is rooted at . In this case, the subtree containing the parent of does not exist, and thus we need to check if there are at least two subtrees of which have at least nodes.
Hence, the final algorithm is as follows:
- Run a DFS to find the size of each subtree, including the "subtree" containing the parent
- For each node, remove each subtree, and check if within the remaining subtrees if there are at least two subtrees which combined have at least k nodes.
- If so, add the size of the removed subtree to the final answer
- Remember to also consider the edge case where the tree is rooted at the node in question
Problem G
Problem G is a typical DP problem. The first important observation is that
Hence, instead of minimimzing the cost of the items which are replaced, we can maximize the cost of the items which are kept.
The second important observation is that the kept elements must form a increasing subsequence. Hence, we can use a DP to find the maximum cost of an increasing subsequence. This number can then be subtracted from the total cost to find the minimum cost of the replaced elements.