Блог пользователя Artyom123

Автор Artyom123, история, 3 года назад, По-английски

1928A - Rectangle Cutting

Solution
Code

1928B - Equalize

Solution
Code

1928C - Physical Education Lesson

Solution
Code

1928D - Lonely Mountain Dungeons

Solution
Code

1928E - Modular Sequence

Solution
Code

1928F - Digital Patterns

Solution
Code
Разбор задач Codeforces Round 924 (Div. 2)
  • Проголосовать: нравится
  • +127
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +75 Проголосовать: не нравится
  1. Why do the authors set $$$\sum c_i\le 2\cdot 10^5$$$ in problem D? There exists both $$$\mathcal O(n\log \max c_i)$$$ and $$$\mathcal O(n\sqrt{\max c_i})$$$ solutions.

  2. Why do the authors set $$$\sum s \le 2\cdot 10^5$$$ in problem E? A init function can precalc the dp array. And the limit for $$$\sum s$$$ made it hard to hack brute-force solutions, for example, just simply do dfs. And the tests are quite weak that some dfs solutions passed the ST. Most of them can be uphacked with the test:

1
673 0 1 200000

However, we can't hack 245835914 with the test above, can anyone help to hack it? :)

Anyway, I found most of the problemset nice. Kudos to the authors & coordinator!

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

thanks for fast editorial

»
3 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Great problems overall, but felt that statements were often convoluted and difficult to comprehend. Still, thanks for the great contest.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can anyone hack my solution?

»
3 года назад, скрыть # |
 
Проголосовать: нравится -26 Проголосовать: не нравится

