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

Автор shishyando, 4 года назад, перевод, По-русски

Опять же, я надеюсь, что вам понравились все задачи. Делитесь своими идеями и альтернативными решениями в комментариях. Итак, разбор:

A: НОД против НОК
B: Техника массивного клонирования
C: Заражение дерева
D: НОД-угадайка
E: МинимизатOR
Кто что делал?
Разбор задач Codeforces Round 781 (Div. 2)
  • Проголосовать: нравится
  • +143
  • Проголосовать: не нравится

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

Автокомментарий: текст был обновлен пользователем shishyando (предыдущая версия, новая версия, сравнить).

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

Thanks for the fast editorial!

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

Thanks for the fastest editorial.

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

Seems like fast editorials are being the trend recently. A welcoming trend, for sure.

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

Wow, Fast editorial
Thank you very much

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

Thanks for the fast editorial! And problems are so nice :)

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

Спасибо за контест, подскажите, когда начислится рейтинг?

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

can someone explain D with more details

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

    Here was my reasoning (same as first approach):

    • 30 Queries to differentiate about 2^30 candidate numbers implies each query must learn 1 bit
    • hence looked for way to first learn LSB, then 2nd LSB etc.
    • Use fact 1 that gcd(x+a,x+b) = gcd(x+a,b-a) if a<b from property that gcd(x,y) = gcd(x,y-x) if x<y
    • Use fact 2 that if x = ?????000 (i.e ends with n zeros), then gcd(x,2^n)=2^n. (notice x is divisible by 2^n and there cannot be any larger number than this that divides 2^n).
    • Say we have learned that x = ?????01. Then, removing the last two bits (by subtracting 1) to get x', and then doing gcd with 2^2 will tell us 3rd bit.
    • In other words, if we know X starts with x in the first n bits, then doing gcd(X-x, 2^n) tells us next bit
    • This gives us a good strategy, but how do we translate to a suitable a and b?

    Answer? Use fact 1 ,gcd(X+ (2^(n) — x), X + (2^(n+1)-x)) = gcd(X + (2^n -x),2^n) = gcd(X-x,2^n), which is used as an indicator for next bit.

    At step n+1, we know the first n bits of X, i.e. x. Propose a= 2^(n)-x; b = 2^(n+1)-x; If gcd is 2^n, then next bit is 1, else 0

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

      still don't get it..

      "Say we have learned that x = ?????01. Then, removing the last two bits (by subtracting 1) to get x', and then doing gcd with 2^2 will tell us 3rd bit."

      If we remove last two bits in x, then gcd with 2^2 will be 2^2.. doesn't matter what 3rd bit is. How do we get whether 3rd bit is 0 or 1?

      "if we know X starts with x in the first n bits, then doing gcd(X-x, 2^n) tells us next bit."

      won't the gcd always be 2^n if you remove first n bits from X ?

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

        You are right. To check bit n, you need gcd with 2^(n+1).

        I meant to say that you need to check with gcd 2^3. If the next bit is a zero, then the gcd will give you 2^3, otherwise next bit is a one.

        The rest needs a little tweaking but the idea is the same.

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

      Thanks for the great explanation. But, I think this would need some workaround as 2^(n + 1) would exceed 2*10^9 (if n = 30).

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

      why If gcd is 2^n, then next bit is 1, else 0

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

        Because if the next bit was 0, it would imply that the number has another 2 as a factor. This would mean that there exists a greater divisor than 2^n, i.e. gcd >= 2^(n+1). Since this is not true, if gcd is 2^n then next bit must be 1

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

    Basically, you should visualize the number in the binary form. Now you can check what is the kth bit if you have Y, with the propriety: X + Y has the first (k — 1) bits set. Now you can check if the kth bit is set by querying (Y + 1 , Y + 1 + 2^(bit + 1) , if this is 2 ^ (bit), then you add it to the answer, otherwise you update Y.

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

    You can check my comment dowm, I tried to explain D clearly

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

Thanks for this rubbish trash boring disgusting fucking meaningless Problem C that I didn't have enough time to write E

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

Sonic Speed Editorial

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

Is there any other solution for A other than [1,n-3,1,1]||[n-3,1,1,1]??

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

why so tight constraint(a,b<2000000000) for d? that forced to think exactly like you which is not always possible;

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

Can someone please explain why my C solution doesn't work? 153069119

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

A very educational round with a fast editorial!!! I really like it, especially for the nice E, extremely educational.

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

In Editorial 1 for D, shouldn't it be $$$x \pmod{2^{k+1}} = r + 2^k$$$ ?

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

It seems like my solution for C is different from the editoral, I used binary search

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

    How do you determine if the tree can be infected in say x seconds (I understand if we can infect it in x then we can infect it in X+1,X+2,...N)? Or can you explain your approach?

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

      In my solution I don't work on a tree, just all nodes of the same parent are put in a set (and node $$$1$$$ is alone in a set) and I make an array for the sizes of those sets, when I infect a set, it decreases by one every second, and for a set to decrease I need to inject it, so I will inject all $$$m$$$ sets first (lets say we have $$$m$$$ sets), it is obvious I want the biggest set to be injected first, so it decrease more early than the other sets, since I inject the $$$m$$$ sets I will use $$$m$$$ seconds, if I sort all sets by size, for each $$$i$$$ $$$(1 \lt = i \lt = m)$$$ set number $$$i$$$ size is decreased by $$$i$$$ elements (the size of the set is the number of non-infected nodes), so I already made the optimal move for the first $$$m$$$ sets, I already know now that for every second all sets decrease by one, so I can binary search on the number of extra second $$$x$$$, I decrease every set size by $$$x$$$, and then I get the summation of the remaining positive sizes of sets, if it is smaller than or equal to $$$x$$$ then x is valid (because I have $$$x$$$ injections) otherwise $$$x$$$ is not valid, and the answer is the $$$minimum$$$ $$$valid$$$ $$$x + m$$$

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

In my opinion,D is much easier than C,since it takes me almost an hour to solve C and WA 5 times,and it takes me half on hour to solve D.

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

/

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

Thousands of people learned today not to use stl::unordered__map in competitive programming.

Yeah I used to know about it, but I've been out of the game for so long that I completely forgot lol.

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

C was interesting, but I wasn't able to solve it. Thanks for the round!

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

Problem B giving tle on unordered_map code while got ac using map code. I know unordered_map time complexity is O(n) in worst case and O(1) in best case so should we only use map for safer side? and how to determine where should we use unordered_map or map? thanks in advance.

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

If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints.

I've also added 2 new features: Near real-time log streaming and Compressed Parameter editing without using tables.

If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).

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

