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

Автор NahC0el, 16 месяцев назад, По-английски

Hope you enjoyed the round!

2107A - LRC and VIP

Solution
Code

2107B - Apples in Boxes

Solution
Code

2107C - Maximum Subarray Sum

Solution
Code

2107D - Apple Tree Traversing

Solution
Code
Bonus 1
Bonus 2

2107E - Ain and Apple Tree

Solution
Code

2107F1 - Cycling (Easy Version)

Solution
Code

2107F2 - Cycling (Hard Version)

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

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

first.

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

Can some one help me what i did wrong in this submission, My Submission.

This is the submission for problem C, but I am getting out of bound error on string s, what have i missed?

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

In D, $$$\text{diam}(T \setminus (u,v)) \le 1/2 \text{diam}(T)$$$. This is because you are removing the center.

So if you recurse over subtrees after removing vertices, the complexity should be $$$O(n \log n)$$$.

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

Problem F1 can be solved greedily.

First, I reversed the array for simplicity.

Now, assume that the value in the first position is fixed. We can see that we can take this first element and swap it with the second one at a cost of $$$1$$$. Then, again, we can take the second element and swap it with the third one, also at a cost of $$$1$$$, and so on.

This is helpful because we can carry the smallest element seen so far and use it whenever we want, and the cost to use it is its value plus $$$1$$$.

So, at each position, we just have to choose whether to use its current value (which is a[i], and then update the current minimum value to a[i], so it can be used in the next iteration), or simply use the minimum value carried all the way to this point plus $$$1$$$.

To do this, we initially fixed the first element. But since $$$N \leq 5 \cdot 10^3$$$, we can simply iterate over all elements, assuming each one as the potential first element, solve the problem under that assumption, and then take the minimum among all of these.

Code: https://codeforces.me/contest/2107/submission/318519641

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

    Thanks Joaozao for sharing the greedy approach. I tried similar greedy approach, but wasn't able to think that we only need one optimal index to start with. After seeing the comment it strike to me. Thank you!

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

learning a lot from D Tks:>

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

troll F1

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

Problem D is cool. This is my D solution

And this is my construction for bonus2:

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

good contest

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

I believe the time complexity in B should be $$$O(n$$$ $$$\cdot$$$ $$$log(n))$$$.

Good and educational contest!

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

Thanks so much! What's the background theory about diameters you mention for problem D exactly?

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

(Problem D)Can someone help me understand why my submission is TLE? I thought it is O(n) or O(nLogn). Idea is to calculate the largest diameter(while also making sure we pick the lexicographically largest nodes if tie), detach all the branches and compute the diameters of those branches too(recursively)

CODE : https://codeforces.me/contest/2107/submission/318555860

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

Why did I even bother with DP on trees in D...

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

    How does your tree DP work for problem D?

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

      Submission link: https://codeforces.me/contest/2107/submission/318526303

      Let node 1 be the root of the tree. For every node we store the farthest child (if there is a tie, then the child with the maximum label) and the lexicographically greatest possible (d, u, v) in the subtree where the current node is the root. Then we begin traversing the tree from the root.

      If current node is a part of the path (u, v), then it's sufficient to remove every node from u to v and process remaining subtrees, otherwise we process the subtree that contains the path (u, v), update every (not removed yet) ancestor of the root of the processed subtree and repeat the described algorithm in the current node again.

      Apologies for the poor description, but this is probably the most horrible solution available for this problem and it's even more painful trying to explain it :)

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

Ok i get it

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

In the editorial of F1, I believe the transition should read $$$dp_{j + 1} + a_p \cdot (j - i + 1) + S$$$ instead of $$$dp_{j + 1} + a_p \cdot j + S$$$.

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

