Codeforces Round #804 (Div. 2) Editorial reupload

Revision en8, by tibinyte, 2022-10-31 00:16:45

E — Three Days Grace

Author: tibinyte

Solution

We can see that in the final multiset, each number $$$A_i$$$ from the initial multiset will be assigned to a subset of values $$$x_1, x_2,....,x_k$$$ such that their product is $$$A_i$$$. Every such multiset can be created. Also let $$$vmax$$$ be the maximum value in the initial multiset.

Consider iterating through the minimum value. To get the best maximum value that has this minimum we fixed, one can use dynamic programming $$$dp[i][j] = $$$the best possible maximum if we had number $$$i$$$ and the minimum value in the product is $$$j$$$, $$$j$$$ is a divisor of $$$i$$$. This dp can be calculated in $$$O(vmax \cdot log^2(vmax))$$$ for all values. We can also process all updates when incrementing the minimum and keeping the result with a total effort of $$$O(vmax \cdot log^2(vmax))$$$. Thus we have a total time complexity of $$$O(vmax \cdot log^2(vmax))$$$. However, this ( we hope ) won't pass.

Here is a way more elegant solution ( thanks to cadmiumky ):

To get things straight, we observe that when we decompose a number, we just actually write it as a product of numbers. We still consider fixing the minimum value used in our multiset, call it $$$L$$$. We will further consider that we iterate $$$L$$$ from the greatest possible value (i.e. $$$vmax$$$) to $$$1$$$, and as such, we try at each iteration to calculate the minimum possible value which will appear in any decomposition as the maximum value in said decomposition.

We shall now retain for each element the minimal maximum value in a decomposition where the minimum of that decomposition is $$$L$$$, let's say for element $$$i$$$, this value will be stored in $$$dp[i]$$$. Naturally, after calculating this value for every number, we now try to tweak the calculated values as to match the fact that, after this iteration concluded, we will decrease $$$L$$$. For further simplicity, we denote $$$L' = L - 1$$$.

So, we changed the minimum value allowed. What changes now? Well, it is easy to see that any element that is not divisible by $$$L'$$$ won't be affected by this modification, as much as it is impossible to include $$$L'$$$ in any decomposition of said number. So it remains to modify the multiples of $$$L'$$$. Let's take a such number, $$$M$$$. How can we modify $$$dp[M]$$$? Well, we can include $$$L'$$$ in the decomposition as many times as we want, and then when we decide to stop including it, we remain with a number which needs to be further decomposed. The attributed maximum of this value should already be calculated, so we can consider it as a new candidate for the update of $$$dp[M]$$$. This idea could be implemented simpler by going through multiples of $$$L'$$$, and for an element, updating $$$dp[i]$$$ with $$$dp[i / L']$$$ (by taking the minimum of either)

We now need for each iteration to keep track of the attributed maximums of each element that actually appears in our initial list. This can be done by keeping a frequency of all these elements, and after all updates, taking the (already known) maximum of the previous iteration and decreasing it until we find another element that actually appears in our set (this can be verified by simply checking the frequency). This is correct, as much as all the values gradually decrease as $$$L$$$ decreases, so their maximum would have to decrease as well.

Final time complexity: $$$O(vmax * log(vmax))$$$

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en42 English tibinyte 2022-11-06 21:29:15 0 (published)
en41 English tibinyte 2022-11-06 19:15:16 0 (saved to drafts)
en40 English Gheal 2022-10-31 22:14:25 0 (published)
en39 English Gheal 2022-10-31 22:12:04 16
en38 English Gheal 2022-10-31 22:11:13 13 Tiny change: 'er authors of round 804. We are v' -> 'er authors. We are v'
en37 English Gheal 2022-10-31 22:10:53 211
en36 English Gheal 2022-10-31 22:04:25 26
en35 English Gheal 2022-10-31 22:03:40 39
en34 English Gheal 2022-10-31 22:01:52 74
en33 English Gheal 2022-10-31 21:57:17 200
en32 English Gheal 2022-10-31 21:57:01 747
en31 English Gheal 2022-10-31 21:48:53 629
en30 English tibinyte 2022-10-31 21:41:38 7
en29 English tibinyte 2022-10-31 21:41:03 38
en28 English Gheal 2022-10-31 21:38:59 40
en27 English tibinyte 2022-10-31 21:37:09 17
en26 English Gheal 2022-10-31 21:34:15 81
en25 English Gheal 2022-10-31 21:33:19 2311
en24 English tibinyte 2022-10-31 21:33:02 34
en23 English tibinyte 2022-10-31 21:32:50 340
en22 English tibinyte 2022-10-31 21:31:52 3999
en21 English tibinyte 2022-10-31 21:29:21 303
en20 English tibinyte 2022-10-31 21:29:00 18 Tiny change: 'anks to \ncadmiumky\n ):\n\nT' -> 'anks to \n[user:cadmiumky]\n ):\n\nT'
en19 English tibinyte 2022-10-31 21:28:31 4797
en18 English Gheal 2022-10-31 21:27:42 924
en17 English Gheal 2022-10-31 21:18:46 409
en16 English Gheal 2022-10-31 21:16:45 519
en15 English Gheal 2022-10-31 21:13:59 46
en14 English Gheal 2022-10-31 21:12:58 48
en13 English Gheal 2022-10-31 21:11:37 1405
en12 English tibinyte 2022-10-31 00:18:26 7
en11 English tibinyte 2022-10-31 00:18:18 79
en10 English tibinyte 2022-10-31 00:18:10 73
en9 English tibinyte 2022-10-31 00:18:00 3259
en8 English tibinyte 2022-10-31 00:16:45 3531
en7 English tibinyte 2022-10-31 00:15:55 2754
en6 English tibinyte 2022-10-31 00:15:29 2047
en5 English tibinyte 2022-10-31 00:14:20 1085
en4 English tibinyte 2022-10-31 00:12:33 2468
en3 English tibinyte 2022-10-31 00:08:20 10
en2 English tibinyte 2022-10-31 00:07:58 2606
en1 English Gheal 2022-10-30 23:34:08 50 Initial revision (saved to drafts)