Why unordered map is giving TLE in problem B, but map works?? My solution was basically the same as the one in the editorial, only difference was it used unordered map instead of map.

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

I have a solution E that catches ML.

Let's build a merge sort tree from tries. Then when we respond to the request, we make a descent over <= log n trues. We go from the highest bit to the smallest. Now let's say there are cnt numbers by which we can go left. Then if cnt >= 2, then we'll just go left. And if cnt < 1, then if cnt = 1, then we add this number to some array b on the left, and we ourselves go to the right. After that, the answer will be found as some b[i] | b[j]

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

failed maine test on problem C , WA on test 20 can someone tell what's wrong in this code ?

Solution link

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

Did anyone AC problem D using the CRT solution?

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

I had a slightly different approach to D.

At every step, I would add padding to the number and make sure that it is divisible by 2, 4, 8, ... .

For example, if the hidden number is 5, I would first check if it is even by asking (+2, +4).

Since it is not even, I must pad the number by +1.

Then, I make sure it is divisible by 4. I ask (padding, padding+4), which is (+1, +1+4). This returns 2, which means that I must further add 2 to the padding to get +3 padding.

Then, I make sure it is divisible by 8. I ask (padding, padding+8), which is (+3, +3+8). This returns 8, which means that I must further add 8 to the padding to get +11 padding.

I repeat this process until 2^30 and I can finally subtract the padding from 2^30 to get the original number.

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

unordered_map gave TLE in problem B.. (T T)

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

In C

You do two operations every second

Doesn't that mean you do at most 2 operations each second?

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

Can someone please explain why my C solution doesn't work? Solution

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

The 2nd solution for problem D is neat! Thanks!

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

So many fsts for test case 20 in problem C. Can anyone explain why I got fst on test 20 ? 153064962

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

In editorial of Problem D [Solution 1], the value of x mod 2^(k+1) should be r + 2^k and not r+2^(k-1).

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

I have another approach for E.Let we will solve all queries together.We will go from greater bit to lowest.If we have at least two zeros in a greater bit we have 0 in it in the answer,and we can push the query to the array of numbers,which have zeros at our bit.Otherwise,we push the query to the array of all numbers we have now.This solution works in O(nlog^2) memory and O(nlog^3) time.The every query was in 30 layers one time.Let we will count sum of the lengths of the arrays we process.It is the sum for each element of it's total count.It is at most 30*(count of nodes where element goes to both sons).But if in some node element goes to both sons,then is has 0 in this bit,and it was a query when it is the only zero.But then the sum of (count of nodes where element goes to both sons) by elements is at most number of queries at all layers(each query gives us at most 1)<=30*q,so our soluiton runs in (n+q)*30^2 memory and (n+q)*30^2*log time. (The main moment is that in each node i eliminate all elements,which are not used in queries).So we have much slower solution,but without and ideas:)

