vaaven's blog

By vaaven, history, 6 months ago, In English
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code

Full text and comments »

  • Vote: I like it
  • +76
  • Vote: I do not like it

By vaaven, history, 6 months ago, In English

Hello, Codeforces! We're glad to invite you to take part in Codeforces Round 953 (Div. 2), which will start on Jun/16/2024 12:05 (Moscow time). Note the unusual start time of the round. You will be given 6 problems and 2 hours to solve them.

This round will be rated for participants whose rating is below 2100. Participants with higher rating can participate unofficially.

The problems were authored and prepared by Artyom123, IzhtskiyTimofey, bashkort, TheEvilBird, 127.0.0.1, Tikhon228 and me.

The round is based on All-Russian olympiad in the name of Keldysh.

We would like to thank

Score distribution: $$$500 - 750 - 1250 - 1500 - 2000 - 2500$$$

UPD: Editorial

Full text and comments »

  • Vote: I like it
  • +249
  • Vote: I do not like it

By vaaven, 21 month(s) ago, translation, In English

1802A - Likes

Idea: Aleks5d, Preparation: vaaven

Solution
Code

1802B - Settlement of Guinea Pigs

Idea and preparation: Aleks5d

Solution
Code

1801A - The Very Beautiful Blanket

Idea and preparation: 4qqqq

Solution
Code

1801B - Buying gifts

Idea: Tikhon228, Preparation: DishonoredRighteous

Solution
Code

1801C - Music Festival

Idea: vaaven, Preparation: ViktorSM

Solution
Code

1801D - The way home

Idea: Tikhon228, Preparation: Ormlis

Solution
Code

1801E - Gasoline prices

Idea and preparation: Kirill22

Solution
Code

1801F - Another n-dimensional chocolate bar

Idea and preparation: Tikhon228

Solution
Code

1801G - A task for substrings

Idea and preparation: grphil

Solution
Code

Full text and comments »

  • Vote: I like it
  • +125
  • Vote: I do not like it

By vaaven, 22 months ago, translation, In English

1793A - Yet Another Promotion was authored and prepared by Ormlis

1793B - Fedya and Array was authored and prepared by TheEvilBird

1793C - Dora and Search was authored by fedoseev.timofey and prepared by vaaven

1793D - Moscow Gorillas was authored and prepared by Gornak40

1793E - Velepin and Marketing was authored and prepared by Tikhon228

1793F - Rebrending was authored by Tikhon228 and prepared by vaaven

1793A - Yet Another Promotion

Let $$$n = (m + 1) \cdot q + r$$$.

Note that you need to use a promotion if $$$a \cdot m \leq b \cdot (m + 1)$$$. In this case, we will buy potatoes $$$q$$$ times for the promotion. The remaining potatoes (or all if the promotion is unprofitable) can be bought at $$$\min(a, b)$$$ per kilogram.

Then the answer is:

$$$q \cdot \min(a \cdot m, b \cdot (m + 1)) + r \cdot \min(a, b)$$$

Thus this solution works in $$$\mathcal{O}(1)$$$

Code

1793B - Fedya and Array

Note that the local minimums and maximums will alternate, and there will be the same number of them $$$k$$$. Let's call the $$$i$$$-th local maximum by $$$a_i$$$, the $$$i$$$-th local minimum by $$$b_i$$$. Without loss of generality, consider that $$$a_i$$$ goes before $$$b_i$$$. To get $$$b_i$$$ from $$$a_i$$$ we need to write out $$$a_i - b_i$$$ numbers, to get $$$a_{(i + 1) \bmod k}$$$ from $$$b_i$$$ we need to write out $$$a_{(i + 1) \bmod k} - b_i$$$ numbers.

Thus, $$$(a_1 - b_1) + (a_2 - b_1) + (a_2 - b_2) + \ldots + (a_k - b_k) + (a_1 - b_k) =$$$

$$$= 2 \cdot (a_1 + a_2 + \ldots + a_k) - 2 \cdot (b_1 + b_2 + \ldots + b_k) = 2 \cdot (A - B) = n$$$

The array $$$[y, y + 1, y + 2, \ldots, x - 1, x, x - 1, x - 2, \ldots, y + 1]$$$ will satisfy the condition.

Code

1793C - Dora and Search

Suppose we want to check whether the entire array satisfies the claim. If this is the case, then we can output the entire array as an answer. Otherwise, one of the two extreme elements does not meet our requirements. From this we can conclude that all segments containing an element that does not meet our requirements will also be incorrect, because this extreme element will remain the minimum/maximum.

The algorithm follows from the fact above: let's look at the sub-section $$$[l; r]$$$, which is initially equal to $$$[1; n]$$$. If $$$a_l = \min(a_{l}, a_{l+1}, \ldots, a_{r})$$$ or $$$a_l = \max(a_l, a_{l +1}, \ldots, a_r)$$$, then we proceed to the segment $$$[l+1; r]$$$. A similar reasoning is also needed for $$$a_r$$$. Thus, either after some iterations we will get the required sub-section, or we will get $$$l == r$$$ and the answer will be $$$-1$$$.

Final asymptotics: $$$\mathcal{O}(n\log n)$$$ or $$$\mathcal{O}(n)$$$ depending on the implementation.

Code

1793D - Moscow Gorillas

Denote by $$$pos_x$$$ the index of the number $$$x$$$ in the permutation. Subsegments with $$$\operatorname{MEX}>1$$$ are as follows $$$1 \le l \le pos_1 \le r \le n$$$.

Denote by: $$$l_x = \min{[pos_1, pos_2, \ldots, pos_x]}$$$, $$$r_x=\max{[pos_1, pos_2, \ldots, os_x]}$$$.

Subsegments with $$$\operatorname{MEX}>x$$$ are as follows $$$1 \le l \le l_x \le r_x \le r \le n$$$. Let's find all subsegments with $$$\operatorname{MEX}=x$$$.

If $$$pos_{x + 1} < l_x$$$, then the subsegments with $$$\operatorname{MEX}=x+1$$$ are as follows $$$pos_{x+1} < l \le l_x \le r_x \le r \le n$$$

If $$$l_x \le pos_{x + 1} \le r_x$$$, then there is no subsegment with $$$\operatorname{MEX}=x+1$$$

If $$$r_x <pos_{x+1}$$$, then the subsegments with $$$\operatorname{MEX}=x+1$$$ are as follows $$$1 \le l \le l_x \le r_x \le r < pos_{x+1}$$$

It remains only to intersect the sets of such subsegments for $$$p$$$ and $$$q$$$, which is done trivially.

Code

1793E - Velepin and Marketing

Let's sort people by their group size requirement. Suppose we have such a person $$$i$$$ that he is not satisfied, and we have a person $$$j > i$$$ who is satisfied. Then we can replace person $$$j$$$ in his group with $$$i$$$ and the answer for us will not be worse. It follows that for a particular $$$k$$$ the answer is some prefix of the people we can make satisfied.

Let us also prove that there exists some arrangement of groups that covers the same prefix, and that each group is a continuous segment. Let's take some correct partitioning into groups. Then each group will be a set of unconnected segments. Let's take the leftmost such segment. Note that we can swap it to the nearest segment of the same group to the right without breaking anything.

Thus we obtained that we can look for a solution in the form of partitioning each prefix into valid groups, which are segments. We will solve this problem using dynamic programming.

Let $$$dp[i]$$$ -- the maximum number of groups into which $$$i$$$th prefix can be partitioned, so that everyone is satisfied (and no elements beyond the prefix can be used). Dynamics base: $$$dp[0] = 0$$$ (empty prefix maximum can be divided into 0 groups). Transition: for $$$i$$$th person his group must have size at least $$$a[i]$$$, so the transition looks like this $$$dp[i] = \underset{0 \leqslant j \leqslant i - a[i]}{\max} dp[j] + 1$$$. But what if $$$a[i] > i$$$? Then we can't dial the $$$i$$$th prefix. Then we put $$$dp[i] = -\infty$$$. This dynamics can be calculated using prefix maximums. This part of the solution works for $$$\mathcal{O}(n)$$$.

Earlier we said that the answer would be some prefix of people who would be satisfied. If we can partition the prefix into some number of groups, then that answer can be the prefix for all $$$k \leqslant dp[i] + n - i$$$. (we partition our prefix into $$$dp$$$, and the rest of the people one by one into the group)

