Unofficial editorial Codeforces Round #640 (Div. 4)

Revision en25, by spookywooky, 2020-05-09 19:06:35

This is unofficial editorial to first Div4 contest Codeforces Round 640 (Div. 4). Please be lenient, this is my first editorial of a real contest.

In 1352A - Сумма круглых чисел there is a definition of a number to be "round". To create the set of round numbers we need to observe that every single digit of the initial number can be used to create a round number. Just add the numbers of zeros according to the position of the digit. ie "123" becomes "100 20 3".

Code

1352B - Сумма слагаемых одинаковой четности ask us to create a set of numbers where the sum equals a given n. The summands are limited to be all odd or all even, and the number of summands is given as k. So it turns out it is a case work of n and k being odd or even.

If n is odd and k is even there is no solution, since an even number of summands of same parity will allways result in an even sum.

If n is odd and k is odd we can construct a solution by using 1s and a last, bigger number as $$$n-k+1$$$. But we need to check if $$$k>=n$$$ since if it is not then there is no solution.

If n is even and k is even the previus solution works, too.

Last case n is even and k is odd. Here we cannot use 1s as summands, because if we do the sum will be odd, but it must be even. So we use 2s intead of 1s. Therefore we need to check that $$$k*2<=n$$$ and use last number as $$$n-2*(k-1)$$$

Code

1352C - K-е неделящееся на n is a bit mathy. We need to find a number not divisable by n where $$$k-1$$$ lower numbers exist not divisable by n. How to do that?

To tell for a given number how much lower/equal numbers exist being divi by n is simple, it is: $$$k-k/n$$$ Since every nth number is divi by n we can use blocks of size n to calculate the solution. A block of n numbers has $$$n-1$$$ numbers not divi by n. So we need to use $$$k/(n-1)$$$ blocks, plus the remainder of the blocksize. But there is a flaw with this. If the remainder is zero. To workarround that, we can subtract one from solution, and then add one until solution fits.

However, I hope there is a simpler definition, too ;)

Code

1352D - Алиса, Боб и конфеты has lengthy statement to make all points clear. We just need to implement all those details. I did using a left pointer and a right pointer. Then, in a loop, I add elements from left according to the rules, and then elements from the right, until left and right meet somewhere in the middle. There the loop ends and the collected numbers can be printed.

Code

1352E - Особые элементы ask to find the number of elements of an array where the sum of some consecutive subarray equals the element. Seen from the other end it is, for every possible sum is there such an element in the array, lets count such elements. The constraint of max 8000 elements gives us a hint that the solution should work in $$$O(n^2)$$$

First, collect the frequency of all elements of the array. Then lets make a loop over all elements of the array, and on every step maintain a list of the sums of all subarrays ending at that position. This list of sums can be created on the fly by adding the current element to all existing sums, and afterwards adding the current element as a new sum to the list.

Example for an array with elements $$$[1, 17, 3, ...]$$$. After third, before fourth step the list of sums will contain the elements $$$[1+17+3, 17+3, 3]$$$.

So, on every loop we create an inner loop over the sums so far, and check using the frequency array how much elements with the sum exist, adding that number to ans.

Since it is asked how much such elements exist, not how much subarrays, we need to care that every element is counted only once. Just set the frequency of an element to zero after having counted it once. Then it is counted as zero the next time, which does not hurt.

Code

1352F - Восстановление 01-строки makes us constructing a string of 1s and 0s. We are given 3 numbers, which are the number of substrings "00", number of substrings "01" or "10", and number of substrings "11". The constructed string must match these numbers.

One option to create it is first putting the "00" as much as needed, then the "11" as much as needed, then alternating 0s and 1s until n1 is fullfilled. We need to be careful for some corner cases if numbers are zero. But there is no need to check for inconsistency, since the statement says the numbers are so that it is allways possible to create the string. Ie there is no input like 1 0 1.

Code

1352G - Особая перестановка We are aksked to create a permutation of given lenght with adjcent elements being "near" to each other, the diff must be within $$$(2,4)$$$. In the examples there is one such array of size 4, it is $$$[2,4,1,3]$$$

How to create longer such permutations? Observe that we can reuse the one of length 4 by putting one after the other $$$[2, 4, 1, 3, 2+4, 4+4, 1+4, 3+4, 2+8,...]$$$. But by this pattern we can create only permutations of length multiple of 4. If we had solutions of size 5, 6 and 7, we could put them after some of size 4...

I used pen and paper to find such ones: $$$[1,4,2,5,3], [1,4,2,6,3,5], [1,4,2,6,3,7,5]$$$ One might use brute force to find them programatically. Edit: And as I just noticed, in the example there are two of length 6 and 7, too.

So we can construct the final permutation by putting $$$(n/4)-1$$$ arrays of size 4 one after the other, and then add one of size $$$4 + n\%4$$$

Note that for $$$n<4$$$ there is no solution.

Code
Tags div4, editorial, beginner, 1352

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en30 English spookywooky 2020-05-10 00:38:25 6 Tiny change: 'hat for $n<4$ there i' -> 'hat for $n{\lt}4$ there i'
en29 English spookywooky 2020-05-10 00:30:06 62
en28 English spookywooky 2020-05-09 21:48:50 4197
en27 English spookywooky 2020-05-09 19:36:06 0 (published)
en26 English spookywooky 2020-05-09 19:09:36 61
en25 English spookywooky 2020-05-09 19:06:35 512
en24 English spookywooky 2020-05-09 18:57:14 6 Tiny change: ' k is even, too, the previ' -> ' k is even the previ'
en23 English spookywooky 2020-05-09 18:54:33 50
en22 English spookywooky 2020-05-09 18:42:53 10
en21 English spookywooky 2020-05-09 18:39:56 572
en20 English spookywooky 2020-05-09 18:38:14 527
en19 English spookywooky 2020-05-09 18:35:30 760
en18 English spookywooky 2020-05-09 18:32:26 764
en17 English spookywooky 2020-05-09 18:29:52 760
en16 English spookywooky 2020-05-09 18:28:16 233
en15 English spookywooky 2020-05-09 18:25:31 1030
en14 English spookywooky 2020-05-09 18:23:47 365
en13 English spookywooky 2020-05-09 18:18:37 640
en12 English spookywooky 2020-05-09 14:20:14 2 Tiny change: 'ize $4 + n%%4$\n\nNot' -> 'ize $4 + n\%4$\n\nNot'
en11 English spookywooky 2020-05-09 14:18:26 7 Tiny change: 'ize $4 + n mod 4$\n\nNote' -> 'ize $4 + n%%4$\n\nNote'
en10 English spookywooky 2020-05-09 14:17:49 10 Tiny change: ' of size $n%4+4$\n\nNote' -> ' of size $4 + n mod 4$\n\nNote'
en9 English spookywooky 2020-05-09 14:16:36 2
en8 English spookywooky 2020-05-09 14:15:03 13
en7 English spookywooky 2020-05-09 14:11:06 4
en6 English spookywooky 2020-05-09 14:10:17 63
en5 English spookywooky 2020-05-09 14:07:27 27
en4 English spookywooky 2020-05-09 14:00:12 1352
en3 English spookywooky 2020-05-09 13:04:14 1279
en2 English spookywooky 2020-05-09 12:46:21 8
en1 English spookywooky 2020-05-09 12:43:31 2273 Initial revision (saved to drafts)