link: https://mirror.codeforces.com/contest/1665/submission/153052760

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

Can anyone tell me why this solution for C doesn't work? Link

Code explanation

Thanks in advance for your help

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

In problem c, After updating the atleat one child of a parent as infect we use ans++ and cnt-1 to queue i think in one time we (spread+inject) so ans+=2 and cnt-2 will be inserted can anyone clarify https://mirror.codeforces.com/contest/1665/submission/153080174 link to my sol

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

I think my solution for 1665E - MinimizOR is hackable: 153083146 In short, I split queries bit by bit, and when I set bit to 0 I also filter the numbers.

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

Here's a screencast of me doing todays codeforces round:))

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

ENG: D could be done even simpler in my opinion!

Idea

RU: На мой взгляд, D можно было решить даже проще!

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

This editorial is very fast!!!

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

How can I get the key idea for the solution is that the answer always lies among no more than 31 minimal numbers? why?

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

I love problem E because it has so many different ways to solve it!

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

Thanks for many different solutions!

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

Could someone please explain me the "Other solutions" of problem E?I think I don't really understand that.

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

Tyyyyyy will be crooked

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

Can anyone help me, why is my solution for A giving WA?

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

Can anyone help why my submission for problem C has FST WA on testcase 20. 153086129 I calculated the time required for injecting one sibling, then tried to spread and inject other.

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

Can anyone tell why in problem C the answer to the following test case should be 4 and not 5? 8 4 8 8 1 8 1

  • »
    »
    4 года назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится
    • (1 = spread : nothing | infect : vertex 2)
    • (2 = spread : vertex 4 from vertex 2 | infect : vertex 6)
    • (3 = spread : vertex 5 from vertex 2 and vertex 8 from vertex 6 | infect : vertex 3)
    • (4 = spread : vertex 7 from vertex 2 | infect : vertex 1)
»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Можно, пожалуйста, пояснение по задаче D? Почему gcd(x + 2^k — r, 2^(k+1)) равен gcd(x + 2^k — r, x + 2^k — r + 2^(k + 1))??? Откуда в правой части взялось 2^k — r? И почему gcd должен быть именно 2^(k — 1)? И почему при этом остаток становится r + 2^(k — 1)??

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

    Почитай мой коммент выше, постарался предельно ясно обьяснить, если что, готов и на вопросы ответить

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

Can anyone explain why my C solution is not working? submission

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

Problem D, we asked $$$\gcd(x+2^k-r,2^{k+1})$$$. If the result is $$$2^{k+1}$$$, shouldn't $$$x\bmod 2^{k+1}=r+2^k$$$?

Or am I mistaken?

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

What brief problems there are! Just like math!

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

I really don't understand why my solution for Problem B got TLE on test case 13 when I used hash map. And after the contest when I submitted same solution just by using simple map, it got accepted.

TLE

Accepted

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

average CRT D solver

1d5826c6-7399-40a9-8f9f-e0662ab6fef0

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

I'm having some trouble figuring out my error for problem C. I tried printing the test case I got wrong (test 6 case 246) and got:

10
3 8 3 1 1 3 1 1 3 5

Am I missing something or shouldn't this test case be n=11 since there are 10 inputs on the second line? Original submission: https://mirror.codeforces.com/contest/1665/submission/153081192

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

I dont understand why i get wrong-ans in problem C test case 20. I try so hard but can't fix it.Plz help me anyone to fix.

https://mirror.codeforces.com/contest/1665/submission/153127622

Thanks.

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

Getting TLE on test case 5 in Java O(nlogn) solution, can someone tell what's wrong? link

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

spookywooky can you explain this part of your code for problem C..!

    while(cnt<q.top()) {
        int v=q.top();
        q.pop();
        cnt++;
        q.push(v-1);
    }
  • »
    »
    4 года назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    This is simulating the process. The idea is to maintain that priority_queue, where we store foreach vertex the day that all children of that vertex are finished. $$$cnt$$$ is the number of days.

    Initially we put all vertex into the queue. Then, when each vertex has at least one infected child, we infect every day another child of the vertex with the most uninfected childs, and put that decremented number on the q.

    Note that in the beginning, we have to add an artificial vertex, which is the parent of the root vertex. We need this, because the root vertex also needs to become infected.

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

Can someone please tell me where I'm making error for Problem (C)? I have implemented exactly what's the tutorial says. I had figured out the solution but failed to implement it. I have looked at my solution multiple times but don't know what I'm missing. Please help me out.

You can look at my submission: https://mirror.codeforces.com/contest/1665/submission/153271339

I have added comments for better code readability.

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

For problem D, wouldn't $$$x \mod {2^{k + 1}} = r + 2^k$$$ if the gcd is equal to $$$2^{k + 1}$$$? (not k — 1 as written)?

