2

Codeforces Round #456 (Div. 2) Editorial

 6 months ago
source link: https://codeforces.com/blog/entry/56920
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

We, the round authors, are eternally grateful to all the brave who took part in this round. Sadly there were some issues to encounter, but we hope it only made the contest more interesting :)

912A - Tricky Alchemy

Note that the crystals of each color are bought independently.

One needs 2·x + y yellow and 3·z + y blue crystals. The answer therefore is max(0, 2·x + y - A) + max(0, 3·z + y - B).

Author — GreenGrape
Code: 33946912

912B - New Year's Eve

If k = 1, the answer is n.

Otherwise, let's take a look at the most significant bit of answer and denote it by p (with the 0-th bit being the least possible). It must be the same as the most significant bit of n. This means the answer cannot exceed 2p + 1 - 1.

Consider the numbers 2p and 2p - 1. Obviously, they both do not exceed n. At the same time, their xor is 2p + 1 - 1. Hence, the maximum answer can always be obtained if k > 1.

Author — GreenGrape
Code: 33946939

912C - Perun, Ult!

The statement almost directly states the formula for the answer — it is calculated as bc4ee17639ff1c2732a402faff7bc511f78f3273.png, where f(t) is amount of enemies we can kill at t-th second. Thus, we need to learn how to calculate f(t) and find such values of t that are potential candidates for the point the maximum is achieved at.

First, let's consider the case we have no enemies with maximum health exceeding 75515265b38bd0ce1a0e393838887f819e16dd63.png. Additionally, let 18d1eb90a26829e8a78c38f7f12b1cf3e5413fe4.png.

So, how we can calculate f(t)? Let's model the process. There are three kinds of events that affect its value:

  1. Some enemy has his health updated, it is now less than or equal to 75515265b38bd0ce1a0e393838887f819e16dd63.png, thus we can kill the enemy;
  2. Some enemy has his health updated, it is now greater than 75515265b38bd0ce1a0e393838887f819e16dd63.png, thus we can't kill the enemy;
  3. The enemy has regenerated enough health to become invincible again.

One can observe that the optimal answer is reached at the second exactly preceeding the events of the second and the third kind. Indeed, otherwise we can move on to the next second: the bounty is increased and f(t) doesn't decrease, thus providing us with a better answer.

What remains for us is to calculate the time when the aforementioned events occur to run scanline. The first two kinds correspond directly to the updates (and initial values — we can treat them as updates occuring at zeroth second). Let's calculate when the events of third kind would occur. Let the second t be the moment when one of the enemies' health became equal to h. Let r be the regeneration rate of the enemy. At the second f3956b18b4a7fe17ca64e14dd6cfc07a934ec0d6.png he will regenerate enough health to become invincible again. One also needs take care of the case when r = 0: if there are no health updates after the enemy became killable, one can kill him at any moment and earn infinitely large amount of money. Note that one should need when did the last event of the first kind happen as updates cancel the potentially planned events of the third kind.

Now, consider the case when some enemy has maximum health less than or equal to 75515265b38bd0ce1a0e393838887f819e16dd63.png. In that case, there is always an enemy to kill, and, since the bounty increases over time, the answer can be infinitely large.

Finally, if 3dc0d844b444d6645b17e6b28519e2455bf8a733.png, the bounty stays constant and we cannot obtain the infinitely large answer. Since the bounty is constant, we are to find the maximum value of f(t) and multiply it by bounty. This is a simple task and is left as an excersise :)

Time complexity — 86b93d80996e04dd64507bc187993581f1a636e8.png.

Author — rek
Code (rek): 33953146
Code (xen): 33946949
Code (GreenGrape): 33947166

912D - Fishes

Let's solve a simplified problem first. Assume we know all fishes' positions (xi, yi) (1-indexed). Denote as g(x, y) the amount of fish that is inside a scoop with lower-left angle located at (x, y). Then the expected value is equal to:

af770f7ad8c5aeef82acbb3f796b9176165cd18f.png

It's quite obvious that straightforward computation will result into O(n·m) time. However, we can invert the problem and calculate for each fish f(xi, yi) — how many scoops it is covered by. f is given by the following formula:

40df72e85411bd380cc4307ebe6f98ccd90d8720.png

Let's get back to 80772a9251eacf98d0294a2c75e36434ec966461.png and transform it into:

eb054a8dfa172ce9ce04d78b84b16e194164158d.png

In other words, in order to maximize the expected value we have to choose k best values among n·m possibilities.

From now on there are several approaches.

  • Solution I.

    Imagine we're considering f(x0, y), i.e. with a fixed coordinate x0. Note that in this case f is y-convex, i.e. it's non-decreasing until some point, and after — non-increasing. Moreover, it always reaches its maximum at 315200bd23629db9e757e5074b7c0d63c8e37008.png. Denote the points to the left of this one D, and to the right (inclusive) — I.

    The rest of the solution looks as follows: initially we put 2·n points into the set, two per each x-coordinate, one for D and one for I. On each step we take the point with maximum value of f and replace it with its left or right neighbour (depending on which part it was from: D means that the substitute will be to the left, I — to the right).

    Complexity: 18fdd9d208b710e71a0489b2b0a2338fb730f77c.png.

  • Solution II.

    Let's enhance the contemplations from the previous paragraph. Notice that f is convex by both values. Then we can take bca66af06995d23c09af3afc013a64133d54dfb2.png) as the starting point and launch a breadth-first search with a priority queue, erasing the most significant point according to f from the queue and checking all of its yet unvisited neighbours in a 4-connected area.

    Complexity: 2303d62ad9668cd4d497388695339d282a49a847.png with a greater constant compared with solution one.

Author — GreenGrape
Code (xen): 33946962
Code (GreenGrape, solution 1): 33946974
Code (GreenGrape, solution 2): 33946993

912E - Prime Gift

The initial idea that emerges in such kind of problems is to use binary search to determine the k-th element. Still we have to somehow answer the following query: "how many elements no greater than x satisfy the conditions?"

It's easy to see that all such numbers can be represented as p1a1·...·pnan. Denote D(G) as a set of numbers not exceeding 1018 such that all their prime divisors lie inside G.

Let N be our initial set. Sadly we cannot just generate D(N) since its size might be of the order of 8·108. Actually, the goal is to invent a way to obtain information about all elements without having to generate them explicitly.

Imagine we split N into two such sets A and B that 8be6ac5be91d49b525da4d2d571b59ce7c9415e3.png and d5a965a11aed806522aa57092e80df0a821ff706.png. According to the fact that each element of D(N) can be represented as product of some element of D(A) and some element of D(B), we can explicitly generate D(A) and D(B), then sort them and find out how many pairwise products do not exceed x in O(|D(A)| + |D(B)|) using two pointers approach. This gives 4a2dd1bb368322053a1ddfe8c4656a40e42ad837.png in total.

The main part is to find the optimal partition. The first thought is to send the first a64c1b2df604f8cfa9acf6716a3cc1424488361e.png elements to A and the rest to B. However, this is not enough; in this case the approximate size of D(A) might reach 7·106 which is way too much to pass. To speed it up we can, for example, comprise A of elements with even indexes (i.e p0, p2, ...) so that sizes of both D(A) and D(B) do not exceed 106 and the solution runs swiftly.

Author — GreenGrape
Code (xen): 33947002
Code (GreenGrape): 33946911


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK