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

Автор Hori, 18 месяцев назад, По-английски
A
B
C
D
E
F
G1
G2
H
Разбор задач Codeforces Global Round 27
  • Проголосовать: нравится
  • +117
  • Проголосовать: не нравится

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

Okay so D is done by ChatGPT but also oursaco. oursaco, if you don't mind, can you please tell a little bit about how you contributed to the creation of that problem? Like did you just write the prompt or did the chatGPT stuff have to be revised?

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

    Asked chatgpt to make a problem which was initially solving the problem for a single array with no i < j constraint (this was an existing div2a). I thought the i < j constraint would make it more interesting and solved for it. Then the solve for every prefix part was added. Also made sure to plug it back into o1-preview to make sure it couldnt solve XD

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

Code is not accessible.

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

cant see tutorial of D, but is it possible to solve D using knapsack?

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

    I didn't solve it, but I think D is to be solved using the observation: if $$$a_i=2^{p_i} \cdot {q_i}$$$, there should be $$$p_i$$$ operations that greedily decrease $$$A_i$$$ and greedily increase the member of the prefix with greater or equal index and max $$$q$$$.

    With this, we can compute the optimal sum for each prefix separately in quadratic time. Or, we can compute for each prefix using the the previous prefix in linear time (somewhat like DP).

    Essentially, we can process the $$$i^{\text{it}}$$$ prefix as adding $$$A_i$$$ to an array $$$p$$$. By adding $$$A_i$$$, we offer a new $$$q$$$ to all of the elements that precede it. We obviously want all of $$$A_1, \cdots, A_{i-1}$$$ that are using their operations to increase an element with a lesser $$$q$$$ to instead increase $$$A_i$$$. All of the elements that precede the nearest element with a greater $$$q$$$ will not change, but those that follow will now use their operations to increase $$$A_i$$$. We can find the nearest greater element with a greater $$$q$$$ using monotonic stacks, which are well explained by the USACO Guide or CPH. With prefix sums, we can find the the factor by which $$$A_i$$$ increases, and monotonic stacks have a linear time complexity, so the solution is done in $$$O(n)$$$.

    I don't see the intuition for Knapsack DP as the problem isn't looking for a subset, but maybe you're on to something.

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

      All of the elements that precede the nearest element with a greater q _ will not change_

      I don't think comparing only q values work, since a later element with lower q could make better use of previously allocated operations to higher q values if enough operations are performed on it that take place after the previous greater q. Case in point sample test case 2, at prefix 5 — where using operations(1) previously performed on 9 are better performed on 7 since 7's max operations can be 3 with the one from the 9, making its value(9 + 7*(2^3)) be higher than 9*2 + 7*(2^2).

      EDIT : Got an AC by applying a logarithm based check for shifting operations.

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

C is the hardest div2C in history. Even harder than D2 from the last div2 round (rated 2187 on CList).

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

Yo, i've just completed tutorial for problem D in Hackmd : link

Sorry for not directly publishing tutorial in codeforces, i prefer using hackmd :)

First time getting upvoted, thanks a lot !

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

C odd case: since n%2==1 then l will always be 1 so it is easier to just set the last 4 to 1 3 n-1,n

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

Can someone share their thought process of D like how and why it works

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

    you have to collect all the 2's in terms of powers and give them to the best successor possible, then calculate the sum of the remaining odds which are not considered

    Ex1:1 6 5
    for this, you have to give all the 2 powers to 5 which is better over 3

    Ex2:4 1 3
    same as above but the format is different, first you will accumulate at 1 then accumulate at 3

    Ex3:4 7 2 2 2
    Here for the first 2 elements, 4 will be accumulated into 7
    For 3 elements, since there is no benefit of sending 4 to 2 over 7, we leave it unchanged
    For 4 elements, first, we will accumulate the previous 2 and then compare 4 with 7, since 7 is bigger, the powers remain unchanged
    For 5 elements, we will accumulate past 2 twos to get 8, which is in turn greater than 7, so we will send the 4 to 8 making it 32

    thus we will be storing all the accumulated indices and powers in a stack, all the odds remaining after removing powers of 2 in other variables unaccumulated_odds, whenever we get a new element we try to pop elements in the stack, and try to accumulate values at new element same as shown in example 3

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

    think like this . from a given i to previous 31 even numbers you will have a number with or without shifting 2's greater than 1e9 . since every ai is bounded by 1e9 . all other even numbers before that will lose thier 2's to largest element

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

Can E be solved by simulated annealing?I submitted serveral times, but no luck.288417811.This is the first time I wrote simulated annealing, so it may come with some mistakes. So I wonder whether it can be solved by such a approach or not.

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

No way in hell I can ever "notice" what the editorial tells us to notice in problem D.

Solved it by thinking about where to utilize the 2s in between the last number we have given 2s to and the current number i. If we can use these 2s to make a[i] greater than the last number we gave all the 2s, then we should give all the twos we gave the previous number to this a[i]. Else we re-use the answer from the last number we gave all the twos and give all the twos in between to a[i]. To maintain the last number we gave all the 2s we can use monotonic stack. Submission: https://mirror.codeforces.com/contest/2035/submission/288418798

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

you can solve E using ternary search too.

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

for C, we can also approach it greedily, observe if n is odd then last operation is and so we can get to less than or equal to last operation, so we choose last to be n and second last to be n-1 as (n &(n-1)) = (n-1), but what if we try to make the first bit of (n-1) on? then we can make it (n), but how to do it? observe if we place 3, 1 before it we would get the first bit ON which would be carried forward to get the maximum value as n. Also in the case of n if it is not a power of two, n would have a bit which isn't present in the sequence so we can greedily place it at the end, and have a permutation such as 1, 2, 3..., n. Rest is nicely explained in the editorial, thanks for it!

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

Am I the only one who found B to be harder than C and D? (and D easier than C)

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

One more way to solve F is (same observation(and logic) but different implementation) :

Do a normal binary search on answer , but this time instead of checking for only mid , check from mid-2*n+1 , mid , even if one turns out to be true(say for num) , hi = num-1 else lo = mid+1;

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

can someone help me in proving my solution for C? I brute forced some cases of $$$ 2, 1, 3, 4, .... , n $$$ and noted that we get the required $$$ k $$$ somewhere always. based on this, my solution is just $$$ x + 1, x + 2, x + 3, ......, n, 2, 1, 3, ....., x $$$ for some x such that the resultant $$$ k $$$ for $$$ 2, 1, 3, ... x $$$ is maximum and $$$ n - x $$$ is even. I cannot formally prove why the sequence $$$ 2, 1, 3, 4, ... x $$$ gives the best k always.

Submission

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

I just don't know why $$$O(n^2\log{a_i})$$$ does not fit in 4 seconds in problem F.

UPD: It seems to be my problem. I was submitting in G++ 17 instead of G++ 20, which is based on 32-bit architecture.

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

    We knew the TL was tight for some people but were also aware of a very fast N^3 solution and wanted to cut it.

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

      In such cases, it might be better to modify the constraints a bit.

      For example, you could use 0 <= a_i <= 10^5 instead of 0 <= a_i <= 10^9.

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

        I guess that would have made sense. However, there were a lot of optimizations that where any of them could make it pass (although unoptimized passed comfortably for many people).

        • Adjust the bounds of binary search to look for only a better answer
        • Skip half of the binary searches by only checking the correct parity of the answer
        • Unrolling the dfs order and iterating instead of doing recursion
        • Optimizing to $$$\mathcal{O}(n^2 \log n + n \log a)$$$
»
18 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I am confused, in problem D , we can have $$$a_i$$$ > $$$a_j$$$ but it is more optimal to divide $$$a_i$$$ by 2 and multiply $$$a_j$$$ by 2

example: 20 and 9

20 + 9 = 29

but we can do: 20 / 2 / 2 = 5

and 9 × 2 × 2 = 36

and the sum becomes 41 > 29

what am I missing here ?

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

However, E can be solve in $$$O(z^{3/4})$$$ , only by using some optimizations in brute force. My Code

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

    Please explain the logic you used for optimization

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

      Every time increase $$$d$$$ by $$$k$$$ and use a second operation, it will be end in at most $$$O(\sqrt{\dfrac zk})$$$ times.

      If you choose to increase $$$d$$$ by $$$i$$$ and always choose the second operation from then on, the additional cost is $$$ix+\lceil\dfrac z{d+i}\rceil y$$$ . You can find that there are at most $$$O(\sqrt z)$$$ different $$$\lceil\dfrac z{d+i}\rceil$$$ and always use the smallest $$$i$$$ with the same $$$\lceil\dfrac z{d+i}\rceil$$$ will be better. So now is $$$O(\sqrt{\dfrac zk}\min(\sqrt z,k))$$$ .

      If $$$\sqrt z\le k$$$ , then $$$O(\sqrt{\dfrac zk}\cdot\sqrt z)=O(\dfrac z{\sqrt k})\le O(z^{\frac 34})$$$ ;

      If $$$\sqrt z \gt k$$$ ,then $$$O(\sqrt{\dfrac zk}\cdot k)=O(\sqrt{zk}) \lt O(z^{\frac 34})$$$ .

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

Hori

In the editorial of E problem, you have mentioned this statement.

a * b >= 2 * z , this implies that min(a,b) <= sqrt(2*z) ? Why is that ? How did we reach there ?

===> a * b >= 2 * z

===> min(a,b) * max(a,b) >= 2 * z

===> sqrt ( min (a,b) * max(a,b) ) >= 2 * z 


Just to replace few values, lets take a = 5 , b = 5, and z = 5, then a * b >= 2 * z holds true, but min(a,b) <= sqrt(2 * z) doesn't hold true.

Please can you elaborate a little here ?

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

    When z=5 and a=5, b=2 satisfies the inequality. Therefore, min(a, b) <= sqrt(2*z) is still satisfied. While there are definitely, other solutions, b=5 is one as you correctly point out, they are simply worse than the solution we just found as they cost more.

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

is system testing done till this round !!?

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

For solution E, I don't understand how we got $$$\dfrac{a\cdot b}{2} \ge z$$$

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

    You can think about it like this: since we know that $$$damage(a, b) \gt \frac{a \cdot b}{2}$$$, if we pick $$$a, b$$$ where $$$a \gt \sqrt{2z}$$$ and $$$b \gt \sqrt{2z}$$$, then $$$\frac{ab}{2} \gt z$$$, meaning that $$$damage(a, b)$$$ is surely greater than $$$z$$$. So our $$$a$$$ and $$$b$$$ were not optimal.

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

The value for $$$\text{has_}i$$$ in Problem F's editorial is wrong. Should be $$$\lfloor \frac{x}{n}\rfloor + [(x\mod n)\geq i] =:\text{has_}i$$$ instead.

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

Hey I came up with this solution but unsure of it's correctness and having difficulty in implementing it because of large numbers and comparing them while handling mod

dp[i] = max result possible for prefix i,

so transition would be whether i place all power of 2 possible in ith index and get sum of base odd number for all 0->i-1 indexes

or

take last index where we placed all 2's before that i.e. dp[last[i-1]] + placing all 2's in range last[i-1] +1 to i to ith element can then calculate answer and take whatever is maximum and set last[i] = i or last[i-1] accordingly. can anyone help me with its correctness and how to implement it.

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

In F, let's say the root is 3, now is doing operation on node 1, the parities of the nodes are 0 0 1 0 0

If I do the operations as the editorial "Have all nodes toggle their own parity, but have the root toggle the parity of the node that comes directly after it. Now, that node can just fix itself, and all nodes have the same parity."

After the operations, isn't it became 1 1 1 0 1? how come to make all parity the same?

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

How to think of problems such as C ??

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

Problem E we can solve it in O(k * sqrt(z / k)) if k <= 10000, else O(1e4 + sqrt(z / k))

If k <= 10000, the brute force takes k * sqrt(z / k)

Else, we try for 1 to 1e4 upgrades without attacks between them. Then we need to calculate the points where (z + w — 1) / w * y is optimized, and these points are (in worst case) 1e4. These points can be calculated while simulating jumps of length k, so the complexity of this part is 1e4 + sqrt(z / k)

It maybe has a better complexity if we reduce the threshold of k, but I'm too lazy to calculate it xd

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

Seems like E can be solved in O(sqrt(z)) time, here is the code for it: 366869087 As of writing this, it also has the best execution time