In this problem you should guess that exists only three valid groups of three

1) 1, 2, 4

2) 1, 2, 6

3) 1, 3, 6

(You can see that integers 5 and 7 are bad).

So, we will greedy take these groups of three. If some integers will be not used, the answer is -1. In other case, print found answer.

The problem is solved by greedy algorithm. We will pass the note only in correct direction. Also, if we can pass the note at the current moment of time, we do it. In other case, we will hold it and don't give it to neighbors (we can make this action at any moment of time). Obviously this algorithm is correct. You should only implement it carefully.

In the problem you should carefully get formula. The optimal solution put marbles by two in a row. And then put one marble upon others if it possible. The most difficulties were to deal with this last phase.

In comments to the post were given formulas how to put the last marble (exactly in the middle). And there was a good beautiful illustration, which describes the situation.

In the problem you can count number of correct puzzles or substract number of incorrect puzzles from number of all puzzles. In any case you should count DP, where the state is (*j*, *mask*) — *j* — number of the last full column, *mask* — mask of the last column. This problem is equivalent to the well known problem about domino tiling or the problem about parquet.

To get the solution of the whole problem I did the following. I try to attach one domino to each of 4 directions, then paint all three cells in black and count the number of correct puzzles. But in this case you will count some solutions several number of times. So you need to use inclusion exclusion formula for these 4 directions.

The problem can be solved in different ways. The most easy idea is sqrt-optimization. Split all queries into *sqrt*(*m*) blocks. Each block we will process separately. Before processing each block, we should calculate minimum distances from every node to the closest red node using bfs. To answer the query we should update this value by shortest distances to red nodes in current block.

The solution becomes simple. Every *sqrt*(*m*) queries we make simple bfs and for every node *v* WE calculate value *d*[*v*] — the shortest distance to some red node from node *v*. Then to answer the query of type 2 you should calculate *min*(*d*[*v*], *dist*(*v*, *u*)), where *u* — every red node, which becomes red in current block of length *sqrt*(*m*).

Distance between two nodes *dist*(*u*, *v*) can be got using preprocessing for lca.

Can someone please explain problem D in more detail, I am not able to understand anything from what is posted.

Thanks

Solution is simple. let me explain it with a examples:

suppose we have filled all the empty blocks from column 0....i-1 and on ith column we have filled some of the empty blocks which is stored using mask 0,1,...,7

suppose if mask is currently 0 and i=2, so configuration could be something like this.

blocks with A are already filled.

now we have two options, we might use some vertical block or not:

now we are almost ready with solution and need to check while putting block if that box was empty earlier and which we put block near 0, we need to note this also.

I think, you should be able to understand my code now.

Could you please post the link to your solution.

I got the algorithm but I am lacking clarity over some points like considering the state (i,3), How does one keep track of whether a single vertical block was placed or 2 horizontal blocks covering the squares were placed.

Anyways, thanks for your explanation.

solution: http://codeforces.com/contest/342/submission/4424666

it does not matter whether it was vertical block or 2 horizontal block. (i,3) tells that all the blocks in first i-1 columns are filled and in this column there is only last row left to be filled.

.d.

Assume that you have the distances from vertex with index

1to every vertexuin a vectord[].Define

lca(x, y)the lowest common ancestor for vertexxandy. You must find out the distance fromvtou.The result is

d[u] + d[v] — 2 * lca(u, v)because you add twice the distance from vertex1tolca(u, v)(that's where the 2 roads intersects). You can draw a tree on a paper and and work with some examples, it will be clearer.Could you please tell me the complexity of the Problem E?

Here is my solution: http://codeforces.com/contest/342/submission/4509629 The complexity seems to be O((n+m)*sqrt(m)). But it still gets TLE.

In E problem , there isn't any boundary case like chain. Then O(N*N) solution can get AC.

Any online solutions for problem E ? Or different solutions to E?

My solution uses Heavy-light decomposition and segment trees. Complexity is O(N log^2 N). http://codeforces.com/contest/342/submission/6999318