bad F :(

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can we solve D by binary search?

  • »
    »
    3 года назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится +1 Проголосовать: не нравится

    yes we can do that take minm squads as 1 and maximum can be max(c[i]) then we can easily see that value of our predicate function first increases and then decreases so it's like we have to find peak value in rotated sorted array which could be solved by binary or ternary search ,here is model solution using ternary search https://codeforces.me/contest/1928/submission/245874386 hope it helps

    • »
      »
      »
      3 года назад, скрыть # ^ |
       
      Проголосовать: нравится +3 Проголосовать: не нравится

      Let's say that we have to calculate the max value of h(k)

      where h(k) = f(arr, k)-g(k)

      g(k) = (k-1)*x

      f(arr, k) is the total number of pairs after optimal distribution.

      I get that k will be in the range: [1, max(arr[i])] but cannot understand why h(k) will be a unimodal function. Can you prove?

      • »
        »
        »
        »
        3 года назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        for each element i of the array (lets name it arr), when you increase the number of squads k, you are distributing the creatures more and more, which means that you are increasing the total number of pairs.

        On the other hand, when x is greater than 0, it keeps decreasing the score when k increases.

        So, you can see the score as a combination of an increasing function and a decreasing function.

        One last thing to observe is that when k increases, the increasing function will increase slower and slower. This is because, for every element i of arr, we cannot distribute the creatures in more than arr[i] squads. Meanwhile, the decreasing function is always constant.

        As a conclusion of this, the total score function will either be always decreasing (when the decreasing function keeps decreasing faster than the increasing function even from k=1) or will increase then stop and start decreasing.

        I hope this helps you.

        • »
          »
          »
          »
          »
          3 года назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          While your statements don't imply that the function is unimodal, they still prove why ternary search works(f(x) = f(x + 1) is only possible for x where f(x) is maximum). Thanks a lot!

          • »
            »
            »
            »
            »
            »
            3 года назад, скрыть # ^ |
             
            Проголосовать: нравится 0 Проголосовать: не нравится

            could you precise why it doesn't imply that the function is unimodal? is there anything wrong? or is it because I didnt formulate my statements well?

            • »
              »
              »
              »
              »
              »
              »
              3 года назад, скрыть # ^ |
              ← Rev. 2  
              Проголосовать: нравится 0 Проголосовать: не нравится

              Unimodality requires precisely one 'x' for which the function monotonically increases and decreases before and after.

              Consider a case where increasing bins adds exactly k to the answer. (For example, take k = difference due to increasing the number of pairs for some x bins). In this case, f(x) = f(x + 1), which means f(x) is not unimodal.

              This wouldn't generally work for ternary search because if f(x) = f(x + 1) for some point other than maxima, we might remove the region of interest in the answer. However, since it only happens at maxima in the question(your statement proves this), we never remove the region of interest and ternary search works!

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    It's an upside-down parabola. So ternary search.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

is there any way to download the test case where answer is getting wrong ?

»
3 года назад, скрыть # |
← Rev. 6  
Проголосовать: нравится +9 Проголосовать: не нравится

Problem C: "To consider the second case, it is necessary to proceed similarly with the number n+x−2"

How does it come (n+x-2) for the second case !

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

D using ternary search: solution

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem B: the difference between the maximum and minimum does not exceed n−1 , can become equal.. Is any mathematical proof available to prove that all such sets can always be satisfied?

P.S. Thank you for the fast editorial

»
3 года назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

Artyom123 Code for problem B has two main functions, probably you could edit that. Thanks for the tutorial!

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Please explain C. I have been trying it for like 2 hours and dont get it. The editorial is so unclear

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    and yeah i HATE the code for it. Was it really necessary to write this way?

  • »
    »
    3 года назад, скрыть # ^ |
    ← Rev. 5  
    Проголосовать: нравится +15 Проголосовать: не нравится

    I did it using periodicity of the function. You can see that the whole pattern $$$1,2,3,..,k,k-1,...,2$$$ starts repeating after $$$2k-2$$$ terms. So basically, the graph looks like:

     where $$$t$$$ axis denotes the person number $$$(1,2,..)$$$ and $$$y$$$ is the label assigned. So, now for this graph to have intersection with $$$y=x$$$ one condition will be: $$$k \ge x$$$. Now, we have two cases:

    $$$1.$$$ $$$x=n$$$ is in one of such "rising" lines. From this, we get an equation like: $$$x+p \cdot (2k-2) = n$$$, where $$$p \in \mathbb{Z}$$$, from here we simply get $$$(2k-2) | (n-x)$$$, now you can find all factors of $$$n-x$$$, and impose two conditions, first it should be even (as $$$2k-2$$$ is even), and second, that $$$k = (f+2)/2 \ge x$$$.

    $$$2.$$$ If $$$x=n$$$ in one of the "falling" lines, then you will get $$$2k-x + p \cdot (2k-2) = n$$$, which again can be manipulated as $$$(p+1) \cdot (2k-2) = n+x-2$$$ (not so trivial like the first case) $$$\implies (2k-2) | (n+x-2)$$$, then again you have to check for factors of $$$n+x-2$$$ and do the same thing as in case $$$1$$$ !

    I think I thought very mathematically but I guess the logic should be clear to you now!

    My submission: 245852020

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone please point out the error in my logic to find out the maximum length of the subarray? Even after doing multiple dry runs i am not able to figure out the logical error. 245902899

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +20 Проголосовать: не нравится

P.S. A solution in $$$O(q\sqrt(n))$$$ will not work due to a large constant. I tried very hard to rule it out :D.

My (n+m+q)(log(n)+log(m)) collaterally died :(

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I had a question regarding problem D. Lets call the array containing count of races "cnt" and sort it. Lets say cnt[3]=3 and cnt[4]=6 in a given test case. Is it ever beneficial for us to select number of squads=4 or 5? What I want to know is that is it ever beneficial for us to choose number of squads!= some cnt[i].

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Great solution for D!

Sadly I didn't solve it,I'm too bad:(

»
3 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

In C why divisors of n+x-2

»
3 года назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

I will appreciate if someone could either prove or hack this greedy solution to E

https://codeforces.me/contest/1928/submission/245926933

See https://codeforces.me/blog/entry/125652?#comment-1115429 for an explanation of the greedy approach.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

Curious to know why there are couple of comments as "bug1" and "bug2" in the code solution of 1928C — Physical Education Lesson. Are there any flaws in the code implementation ?

»
3 года назад, скрыть # |
 
Проголосовать: нравится +29 Проголосовать: не нравится

245940869 my $$$q\sqrt n$$$ works (:

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For the code solution to problem B, the code contains 2 instances of signed main() Just a minor typo though.

»
3 года назад, скрыть # |
← Rev. 4  
Проголосовать: нравится +13 Проголосовать: не нравится

Another approach for Div2 D [ Note that this approach does not rely on the sum of $$$c_i$$$ being bounded by $$$2e5$$$ ]

Let's notice the formula for a particular $$$c_i$$$ if $$$k$$$ (number of squads) is fixed. If $$$q = c_i / k$$$ and $$$r = c_i \mod k$$$, then the number of extra pairs is $$$c_i(c_i - 1) / 2 - [(k-r)*q*(q-1)/2 + r*q*(q+1)/2]$$$ [Idea: Subtract the bad pairs from total number of pairs). We need to sum this up for all $$$i$$$ from $$$0..(n-1)$$$

The first term is easy, we can precalculate the sum and store it. Focus on the next two terms inside square bracket. Upon simplification: $$$k*q*(q-1)/2 + qr$$$. We have to sum this for each $$$i$$$.

Let's fix $$$k$$$ and all multiples of $$$k$$$ [This can be done easily]. Consider the multiple $$$mk$$$. For all values of $$$c_i$$$ in the range $$$[mk ... (m+1)k)$$$ the quotient is $$$m$$$ and the remainder is $$$c[i] - m*k$$$. We just have to sum them up by plugging in the formula above. The term $$$k*q*(q-1)/2$$$ is easy. It is constant. For $$$q*r$$$, we will need prefix sums of $$$c$$$. Locating the values of $$$c$$$ in the range can be done using binary search

Time complexity: $$$O(\max{c_i} \log (\max{c_i}) \log n)$$$. Iterating through the multiples and doing binary search to find the range of c.

Submission: https://codeforces.me/contest/1928/submission/245957808

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What is the name of the category in which problems like D — > Lonely Mountain Dungeons lies? Can someone please share a list of problem of similar nature for practice?

»
3 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

P.S. A solution in $$$O(q\sqrt{n})$$$ will not work due to a large constant. I tried very hard to rule it out :D.

Sorry, I didn't know (I think it would be faster if didn't choose a random block size)

  • »
    »
    3 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Can you explain your solution? Could it be that there will be $$$O(q \sqrt[3]{n})$$$ real calculations here?

    • »
      »
      »
      3 года назад, скрыть # ^ |
      ← Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      As in the official solution, we have some (multiset $$$H$$$ of) horizontal blocks and some vertical blocks $$$V$$$. The answer is then

      $$$\displaystyle\sum_{s=1}^{\infty}\sum_{x\in H}\max(0, x - s + 1)\sum_{y\in V}\max(0, y - s + 1).$$$

      For each $$$s$$$, I maintain $$$h_s = \sum_{x\in H}\max(0, x - s + 1)$$$ and $$$v_s = \sum_{y\in V}\max(0, y - s + 1)$$$, as well as the $$$\sum h_iv_i$$$. Sometimes I have events of sort "one horizontal/vertical block of length $$$x$$$ appeared/disappeared". Each such event is equivalent to the query "for each $$$i$$$ from $$$1$$$ to $$$x$$$, add $$$sgn\cdot (x + 1 - i)$$$ to $$$h_i$$$/$$$v_i$$$", where $$$sgn = \pm 1$$$.

      So first I wanted to maintain it with segment tree, storing in each node the following: if the corresponding (horizontal/vertical) blocks on this segment are $$$a_0$$$, $$$\ldots$$$, $$$a_{l-1}$$$, then I store $$$\sum a_i$$$ and $$$\sum i\cdot a_i$$$, and also the dot product $$$\sum h_iv_i$$$. But then it occurred to me that pushing delayed operations is kinda nontrivial. That's why I split everything into blocks of $$$K$$$, and each event is now "add some arithmetic progression to about $$$n/K$$$ whole blocks, and also maybe on the prefix of another block". This can be done quite easily, if we remember for each block the current arithmetic progression to be added (and also what I wanted to store for the segment tree). So yes, this works in something like $$$O(q(n/K + K)) = O(q\sqrt{n})$$$, and I just set $$$K = 300$$$ and submitted. I think $$$K = 600$$$ or so would probably be better, idk.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone explain what is my mistake in task B? 245838570

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I was looking at the code for problem C (physical education lesson). I think there's a mistake. while populating the unordered_set answer, it should be answer.insert((i + 2)/2).

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in the problem tag in problem D: there is ternary search. How can we use it in Problem D??

»
3 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone check my submission in problem D? I cannot demonstrate my solution clearly.

Thank you and have a good day!

My submission: 245993112

»
3 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Can someone prove that the function for D is unimodal?

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

not even kidding , problem 3 was running correctly in contest just because of a single if condition (if divisor is less than x than its possible) , accepted after contest.

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Does not the solution to the problem B yield n^2 complexity? Like, for the test case where n = 2x10E5 and a = [1, 2, 3, ... , 2x10E5].

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem C video Editorial : Youtube Video Link

If you are not able to understand the Editorial then you can refer to this video....I have explained the complete solution in Hindi

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

How to solve E using graphs?

»
3 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

.