Codeforces Round #337 (Div.2) Editorial

Revision en14, by fcspartakm, 2015-12-28 13:28:56

If the given n is odd the answer is 0, because the perimeter of any rectangle is always even number.

If n is even the number of rectangles which can be construct equals to n / 4. If n is divisible by 4 we will count the square, which are deprecated, because we need to subtract 1 from the answer.

Asymptotic behavior — O(1).

At first let's find the minimum in the given array and store it in the variable minimum. It is easy to understand, that we always can paint n * minimum squares. So we need to find such a minimum in the array before which staying the most number of elements, which more than the minimum. In the other words we need to find 2 minimums in the array which are farthest from each other (do not forget about cyclical of the array). If there is only one minumum we need to start paint from the color which stay in the array exactly after the minimum (do not forget about cyclical of the array too). It can be done with help of iterations from the left to the right. We need to store the position of the nearest minimum in the variable and update her and the answer when we meet the element which equals to minimum.

Asymptotic behavior — O(n), where n — the number of different colors.

Let's build the answer recursively. For k = 0 the answer is  - 1 or  + 1. Let we want to build the answer for some k > 0. At first let's build the answer for k - 1. As the answer for k let's take four copies of answer for k - 1 with inverting of values in last one. So we have some fractal with 2 × 2 base: 00, 01. Let's prove the correctness of such building by induction. Consider two vector from the top (down) half: they have zero scalar product in the left half and in the right, so the total scalar product is also equals to zero. Consider a vector from the top half and from the down: their scalar products in the left and the right halfs differs only in sign, so the total scalar product is also zero.

Note the answer is also is a matrix with element i, j equals to \texttt{+} if the number of one bits in number i|j is even.

Complexity O((2k)2).

At first let's unite all segments which are in same verticals or horizontals. Now the answer to the problem is the sum of lengths of all segments subtract the number of intersections. Let's count the number of intersections. For this let's use the horizontal scan-line from the top to the bottom (is can be done with help of events — vertical segment is open, vertical segment is close and hadle horizontal segment) and in some data structure store the set of x-coordinates of the open segments. For example we can use Fenwick tree with precompression of the coordinates. Now for current horizontal segment we need to take the number of the opened vertical segmetns with value x in the range x1, x2, where x — vertical where the vertical segment are locating and x1, x2 — the x-coordinates of the current horizontal segment.

Asymptotic behavior: O(nlogn).

Consider slow solution: for operations of the first type reassign all letters, for operations of the second type let's iterate over the symbols in s from left to right and maintain the pointer to the current position in alphabet permutation. Let's move the pointer cyclically in permutation until finding the current symbol from s. And move it one more time after that. Easy to see that the answer is one plus the number of cyclic movements. Actually the answer is also the number of pairs of adjacent symbols in s that the first one is not righter than the second one in permutation. So the answer depends only on values of cntij —- the number of adjacent symbols i and j.

To make solution faster let's maintain the segment tree with matrix cnt in each node. Also we need to store in vertex the symbol in the left end of segment and in the right end. To merge two vertices in the segment tree we should simply add the values in the left and in the right sons in the tree, and update the value for the right end of the left segment and the left end of the right segment.

Complexity: O(nk2 + mk2logn).

#### History

Revisions

Rev. Lang. By When Δ Comment
ru7 fcspartakm 2015-12-28 13:29:08 2 Мелкая правка: 'k^2 + m k^2 logn)$.' -> 'k^2 + m k^{2} logn)$.'
en14 fcspartakm 2015-12-28 13:28:56 2 Tiny change: 'k^2 + m k^2 logn)$.' -> 'k^2 + m k^{2} logn)$.'
en13 fcspartakm 2015-12-27 16:24:40 1123
en12 fcspartakm 2015-12-27 16:22:15 0 (published)
ru6 fcspartakm 2015-12-27 16:15:37 10 Мелкая правка: 'оличество битов в ч' -> 'оличество единичных битов в ч'
en11 fcspartakm 2015-12-27 16:15:13 872
en10 fcspartakm 2015-12-27 16:14:22 2 Tiny change: 'nSoon...\n[610D &m' -> 'nSoon...\n\n[610D &m'
en9 fcspartakm 2015-12-27 16:13:58 925
ru5 fcspartakm 2015-12-27 16:04:36 3429
en8 fcspartakm 2015-12-27 15:55:53 1 Tiny change: 'nimum$squres. So we' -> 'nimum$ squares. So we'
en7 fcspartakm 2015-12-27 15:55:19 2 Tiny change: 's to $n / 2$. If $n$ ' -> 's to $n / 4$. If $n$ '
en6 fcspartakm 2015-12-27 15:55:02 1379
en5 fcspartakm 2015-12-27 15:46:30 1
en4 fcspartakm 2015-12-27 15:46:19 541
ru4 fcspartakm 2015-12-27 15:46:04 541
en3 fcspartakm 2015-12-27 15:44:34 568 Reverted to en1
en2 fcspartakm 2015-12-27 15:40:22 568
ru3 fcspartakm 2015-12-27 15:35:50 644
en1 fcspartakm 2015-12-27 15:31:45 1661 Initial revision for English translation
ru2 fcspartakm 2015-12-27 15:28:28 6
ru1 fcspartakm 2015-12-27 15:27:04 1649 Первая редакция (сохранено в черновиках)