Thanks for nice problem set! Though scoring and order was IMO inadequate: F1 was much easier than D.

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

    I don't agree the idea for D is easy its just the implementation that's a bit hard. For problem F1 I am sure that many people coded the greedy while having no proof why it works.

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

    Also did you cheat on D? One of your submissions got skipped then immediately after that you submitted again. It looks like you changed variable names and submitted again but im not sure.

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

      IMO, F1 is easier to realize than D. And for sure it's much easier to implement. I mean O(n^2) DP solution for F1, didn't even think about greedy once saw a constraint which made O(n^2) valid. About cheating dunno what do you mean. How one can cheat via resubmit? I've submitted AC solution for D 2 minutes before deadline, had no time to read F1 during contest, spent 1:40+ time on D :) After contest took some drink, read F1 and solved in less than 0:40. Good old school O(n^2) DP, not common at all these days :(

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

        Yes but my point was if your submission got skipped that means that someone else wrote something very similar but it seems that you took that code that someone else had written down and changed couple things and resubmitted it

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

          Please learn how to use codeforces before accusing someone.

          You are correct that skipped might be because contest admins (like me) think its cheating. But there can be multiple other reasons: for example, if you accidentally take part in a contest you tested, we will skip your submissions. Or if you submit to the same problem multiple times after AC, your first submit will be skipped (there are non cheating reasons to do this)

          This is a case of the last type

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

          Probably that "someone else" was me. I've fixed what I though could be a bug and resubmitted a few seconds after previous submission. I saw AC for the previous submission, but at system testing it changed to "skipped".

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

in problem D. lets say the diameter of the tree is 19 from u to v and that would be (19,u,v). then there would exist a path of length 9 from x to y so (9,x,y). if i understand the solution correctly we pick the (19,u,v) first but isnt (9,x,y) lexicograpically the larger one? first stars with 1 and the second starts with 9.

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

No thinking, raw CHT solution for F2.(everything 0 indexed)

same as the editorial, we start with dp[i] for i in 0..n, and we would like the make transition of the form: for some i<=k<j , we use element L[k] to cover the range i..j. -1. If v=L[i], the cost of this is v*(j-i) + (j-1-k) + (j-1-i). Unlike in the editorial, we do not reduce the number of transitions to consider away from n^3, but just do it.

thinking about this in another way, we are paying an additional cost of (v+1) for every element before L[k] and an additional(v+2) for every element after L[k].

We can imagine this as a 2-stage DP, where each element k pulls from before and push to after. Each of these can be handled with an add-only CHT (aka line container). Li Chao tree is also applicable here and is faster, but the constraints are pretty lenient. We need two different CHT/ Li Chao Tree, one for each component of the transition.

Complexity is n log n. You can check my submission for detailed calculations. (Idea took me 5 minutes but getting the formula right took me the remaining 20….)

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

    Just adding a hopefully clearer write-up of this solution here.

    Observation 1: We can partition $$$[1, n]$$$ into several maximal non-intersecting ranges such that for each range $$$[l, r]$$$ we use only element $$$a_y$$$ (where $$$l - 1 \leq y \leq r$$$), and no two ranges use the same element.

    Proof

    Observation 2: The contribution of each such triple $$$[l, r, y]$$$ to the cost is $$$(r - y) + (a_y + 1) \cdot (r - l + 1) - 1$$$.

    Proof

    We will use dynamic programming to find the optimal partition for each prefix.

    Definitions:

    • $$$f(i, 0)$$$ is the minimum cost for partitioning the prefix $$$i$$$ such that no element from this prefix can be taken by some segment ahead (note that this only happens when we use element $$$a_i$$$ for the last segment in $$$[1, i]$$$).
    • $$$f(i, 1)$$$ is the minimum cost for partitioning the prefix $$$i$$$ such that only element $$$a_i$$$ from this prefix can be taken by some segment ahead.

    Transitions (evaluated in increasing order of $$$i$$$):

    • $$$f(i, 0) = \min_{j \lt i}(\min(f(j, 0), f(j, 1)) + (a_i + 1) \cdot (i - j) - 1)$$$
    • $$$f(i, 1) = \min_{j \lt i}(\min(f(j, 0) + 1, f(j, 1)) + (i - j) \cdot (a_j + 2) - 1)$$$

    Both transitions can be performed using two cht's.

    The correctness of the first transition is fairly obvious, but what about that of the second? Notice that if use $$$y \lt r$$$ for some segment $$$[l, r]$$$, and we've already handled the cost on $$$[r, y]$$$ (which is done here through the term $$$\min(f(j, 0) + 1, f(j, 1))$$$), then the cost on $$$(y, l]$$$ is simply $$$a_y + 2$$$ per element ($$$1$$$ for the initial swap $$$(y, r)$$$, $$$1$$$ for the swap $$$(i, i + 1)$$$, and $$$a_y$$$ for simply taking that element). The exception here is the last element, which only has a cost of $$$a_y + 1$$$ as it doesn't require a $$$(i, i + 1)$$$ swap. So the total cost for $$$(y, r]$$$ is $$$(r - y) \cdot (a_y + 2) - 1$$$.

    The answer for prefix $$$i$$$ is obviously $$$\min(f(i, 0), f(i, 1))$$$.

    Code: 342809058

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

