Codeforces Round #420 (Div. 2) Editorial

Revision en2, by send_nodes, 2017-06-25 20:32:07

First, I really need to apologize for the round. There was a serious problem in D that was even covered in the sample test, that the main solution did not handle correctly. I should have been much more careful with this problem and looked for these kind of cases. Unfortunately, it was a big enough issue that caused the round to be unrated. I know this upset a lot of people, but it's tricky to find a solution to this kind of problem after the problem has happened.

I still hope the problems were good quality. If you learned something new from the round, or from this editorial, then the round was worth it. I would advise to solve the problems you couldn't solve during the contest, so you can take away something from the round.

821A — Okabe and Future Gadget Laboratory

We can simulate exactly what's described in the statement: loop over all cells not equal to 1 and check if it doesn't break the city property. To check if a cell breaks the property, just loop over an element in the same row, and an element in the same column, and see if they can add to give the cell's number. The complexity is O(n4).

821B — Okabe and Banana Trees

The critical observation to make is that the optimal rectangle should always have a lower-left vertex at the origin. This is due to the fact that the line has positive y-intercept and negative slope: any rectangle which doesn't have a vertex at the origin could easily be extended to have a vertex at the origin and even more bananas.

Then, we just need to try every x-coordinate for the upper-right corner of the box and pick the maximum y-coordinate without going over the line. We can compute the sum of any rectangle in O(1) using arithmetic series sums, so this becomes O(bm) because the x-intercept can be up to bm. You can make it faster by trying every y-coordinate; this makes the complexity O(b), but this was unnecessary to solve the problem.

Can you solve the problem with better complexity?

821C — Okabe and Boxes

It looks like Daru should only reorder the boxes when he has to (i.e. he gets a remove operation on a number which isn't at the top of the stack). The proof is simple: reordering when Daru has more boxes is always not worse than reordering when he has less boxes, because Daru can sort more boxes into the optimal arrangement. Therefore, our greedy algorithm is as follows: simulate all the steps until we need to reorder, and then we resort the stack in ascending order from top to bottom.

This has complexity O(n2 log n). However, we can speed this up if we note that whenever we reorder boxes, any box currently on the stack can be put in an optimal position and we can pretty much forget about it. So whenever we reorder, we can just clear the stack as well and continue. This gives us O(n) complexity because every element is added and removed exactly once.

821D — Okabe and City

Working on it...

821E — Okabe and El Psy Kongroo

You can get a naive DP solution by computing f(x, y), the number of ways to reach the point (x, y). It's just f(x - 1, y + 1) + f(x - 1, y) + f(x - 1, y - 1), being careful about staying above x axis and under or on any segments.

To speed it up, note that the transitions are independent of x. This is screaming matrix multiplication! We can make a 16x16 matrix, where the entry (i,j) is 1 if we can go from y value of i to y value of j in one move. You can build this quickly and then matrix exponentiate for under every segment, then carry the DP values of the end of that segment over to start the next segment. This gives complexity O(nh3 log w) where h = 16 and w = k.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en15 English send_nodes 2017-07-05 01:05:29 784
en14 English send_nodes 2017-06-26 19:26:00 906
en13 English send_nodes 2017-06-26 01:13:40 7 Tiny change: 'nition of city here was ' -> 'nition of lab here was '
en12 English send_nodes 2017-06-26 01:11:07 94
en11 English send_nodes 2017-06-26 01:01:04 175 Tiny change: ' technique, you shou' -> ' technique for speeding up DP, you shou'
en10 English send_nodes 2017-06-26 00:05:30 85
en9 English send_nodes 2017-06-25 23:04:04 0 (published)
en8 English send_nodes 2017-06-25 23:03:12 18 Tiny change: ' summary="[user:Benq,2017-06-25]'s code:">' -> ' summary="Benq's code:">'
en7 English send_nodes 2017-06-25 23:02:51 13 Tiny change: 'ions. The easiest one I fo' -> 'ions. The most elegant one I fo'
en6 English send_nodes 2017-06-25 23:02:22 6 Tiny change: ' summary="Code">\n~~' -> ' summary="O(b) Code">\n~~'
en5 English send_nodes 2017-06-25 23:00:42 185
en4 English send_nodes 2017-06-25 22:58:19 17886 (saved to drafts)
en3 English send_nodes 2017-06-25 20:32:27 0 (published)
en2 English send_nodes 2017-06-25 20:32:07 694
en1 English send_nodes 2017-06-25 20:28:20 3316 Initial revision (saved to drafts)