NahC0el's blog

By NahC0el, 13 months ago, In English

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
  • Vote: I like it
  • +157
  • Vote: I do not like it

| Write comment?
»
13 months ago, hide # |
 
Vote: I like it -130 Vote: I do not like it

first.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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?

  • »
    »
    13 months ago, hide # ^ |
    Rev. 3  
    Vote: I like it +11 Vote: I do not like it

    Possible int overflow with k cuz k ≤ 1e12. The error probably started there and ruined the rest.

    • »
      »
      »
      13 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it +3 Vote: I do not like it

      Thanks a lot, was stuck at this for a while. I was focusing on string overflow because error showed out of bound on string.

      • »
        »
        »
        »
        13 months ago, hide # ^ |
         
        Vote: I like it +3 Vote: I do not like it

        got same error wasted 25 mins in contest. and 400 points :(

»
13 months ago, hide # |
Rev. 3  
Vote: I like it +13 Vote: I do not like it

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)$$$.

»
13 months ago, hide # |
Rev. 3  
Vote: I like it +16 Vote: I do not like it

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://mirror.codeforces.com/contest/2107/submission/318519641

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    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!

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

learning a lot from D Tks:>

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

troll F1

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +40 Vote: I do not like it

Problem D is cool. This is my D solution

And this is my construction for bonus2:

»
13 months ago, hide # |
 
Vote: I like it +9 Vote: I do not like it

good contest

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

Good and educational contest!

  • »
    »
    13 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it +9 Vote: I do not like it

    The idea is $$$O(n)$$$, the given code runs in $$$O(n \log n)$$$ due to the call to sort, but that's not really necessary because you only need the maximum which you can find in $$$O(n)$$$ time.

»
13 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

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

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +6 Vote: I do not like it

    I find this blog very useful.

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Thanks, this is very helpful! However, I meant something more along the lines of a general math theorem that describes how tree diameters behave. Is there some graph theory theorem for this? That's what their "common central node" remark led me to believe.

»
13 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

(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://mirror.codeforces.com/contest/2107/submission/318555860

»
13 months ago, hide # |
Rev. 2  
Vote: I like it +4 Vote: I do not like it

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

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    How does your tree DP work for problem D?

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it +3 Vote: I do not like it

      Submission link: https://mirror.codeforces.com/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 :)

»
13 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Ok i get it

»
13 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

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$$$.

»
13 months ago, hide # |
 
Vote: I like it +12 Vote: I do not like it

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

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it -18 Vote: I do not like it

    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.

    • »
      »
      »
      13 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      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 :(

      • »
        »
        »
        »
        13 months ago, hide # ^ |
         
        Vote: I like it -18 Vote: I do not like it

        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

        • »
          »
          »
          »
          »
          13 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          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

        • »
          »
          »
          »
          »
          13 months ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          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".

»
13 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

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.

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it +6 Vote: I do not like it

    array {19} is lexicographically larger than {9}. This isnt a string but an array of numbers so definition is based on the number and not the digits.

»
13 months ago, hide # |
Rev. 4  
Vote: I like it +20 Vote: I do not like it

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….)

  • »
    »
    7 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    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

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
13 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    12 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      12 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      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})$$$

»
13 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

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

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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).

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You find diameters in $$$\mathcal{O}(n)$$$ by DFS run

    • »
      »
      »
      13 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it +3 Vote: I do not like it

      If we find diameters in O(n) for each component in the forest, how are we saying it is bounded by n sqrt(n)?

      • »
        »
        »
        »
        13 months ago, hide # ^ |
         
        Vote: I like it +9 Vote: I do not like it

        the point is that tree size reduces in each step, the proof in editorial explains that this reduction can't take more than sqrt N steps...

        so yes you do O(n) to find the diameter but only sqrt N times.

        Imagine like layers of merge sort... each layer altogether takes O(n) time but there are only logN layers.

»
13 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

can anyone help me why i got wrong answer? 318519456

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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)$$$.

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
13 months ago, hide # |
Rev. 8  
Vote: I like it 0 Vote: I do not like it

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

https://mirror.codeforces.com/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://mirror.codeforces.com/contest/2107/submission/318799790

Upd: i did it)

»
13 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

I just want say the D is amazing!

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
13 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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?

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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?

»
12 months ago, hide # |
Rev. 2  
Vote: I like it +8 Vote: I do not like it

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

»
12 months ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

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

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
12 months ago, hide # |
Rev. 3  
Vote: I like it 0 Vote: I do not like it

Good contest

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Wasnt B too easy for a div 2 contest ??

»
12 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    11 months ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    nvm, i thought who finishes the apples first in one box wins:'|

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      What if we have the max element more than one time, then we should compare with

      max_element-min_element > k

      Instead of max_element-min_element-1 > k

      For e.g

      4 2

      7 7 4 5

      Here Tom loses, but still winner according to solution?

      Am I missing something?

      • »
        »
        »
        »
        8 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        In your example, after the first move (Tom chosing to reduce first occurence of 7 to 6) the array becomes a = [ 6, 7, 4, 5 ] after the move, d = max(a) = min(a) = 3, d > k follows --> Tom loses! According to the solution as well, Tom does lose.

»
5 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

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}$$$).