For question D, I initially thought of violence, but I didn't dare to write it because I didn't understand the complexity of the proof of greed. How can I improve the ability of greed

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

In the editorial for D, I'm not sure how property 3 is coming into play here. The property holds when all $$$a_i$$$ are distinct, but in this case, when we remove a diameter, the tree is split into a forest, and many components in that forest might have the same diameter (it would be smaller for sure, but repetition is possible). So we can't say that all $$$a_i$$$ are distinct right?

Edit: Understood now, we are looking from the perspective of each node, a single node won't be involved in more than $$$O(\sqrt(n))$$$ operations

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

    Could explain how looking from the perspective of each node makes sure that there is O(sqrt(n)) subtrees?

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

      So in the end every single node will be disconnected right. Now for each node, let us focus only on operations which affected the component in which that node belongs. Since you're only focusing the operations performed in a particular component, the diameter will be strictly decreasing, hence as mentioned in the editorial, there can only be $$$O(\sqrt{n})$$$ operations that involve the connected component of that node, hence if we add this over all nodes, the total number of operations is $$$O(n \sqrt{n})$$$

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

Thank you for properly explaining time complexity in problem D, very helpful.

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

Problem D: Why is only the sum of diameters are considered to calculate the run-time? What about the run-time complexity of calculating the diameter itself for each component.. Where is the proof that calculating all the diameters for each component in the forest is also bounded by sqrt(n).

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

can anyone help me why i got wrong answer? 318519456

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

can someone please explain the DP solution for F1? Didn't quite understand from the editorial

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

    I used this approach:

    1. Let $$$d_i$$$ be equal to answer on prefix $$$[1,n]$$$. Base is $$$d_0 = 0$$$.

    2. Now, we can move from $$$i$$$ to another index $$$j$$$ $$$(j \lt i)$$$ by using $$$a_i$$$, we always jump using $$$a_i$$$, so we move from $$$i$$$ to $$$i-1$$$ paying $$$a_i$$$, then we swaps $$$(a_i, a_{i-1})$$$ and so on, until we arrive at position $$$j$$$. Formula will look as $$$(j-i)\cdot a_i + j - i$$$ or something like that. Just iteratate over all possible $$$j$$$ in $$$\mathcal{O}(n)$$$.

    3. And we can use another number $$$j$$$ $$$(j \lt i)$$$, swap $$$(a_i, a_j)$$$ and move from $$$i$$$ to $$$j$$$ using $$$a_j$$$ at each jump. Formula will look as $$$2(j-i)\cdot a_j + 2(j - i)$$$ or something like that. Just iteratate over all possible $$$j$$$ in $$$\mathcal{O}(n)$$$.

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

