Editorial for CROC 2016 Elimination Round

Правка en2, от ksun48, 2016-03-18 23:47:40

645A - Amity Assessment

Idea: yummy

One solution is to just brute force and use DFS to try all the possibilities. Alternatively, note that two puzzles can be changed to each other if and only if the A, B, and C have the same orientation—clockwise or counterclockwise—in the puzzle. A third option, since the number of possibilities is so small, is to simply classify all of the 4! = 24 configurations by hand.

Code

645B - Mischievous Mess Makers

Idea: ksun48

Loosely speaking, we’re trying to reverse the array as much as possible.

Intuitively, the optimal solution seems to be to switch the first and last cow, then the second and second-to-last cow, and so on for k minutes, unless the sequence is reversed already, in which case we are done. But how can we show that these moves give the optimal messiness?

It is clear when k ≥ n - 1 that we can reverse the array with this method.

However, when k < n - 1, there are going to be cows that we must not have not moved a single time. Since in each move we swap at most 2 cows, there must be at least n - 2·k cows that we have not touched, with pi = i. Two untouched cows i and j must have pi < pj, so there must be at least pairs of cows which are ordered correctly.

In fact, if we follow the above process, we get that pi = (n + 1) - i for the first k and last k cows, while pi = i for the middle n - 2·k cows. From this we can see that the both i < j and pi < pj happen only when i and j are in the middle n - 2·k cows. Therefore we know our algorithm is optimal.

An O(k) solution, therefore, is to count how many incorrectly ordered pairs (i, j) are created at each step and add them up. When we swap cow i and (n + 1) - i in step i, this creates 1 + 2·(n - 2i) more pairs. So the answer will be .

We can reduce this to O(1) by using our earlier observation, that every pair except those pairs are unordered, which gives total pairs (i, j). Note that this does always not fit in a 32-bit integer.

Code

645C - Enduring Exodus

Idea: GlebsHP

First, observe that the k + 1 rooms that Farmer John books should be consecutive empty rooms. Thus we can loop over all such sets of rooms with a sliding window in linear time. To check the next set of rooms, we simply advance each endpoint of our interval to the next empty room. Every time we do this, we need to compute the optimal placement of Farmer John’s room. We can maintain the position of his room with two pointers—as we slide our window of rooms to the right, the optimal position of Farmer John’s room should always move to the right or remain the same. This solution runs in O(n).

Alternatively, we can use binary search or an STL set to find the best placement for Farmer John’s room as we iterate over the intervals of rooms. The complexity of these approaches is .

Code

645D - Robot Rapping Results Report

Idea: abacadaea

The robots will become fully sorted if and only if there exists a path with n vertices in the directed graph defined by the match results. Because it is guaranteed that the results are not contradictory, this graph must be directed and acyclic—a DAG. Thus we can compute the longest path in this DAG via dynamic programming or a toposort.

We now have two cases. First, if the longest path contains n vertices, then it must uniquely define the ordering of the robots. This means the answer is the time at which the last edge was added to this path. Otherwise, if the longest path has fewer than n vertices, then multiple orderings satisfy the results and you should print  - 1. Note that this algorithm runs in O(n + m).

Another solution to this problem binary searches on the answer. For some k, consider only those edges that were added before time k. We can determine if the robots could be totally ordered at time k by running a toposort and checking if the longest path covers all n vertices. This might be more intuitive for some, but has a complexity of .

Code

645E - Intellectual Inquiry

Idea: yummy

For simplicity, let’s represent the letters by 1, 2, ..., k instead of actual characters. Let a[i] denote the number of distinct subsequences of the string that end in the letter i. Appending the letter j to a string only changes the value of a[j]. Note that the new a[j] becomes —we can have the single letter j, or append j to any of our old subsequences.

The key observation is that no matter what character j we choose to append, a[j] will always end up the same. This suggests a greedy algorithm—always appending the character j with the smallest a[j]. But how do we know which a[j] is minimal while maintaining their values modulo 109 + 7?

The final observation is that if the last occurrence of j is after the last occurrence of j' in our string, then a[j] > a[j']. This is true because appending j to the string makes a[j] larger than all other a[i]. So instead of choosing the minimum a[i], we can always choose the letter that appeared least recently. Since the sequence of letters we append becomes periodic, our solution runs in . Of course, we can also find the least recently used letter with less efficient approaches, obtaining solutions with complexity O((L + n)k).