My reasoning is as follows: let $$$x = y\cdot 2^k + r$$$. Thus, the gcd becomes $$$\gcd((y + 1)\cdot 2^k, (y + 3)\cdot 2^k)$$$. If the gcd is $$$2^{k + 1}$$$, that means $$$y$$$ is odd. Thus $$$x = (y - 1)\cdot 2^k + 2^k + r$$$. Since $$$y - 1$$$ is even, $$$(y-1)\cdot2^k$$$ can be written as a multiple of $$$2^{k + 1},$$$ making $$$x \equiv 2^k + r \pmod {2^{k + 1}}.$$$

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

I want to know if the problem A ask you print all solution about n, how can I solve it?

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

I solved Problem E differently: maintain a trie of all elements of the array: also each node of the trie should store the indices which correspond to the node in the trie: so each index corresponds to $$$log(A_{max})$$$ nodes in the trie, one for each bit of the element.

Now for a query, we are initially in the root node of the trie. We want to find out the set of numbers which result in the minimum OR. If the left son ($$$0$$$ bit) of the current node has $$$\geq 2$$$ nodes in the query range, we go to that node, otherwise if it has exactly $$$1$$$ node in the query range, we add that value to our set of possibilities and go to the right son. If it has $$$0$$$ nodes we just go to the right son.

Formally, the invariant maintained is that the union of set of possibilities and the subtree of the current node stores two values which give the minimum OR. Easy to prove, that this invariant is maintained in every step.

153497046

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

    Thanks for sharing your solution. One question though, I think the below code-snippet isn't correct without checking how many of those positions (ie. tr[curr].pos) fall between $$$[l, r]$$$. I kind of understand why this works, because if none falls between the given range, regardless of how many of them added, they'll not be part of the answer. Therefore, my point is rather on the correctness of the algorithm. I think the correct way would be using your cnt function to find how many falls in the range. What do you think?

    if (is_num)
    {
    	for (int z = 0; z < min(2ll, (int)tr[curr].pos.size()); z++)
    	{
    		possible.push_back(num);
    	}
    }
    
    • »
      »
      »
      4 года назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      thank you sooo much. you are absolutely right. the algorithm is obviously correct, but this implementation detail was wrong, the correct snippet, as you pointed out, is

      if (is_num)
      {
              int count=cnt(tr[curr].pos, l, r);
      	for (int z = 0; z < min(2ll, count); z++)
      	{
      		possible.push_back(num);
      	}
      }
      
  • »
    »
    4 года назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I have a similar solution, but I use Wavelet tree instead of Trie: 153497046

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

.

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

<-- How to solve A?

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

Can Someone help me Why my Solution is Wrong... https://mirror.codeforces.com/contest/1665/submission/258157824

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

For B: Array Cloning Technique, my approach is a bit different. Whatever element appears the most times in the array, we’re definitely going to replicate it — that’s our main target.

Let’s say m = highest frequency of any element, and n = size of the array.

First, to make the whole array the same element, we’ll need to replace all the non-m elements. That’s (n - m) replacements in total — these are our swaps.

Now, the key idea: We can copy the array, which lets us spread the current majority element faster.

Example:

n = 6, m = 2  
Array: [0, 0, 1, 3, 4, 5]   # sorted here just for clarity

We need 4 swaps to turn everything into 0.

Step 1 – Copy the array:

Original: [0, 0, 1, 3, 4, 5]  
Copied:   [0, 0, 1, 3, 4, 5]

Now swap in 0’s where possible:

Original: [0, 0, 0, 0, 4, 5]  
Copied:   [1, 3, 1, 3, 4, 5]

Step 2 – Copy again:

Original: [0, 0, 0, 0, 4, 5]  
Copied:   [0, 0, 0, 0, 4, 5]

Now we only need 2 more swaps:

Original: [0, 0, 0, 0, 0, 0]  
Copied:   [0, 0, 4, 5, 4, 5]

Done — all zeroes.


The pattern here is: after k copy operations, we have:

$$$ (2^k) \cdot m $$$

copies of our majority element.

We want the smallest k such that:

$$$ (2^k) \cdot m \ge n $$$

That’s the same as:

$$$ k \ge \log_2\left(\frac{n}{m}\right) $$$

So:

$$$ k = \lceil \log_2(\tfrac{n}{m}) \rceil $$$

Why not just use a while loop to find k?

In Python, this can cause TLE for large inputs because doubling in a loop is slow compared to C++. So instead, we jump straight to the answer using ceil(log2(n/m)) — O(1) calculation.

Final formula:

Answer = k + (n - m)

Where:

k = ceil(log2(n/m))

That’s it — no unnecessary loops.