If we can't make the whole prefix satisfied ($$$dp[i] = -\infty$$$), then we need to add people from outside. Thus, the maximum number of groups we can split into if $$$i$$$th prefix is completely satisfied is $$$n - a[i] + 1$$$.

Note that if by some prefix we can score $$$k$$$, then we can also score $$$k - 1$$$ (combining two groups into one). Then we need to find the largest prefix that fits the given $$$k$$$ in the query. This can be done by an array of suffix maximums over $$$\mathcal{O}(q)$$$ total. The final asymptotic of the solution is $$$\mathcal{O}(n \log n + q)$$$.

Code

1793F - Rebrending

Let's go through all the elements from left to right. The main task will be to support the current version of $$$dp[i]$$$ -- the minimum difference of $$$a_i$$$ with the elements to the right of it that we managed to consider. Let us correctly calculate $$$dp$$$ for the first $$$r$$$ elements. Let's move on to $$$r + 1$$$. Let's show how to update the answer for all $$$j < i$$$, such that $$$a[j] > a[i]$$$. For $$$j < i$$$, such that $$$a[j] <a[i]$$$ is solved similarly.

Let's take the first element $$$a[j]$$$ to the left of $$$i$$$, such that $$$a[j] > a[i]$$$. Note that if there is $$$l<j < i$$$ such that $$$a[l] > a[j]> a[i]$$$, then we will not update $$$dp[l]$$$ for it, because $$$|a[l] - a[j]| < |a[l] - a[i]|$$$. Also, we will not update the answer for $$$l$$$ such that $$$|a[l] - a[j]| < |a[l] - a[i]|$$$, that is, if $$$a[l] > a[i] + \frac{a[j] - a[i]}{2}$$$. Therefore, further we will be interested only in the numbers from the segment $$$\left[a[i], a[i] + \frac{a[j] - a[i]}{2}\right]$$$.

Let's note that we have reduced the length of the segment by $$$2$$$ times. That is, there will be no more such iterations than $$$\mathcal{O}(\log n)$$$. You can find the rightmost number belonging to a segment using the segment tree. The answer for the segment $$$l_i, r_i$$$ will be $$$\underset{l_i\leqslant j<r}{\min} dp[l]$$$ at the moment $$$r_i$$$. This can also be efficiently found using the segment tree. The final asymptotics of the solution is $$$\mathcal{O}(n\log^2 n + q\log n)$$$.

There is also a solution for $$$\mathcal{O}(n\sqrt{n} + q\log q)$$$ that passes all the tests.

Code

Full text and comments »

  • Vote: I like it
  • +130
  • Vote: I do not like it

By vaaven, history, 22 months ago, translation, In English

Hi everybody,

This Sunday there will be a Moscow Olympiad for Young Students. This contest is prepared by Moscow Olympiad Scientific Committee that you may know by Moscow Open Olympiad, Moscow Team Olympiad and Metropolises Olympiad (rounds 327, 342, 345, 376, 401, 433, 441, 466, 469, 507, 516, 541, 545, 567, 583, 594, 622, 626, 657, 680, 704, 707, 727, 751, 775, 802, 829).

The round will be held at Feb/12/2023 11:35 (Moscow time) and will last for 2 hours. Note the unusual time of the round. The round will be held according to the Codeforces rules and will be rated.

Problems of this competition were prepared by Tikhon228, TheEvilBird, Ormlis, vaaven, Gornak40, Honey_Badger guided by vaaven, grphil and Helen Andreeva.

Thanks to Artyom123 for the round coordination, and also thanks for MikeMirzayanov for systems Codeforces and Polygon, which was used to prepare problems of this olympiad.

Thanks to BucketPotato, KseniaShk, NemanjaSo2005, MagentaCobra, kuertov, prvocislo, 4qqqq, vsinitsynav, FynjyBath, qualdoom, ivanlarin, didedoshka, petyb, PBBx0, Mikhango, charlyk, RP-1, talant, ak2006 for testing.

Due to the official competition source codes of other participants will not be available for an hour after the end of the round.

Good luck everybody!

UPD: Score distribution: 500 — 1000 — 1250 — 1750 — 2500 — 3250

UPD2: Еditorial

Full text and comments »

  • Vote: I like it
  • -493
  • Vote: I do not like it