Code

645F - Cowslip Collections

Idea: desert97

After each query, the problem is essentially asking us to compute the sum of for each choice of k flowers. One quickly notes that it is too slow to loop over all choices of k flowers, as there could be up to choices of k species.

So how can we compute a sum over terms? Well, we will definitely need to use the properties of the gcd function. If we figure out for each integer a how many times occurs in the sum (let this be g(a)), then our answer will be equal to overall all a. In all of these sums, a ≤ 106.

It seems that if d(a) is the number of multiples of a in our sequence, then there are k-tuples which have gcd a. Yet there is something wrong with this reasoning: some of those k-tuples can have gcd 2a, or 3a, or any multiple of a. In fact, .

We will try to write as a sum of these . We’ll take an example, when a ranges from 1 to 4. The sum we wish to compute is g(1) + 2g(2) + 3g(3) + 4g(4) which can be written as

(g(1) + g(2) + g(3) + g(4)) + (g(2) + g(4)) + 2(g(3)) + 2(g(4)), 

or

In general, we want to find coefficients pi such that we can write as . Equating coefficients of g(a) on both sides, we get that . (The mathematically versed reader will note that pi = φ(i), Euler's totient function, but this is not necessary to solve the problem.)

We can thus precalculate all pi in O(max(ai, ci)) using a recursive formula: . We can also precalculate for each l ≤ 200000, so in order to output after each query we should keep track of the values of the function d(i), the number of multiples of i. When receiving ci flowers, we only need to update d for the divisors of ci and add . If we precompute the list of divisors of every integer using a sieve or sqrt checking, each update requires O(numberofdivisors).

Thus the complexity of this algorithm is or preprocessing, and O((n + q)max(divisors)).

Code

645G - Armistice Area Apportionment

Idea: yummy

Thanks to TooSimpIe and Petr for sharing a solution with me that is much more intuitive than the one I originally had in mind! We’ll be updating our editorial with that approach soon.

We begin by binary searching on the the minimum possible difference. Thus we wish to solve the decision problem "Can a difference of k be achieved?" Consider the hyperbola |PX - QX| = k. Note that our answer is affirmative if and only if a pair of outposts defines a line that does not intersect the hyperbola.

Our next step is a reduction to an equivalent decision problem on a circle through a projective transformation. We express this transformation as the composition of two simpler operations. The first is an affine map that takes the hyperbola |PX - QX| = k to the unit hyperbola. The second maps homogenous coordinates (x, y, z) to (z, y, x). Under the latter, the unit hyperbola x2 - y2 = z2 goes to the unit circle x2 + y2 = z2.

Because projective transformations preserve collinearity, a line intersecting the hyperbola is equivalent to a line intersecting the circle. Thus we want to know if any pair of the outposts' images defines a line that does not intersect the circle. We now map the image of each outpost to the minor arc on the unit circle defined by its tangents. (We disregard any points lying inside the circle.) Observe that our condition is equivalent to the existence of two intersecting arcs, neither of which contains the other. Verifying that two such arcs exist can be done with a priority queue and a radial sweep line in .

The total complexity of our solution is therefore , where P is the precision that we need.

Code

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en8 Английский ksun48 2016-03-19 01:08:57 69 Tiny change: 'TooDifficult] and [us' -> 'TooDifficuIt] and [us'
en7 Английский ksun48 2016-03-19 00:24:30 2171 Added alternate solution to G
en6 Английский ksun48 2016-03-19 00:08:30 62 Tiny change: ' $O((n+q)\max(divis' -> ' $O((n+q)\cdot \max(divis'
en5 Английский ksun48 2016-03-19 00:02:20 172
en4 Английский ksun48 2016-03-18 23:55:13 75
en3 Английский ksun48 2016-03-18 23:52:09 69 Tiny change: 'TooDifficult] and [us' -> 'TooDifficuIt] and [us' (published)
en2 Английский ksun48 2016-03-18 23:47:40 58 Tiny change: '03-18]**\nAfter ea' -> '03-18]**\n\nAfter ea' (saved to drafts)
en1 Английский ksun48 2016-03-18 23:46:22 15594 Initial revision (published)