Thoughts
I took an atcoder contest after a long while and it didn't go that well. I didn't really understand how E was getting so many solves by lower rated people, after having determined that the naive square root decomposition I was unsuccesfully attempting to squeeze within the TL couldn't possibly have been the cheese that they used; However, I realized post-contest that it was indeed the solution everyone used, and my code also passes with a modification to the block size :clown:
Solutions
A
Let's solve the problem for a single query spanning the entire grid. What if the grid consists of just white cells? Well, then the answer would just be
Ok, what about arbitrarily colored grids? Is the following general strategy still optimal?:
iterate over (x, y) in increasing order:
if the 2 * 2 grid with (x, y) at the top left is all-white:
color (x, y) black
One can prove that it indeed is (outline: the number of operations performed by any strategy cannot exceed the total number of
So the answer to any query is just the number of
B
This is my favorite problem from this contest.
We're given a complete undirected weighted graph where each edge has a weight of
In problems like these (where we iteratively traverse between states towards some optimal endpoint), it helps to consider two things:
- What an optimal state looks like (ie. its salient properties).
- What invariants are maintained as we traverse between states.
Let's consider the optimal state. An observation that strikes one is that for any
Anyways, this implies that no node can have more than one incident edge with a weight of
This nudges us in the direction of considering individual nodes. Notice that when we perform an operation on some cycle
C
Maybe atcoder has changed over the past few years, because problems like these were only found in ABCs back in the good old days.
Let's divide segments
- Type 1:
- Type 2:
We quickly observe two conditions whose conjunction constitutes a necessary and sufficient condition for a solution to exist:
- No segments of different types can intersect.
- For any two segments of a common type, one cannot be completely contained within the other.
Note that the second condition implies that:
- For two type
segments and , . - For two type
segments and , .
If the n&s conditions are satisfied, the following ordering is valid:
- First, we process all type
segments in decreasing order of . - Then, we process all type
segments in increasing order of .
D
This is a much better problem than D, although it's also quite standard.
It can be reduced to the following:
Given a tree, find the maximum number of pairwise disjoint pairs of nodes
My path to the solution looked like the following:
- Make some observations that would be instrumental to later proving why the solution I end up at works, but don't really advance my general intuition for the problem.
- Draw on intuition from other problems to model a feasible solution.
- Realize that the observations I made were actually useful if only I had thought about the problem in a different way.
An observation that we can make is that an optimal state is also maximal (ie. we cannot pair any nodes without having to break some existing pairs), so all the unpaired nodes lie on the path from the root to some leaf. I actually won't bother going into other details of this first phase.
Now, let's try using an idea that's fundamental to subtree-dp solutions here:
If you can compute the optimal answer for the subtree of node
from the optimal answers for the subtrees of the children of , you can compute the answer for the root by going over nodes in a bottom-up manner.
Alright, so let's say that for some node
Let's say the node
- "Inter-match" the unmatched nodes between the subtrees of
and . This will result in only at most one subtree having some unmatched nodes. - Assume that
still has some unmatched nodes. Then consider the pairs which have both nodes in the subtree of . If we have 2 unmatched nodes in the subtree of , we can simply break apart such a pair from , and match both the unmatched nodes from with one of these newly unmatched nodes from . The number of times we do this is just where is the number of unmatched nodes in and is the number of pairs completely contained within the subtree of .
It's easy to see that the above merging updates can be processed in
How to prove that these merges are optimal? proof by AC
My proof is complicated and not worth explaining, just look at the editorial.
E
My "solution" is just a naive brute-force:
- Choose a block size
and partition the array into blocks with this size. - Process elements in increasing order of
. Whenever you enter a new block, recompute an SOS-DP array that includes all elements before this block. - To find the answer for some
, you just consult one value in the SOS-DP array (corresponding to ), and also brute-force over all elements behind it in its own block.
This takes