Hello everybody!

The contest is over, and I've found that there is no post about the contest. I'm waiting for unfreeze of http://opentrains.snarknews.info/~ejudge/res/res10355 Who knows the expected time?

Let's discuss problems here.

Also it is interesting how good was the onsite in Novosibirsk. What impressions?

How to solve 11?

For each suffix compute the value (as a decimal number) mod

P. Leta_{ i}be the value corresponding to the suffix starting ati.Then, for a fixed

j, the value of the substring [i..j) only depends ona_{ i}. Now it's easy to get anO(nlogn) solution for each query.In fact you can get

O(NT) solution (without logarithm) by using hash table.Just make sure you do

O(N) insertions total instead ofO(NT), otherwise you'll get TL unless you write a hand-optimized hash table.How to solve 5-th problem?

I think 5th is MCMF problem. You can fix number of people who votes for no one, and maximum votes that other candidates get. Then build network with this variables and run MCMF. But not sure about whether it fits in time limit or not. I didn't have time to code it at contest because of 11th problem time limit in DIV2 :) I hope someone who solved it will write more detailed solution.

I solved it with MCMF.

Let

abe the exact number of votes that we want our winning candidate to receive. Construct a flow graph with source/sink, a node for each person that is voting (Nnodes), and a node for each voting option (K+ 1 nodes). We need the following edges:aand a very large negative cost.a- 1 and zero cost.It is clear that the minimum-cost flow here corresponds to the minimum cost required to have our candidate win with exactly

avotes (the large negative cost from the source to our candidate forces any minimum-cost max flow to vote for our candidateatimes). We can increment the cost of this flow bya·(negativecost) to get the exact cost, and look at the residual graph to reconstruct the solution. Finally, we can iterateafrom 1 toNto get the optimal assignment.One thing worth noting is that the function "cost to win with exactly

avotes" (depending ona) isconvex. This can be proved by taking optimal flows fora=xand fora=z, then observing that their convex combination with appropriate coefficients would be a valid flow in the graph fora=y(wherex<y<z).So you could optimize your solution by doing something like a ternary search over

a.How to solve 10? It seems brute force is not too slow and only slight improvement is enough.

1) Brute first

Kmoves.2) After that you have state (

X,Y,DestructedWalls). For optimizations purposeDestructedWallscan be 64 bit mask.3) Clean up bits in

DestructedWallsmask, if you can't visit some walls anymore with (n-K) moves (to reduce different masks count).4) After that there are not so many different states. For each just brute remain (

n-K) moves.K~= 23 - 24 works for us in upsolving.In fact, there is no need to do any portion of brute force here.

You can run DP with states: (turns passed, robot position, 64-bit mask of destroyed walls), always storing the states only for a single current value of "turns passed". In each state store also its multiplicity (i.e. how many ways are there to get it). After modelling each turn, merge all equal states.

Now you have to add the "finish optimization". For each number of passed turns and robot position, precompute the set of cells you can theoretically move into regardless of whether any brick wall is destroyed or not. Then in the main DP you do bitwise AND between the mask of destroyed walls and mask of potentially visitable cells in future (i.e. you are forgetting about destroyed walls you can never bump into).

With such a solution, there is no need to tweak a special parameter

K, and no need to write recursive search.How to solve C and D?

Also, I was wondering if there is any clever solution for F. I solved it using Hopcroft's DFA minimization algorithm.

For F: take any remainder x modulo M. Now consider the chain of segments: [x, x], [b*x, b*(x+1)-1], [b*b*x, b*b*(x+1)-1], ... Find the first segment in this chain that contains 0 modulo M. The pair (k, (x*b**k) mod M) now uniquely defines state x, where k is the number of the segment (in other words, the size of the segment and its left boundary).

C: we will only use "is intersection empty" questions. First, find bounding box using binary searches by X and Y. At least one of its corners is a vertex of the triangle, we can check by truncating a small 0.5*0.5 triangle from the corner. Now we've found a vertex and a 90 degree angle containing everything. Now use two binary searches by angle to find rays containing two sides from that vertex. Finally, use binary searches with half-planes parallel to one side to find the vertex on the other side.

D: I find it really hard to explain, but the solution itself is really easy. First, the problem easily boils down to: you have N left ends, then N right ends of segments, connected somehow in pairs. Let's call two segments connected if they intersect, but not one lies inside another. Now we need to find connected components, check if each component is bipartite, and color with two colors if it is, all faster than N**2.

It turns out that because all left ends come before all right ends, the structure is really simple. Let's look at the left end corresponding to the first right end. All left ends after it intersect it, and thus must have the opposite color. Thus, their right ends must go in reverse order (to avoid intersections between them). Now let's consider the rightmost right end of those. All remaining uncolored right ends to the left of it, again, must have the first color, and so on.

Code for the coloring part:

In fact, we did not find this a simple solution. We used a more technical approach.

Renumber wires on the left to be [1..N] (and wires on the right accordingly). Then the indices of wires on the right form a permutation. It is easy to notice that we have to break this permutation into two increasing subsequences (one for inner wires, one for outer wires). For each element, we know the cost of including it into each subsequence.

This problem can be trivially solved by DP with

O(N^{2}) states. In fact, you can use onlyO(N) states looking like (size of prefix processed, which subsequence contains last processed element). To achieve this, you have to add whole increasing blocks to alternating subsequences, one block on each step of DP.This DP takes

O(N) memory, butO(N^{2}) time. It can be optimized with RMQ structure toO(NlogN) time in a way similar to how classical "maximal increasing subsequence" is optimized with RMQ. In fact, it is also possible to optimize it to pureO(N) without any data structure by simply saving some minimums during long increasing runs. Of course, the details are rather nasty =)Right, the two increasing subsequences paradigm also allows to explain my solution a bit easier: let's look at 1. All numbers to the left of it must go to the other subsequence, and thus be in increasing order. Let the last of them (right before 1) be X. Now all remaining numbers less than X must go to the same sequence as 1, and be in increasing order, and so on.

How one can solve 8?

Yes, I would like to know about solving this problem too. I got TL 16 because of usual tree structure (bad idea when tree looks like a line — so queries would be executed

O(N)), but what is right solution?What we need a data structure that allows to update numbers in a tree and to find the sum of numbers in a subtree. If we do a preorder traversal of the tree, then any subtree will correspond to a segment in that ordering. And a data structure that allows updating numbers and finding segment sums is a Fenwick tree.

Thank you for your answer, I thought about segment tree, but could not understand how to enumerate vertices (build segment tree)... It is not difficult for me when we build segment tree according to the array 1..N, but when we have tree with randomly enumeration in that problem — I don't know about representing.