318653650 what's wrong with my submission,I use binarySearch find answer

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

I didn't understand the proof of the greedy algorithm in problem E from the editorial. Can someone explain it?

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

Hello guys! I am solving problem D and could not get it

https://codeforces.me/contest/2107/submission/318765311 That is my solution for problem D, can anyone please explain why it is getting tl7?)

That is my solution — https://codeforces.me/contest/2107/submission/318799790

Upd: i did it)

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

I just want say the D is amazing!

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

for D problem, is the tutorial prove that the simple solution does not actually take O(n^2) but O(n.sqrt(n)) ? because I don't see the difference between the simple solution and the real solution

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

thanks to the authors for amazing tasks. Especially D and F1-F2 are amazing. The best round in 2025

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

Can someone explain how doing the two dfses is sufficient here? like what if we have 5 to 7 as a diameter and 6 to 8 as a diameter(and some others by the consequences of diameter) and the first dfs finds both 7 and 6 and chooses 7, if 7 to 8 is not a diameter, we will skip eight even though we should have picked it. What am I missing?

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

For problem D, the solution says that there is a $$$O(n^2)$$$ solution. That does mean one that is fast enough right? Because I though that $$$n \le 10^5$$$ was too big for that. Also, I believe this submission 318924590 is $$$O(n^2)$$$. Is $$$O(n^2)$$$ in fact just too slow or is my implementation not $$$O(n^2)$$$ or too inefficient?

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

In F,there is one wap at most,so you can count $$$(i,j)$$$(when going to $$$i$$$ out of $$$[i,n]$$$)and swap them and get a $$$O(n\log n)$$$ solution.

Here's submission

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

Yeah.I've solved hard F without tree,with only ST table. Link

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

O(nlogn) solution for D: (implementation details heavily rely on sets with a custom comparison struct)

let 1 be the root of the tree then for every path, there will be 1 node in it with the least depth (distance to 1)

we first traverse the entire graph by a dfs, the lexi-greatest path with node x as its root node (node with least depth), will be its 2 lexi-greatest branches, connected with itself.

  1. we can obtain the lexi-greatest path of all nodes with a single dfs from 1 (O(n)) I also stored a childs set in each node, in lexi-order (O(nlogn) for all nodes)

  2. insert all nodes in a set, that compares the lexi-value of the longest path of each node (O(nlogn))

  3. then, remove the largest valued node in the set (this could be first or last depending on your ordering) remove all of the child nodes on this path) (O(klogn), k being the nodes removed)

  4. notice, after removing these nodes, only the parents of the largest valued node would be impacted after the removal of these nodes, for nodes on other branches, the highest lexi-value path with them as the root node stays the same

and there are a maximum of k parent nodes, otherwise their would be a longer path than the removed one, so modifying these nodes also take O(klogn) time

  1. return to step 3, until all nodes are removed since the sum of k's is n, these last steps take a total of O(nlogn) time

here is my submission, my implementation skills on graph problems are **** but it shows the method works319444417

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

Thanks to this contest, I learned that apparently my dynamic convex hull trick implementation is wrong!

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

Good contest

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

could anyone tell me why my submission for D gets TLE on test 5, i do the same thing as mentioned in editorial. link to the submission

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

For the B problem, consider the case 2 boxes, k=1 and a1,a2=6,6. Tom plays first ->5,6. Jerry doesn't have any other option but to equalise or will lose due to the max-min<=k constraint-> 5,5. This continues and tom will end up winning. This is irrespective of summation of 'a' being even or odd. In this case summation is even and still tom wins. Please help where did i go wrong.

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

In div2C, isn't it enough to limit INF to k=10^12? I'm not sure why we need to keep a_i terms in sum, as largest consecutive block will be at most 10^12 and summed with -INF, it will cancel out.

One simple way is to have INF=10^13 , as that is sufficient for our purposes (prevents overflow, but still larger than $$$k+\sum{a_i}$$$).