I did this contest live. I am still not that great at div 2, I only solved by to D easy. Post contest I will try D hard, E and F
A
Given a list of numbers, you will have some starting sum, call it . For each operation, one of two things happens. If one is , and one is , then the sum is unchanged. If both are , then the sum increases by , and if both are , then the sum decreases by after the flip. Hence, we can think about the problem as moving the sum up and down by each time, and we want to know if we can reach .
It is important to remember that to move by $4$ or $-4$, we must have a pair of adjacent $-1$ or $1$ respectively. Thus, we keep track of how many pairs of these we have to compute whether or not we can get to $0$.
B
The problem asks us to make a list of numbers (all of which have to be ) such that the smallest subarray sum (which sums to ) is of length . The list I came up with was a list of 's, and then a single at the end. The rest of the numbers in the list is . The sum indeed is correct. Furthermore, since all the numbers are too small, the smallest subarray sum is indeed of length .
C
There are four cases for each digit: either we start with a 0, and go to either 0 or 1, or we start with a 1 and go to either 0 or 1. The interesting cases are 0 to 1 and 1 to 0.
For 1 to 0, as long as you have an odd number of such flips to complete, you can just do it in one move. If there is an even number of such flips to complete, you can complete it in two moves with one move flipping digits and another move flipping .
For 0 to 1, you can piggyback as many as you want as long as there is a 1 to 0 flip happening. Suppose there exist no 1 to 0 flips. There is still a way to kill arbitrary amounts of 0 to 1s. This can happen as long as you have at least one 0 to 0 and 1 to 1.
D Easy
The problem asks what is the smallest k such that a list is k-sortable. To be k-sortable, a list must be able to be sorted by only swapping indicies , such that .
The first observation is that must be of form . For contradiction suppose that with . We claim that works iff works. Suppose works. For swap , clearly both can do the job.
Consider swap . Specifically we know that with . Let and . (note this is possible because we assume that the list is -sortable, namely ).
First, we swap and . This is valid since and .
Then we swap and . This is valid beacuse .
Then, making swap and gives the same total effect as swapping . This move can be visualized using the diagram below.
Note that in case 1, and case 2, .
In general, we can prove that if , then and can be swapped. Specifically, let and with . Let and . Let and . We claim that the same motion can be done to swap , namely swapping and , then swapping , then swapping and . Recall that , hence .
First, we prove that we can swap (or if you swap all in proof with , including subscripts). We can observe that this value is either equal to or , both of which are .
Next, we prove that we can swap . as both values are mod .
The proof is if and only if, but i'm too lazy to prove the inverse. The key is that you would have two seperate ds, which would not cancel out nicely (as above), hence leaving a residual larger than k.
More intuiatively, the result above shows that within each block, arbitrary swaps can be conducted. However, you can swap outside of your block, but only with the same block. Hence, if each block can be sorted, then the entire list can be sorted.
To find the minimum k, binary search can be employed. This is because k-sortability is monotonic on k.
D Hard
The main idea is aleady exploited in D easy. The main challenge is to figure out how to do the queries. This problem has the perfect structure for segment tree, in particular due to the nature of the relationship between k and spans of length powers of 2.
The transition per level is as follows: If the left and right children have (value not index) ranges which are non intersecting and sorted, then the min k between each is used. Notice that if through the entire traversal the ranges are non-intersecting and sorted, it means that the entire list is sorted, hence k=0 is appropriate. If the ranges are intersecting or unsorted, then we pick k based on the level we are at. In particular, we consider the index range spanned by the left and right children, and choose k to be half of that range, namely 2k = length of range. This mirrors thte fact that any range of length 2k can be sorted with k-sortability.
E
We can determine upper and lower bounds on the value of for a tree of order . The lower bound is given by the "star" tree, where each node is connected to the root. This gives a lower bound of , as each outside node ( of them) introduces steps. The upper bound is given by the "line" tree, where each node is connected to the next. In particular the line tree has the odd nodes increasing to the center, then the even ones decreasing from the center. This gives an upper bound of .
The claim is that for every that needs to be made above (all the way up to the upper bound), there is some set of mutations on the tree which makes it achievable. We notice that any tour of the graph must be of even length, hence must be even, and the mutations work in steps of 2. The constructive is as follows: by moving some node "down" the subtree (even or odd depending on the value of the node), we can increase the value of by .
We greedily kill as much excess as possible by moving the lower nodes first, then the higher nodes down the tree.
F
We say that and are in some good the set of paths going through and (namely ) is the same .
The inverse is very easy. If , then is a good set.
Let be in some good . To show that , we need to show that . The first equivalence is by definition of , the second equivalence is by definition of a good set. It is mirrored on the other side.
We can exploit the transitivity of the good set relation to find all good sets. In particular, if are in some good set, and are in some good set, then can all be in the same good set. This is because . Suppose we partition the entire set of nodes into maximal good sets, namely . We claim the solution to the entire problem is This is because for each maximal good set, we can choose any nonempty subset of it to be a good set.
Hence, if we have some mechanism to hash the set of all paths which go through a node, we can use that to find all maximal good sets. Note that to evaluate the gigantic powers of 2 mod 998244353, we must use a precomputed array. As always it is important to remember your mod rules.