maomao90's blog

By maomao90, 2 years ago, In English

1919A - Wallet Exchange

Author: maomao90

Hint 1
Solution
Code

1919B - Plus-Minus Split

Author: maomao90

Hint 1
Solution
Code

1919C - Grouping Increases

Author: maomao90

Hint 1
Solution 1
Hint 1
Hint 2
Hint 3
Solution 2
Code (Solution 1)
Code (Solution 2)
Bonus

1919D - 01 Tree

Author: maomao90

Hint 1
Hint 2
Solution
Code

1919E - Counting Prefixes

Author: maomao90

Hint 1
Hint 2
Solution
Code
Bonus

1919F1 - Wine Factory (Easy Version)

Author: maomao90

Hint 1
Solution 1
Solution 2
Code (Solution 1)

1919F2 - Wine Factory (Hard Version)

Author: maomao90

Hint 1
Hint 2
Hint 3
Solution
Code

1919G - Tree LGM

Author: maomao90

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution
Code

1919H - Tree Diameter

Author: maomao90
Full solution: dario2994

Background
Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Solution
Code
Tutorial of Hello 2024
  • Vote: I like it
  • +760
  • Vote: I do not like it

| Write comment?
»
2 years ago, hide # |
 
Vote: I like it +19 Vote: I do not like it

Thanks for fast editorial

»
2 years ago, hide # |
 
Vote: I like it +45 Vote: I do not like it

C was tough

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

    Yeah definitely, I think it should be around 1700 rated. Actually, I thought to find the longest non-increasing subsequence and remove it from the array. Then I have to calculate the cost of the remaining portion of the array. That should be the answer. But I could not implement it.

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      people are saying that they tried this and it gives WA too!

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

        Yes i implemented that solution , got WA on pretest 2

      • »
        »
        »
        »
        2 years ago, hide # ^ |
         
        Vote: I like it +11 Vote: I do not like it
        LIS-approach counter test
        • »
          »
          »
          »
          »
          2 years ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          What if we first list those elements which are like a[i]<a[i+1] and then find longest non-increasing subsequence and remove them from array a to another array b .
          Take example of your test case : Element which are like a[i]<a[i+1] are 2 3 3 9 1 10 3 6 now longest non-increasing subsequence is 3 3 3 or 3 3 1 (Note that first two 3's are reffering to same element in array) , remove either of them from array a we will get( if we remove former) :
          3 3 and 2 2 9 8 3 1 10 6 We get answer 2 which is right.
          Please provide some counterexample if its wrong, I checked it for many test cases and get correct answers, sadly I couldn't implement it in contest :(

          • »
            »
            »
            »
            »
            »
            2 years ago, hide # ^ |
             
            Vote: I like it 0 Vote: I do not like it
            Counter
        • »
          »
          »
          »
          »
          2 years ago, hide # ^ |
           
          Vote: I like it 0 Vote: I do not like it

          Do you know the link of the Problem C with K subsequences? Can you give me it?

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

        same i too get wa

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it +1 Vote: I do not like it

      agree :))

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

      IMO, C was somewhat tricky to implement but the concept isn't that hard. I think D was pretty tricky, conceptually.

      • »
        »
        »
        »
        2 years ago, hide # ^ |
         
        Vote: I like it +1 Vote: I do not like it

        Can you explain me the concept of C?

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

          You just greedy it. Maintain the two sets and keep on adding them in order. You do have to be careful with how to handle each case though.

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

      Longest non-increasing subsequence can fail sometimes, example 4 3 2 1 8 4 2 1.

      If your code detects 4 3 2 2 1, penalty is 1, but optimal answer is 0 (4 3 2 1 and 8 4 2 1).

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

        this case is invalid

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

        Okay so I randomly thought of a testcase: 8 9 10 2 11 7 4 3

        The answer for this one is 2 if we split this into 8 2 and 9 10 11 7 4 3

        however when I ran the codes submitted by some of the top coders for problem C

        I got different outputs from them. Some gave 1, some gave 2 and some 3.

        I tried to run Tourist's code and his code's answer was 3.

        Now this is really confusing as hell for a newbie like me, was the problem faulty or am I tripping?

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

        YESSS

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

      I firstly implemented this way using longest subsequence, but it was WA. Than I implemented greedy that is accepted.

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

        Can you pls share your approach?

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

          Basically: any item should either be in s or t It doesn't matter which one so have the last item inserted in each one in 2 vars: t,s if (s < t) swap(s,t) so t is the minimum(doesn't matter just wanna have the minimum)

          you agree that the most optimal approach is that the current element of the array goes to the one with the minimum, if it's less than min it's the best, if it was larger than min but smaller than the other one you put it into that, still free since it's smaller

          otherwise put it inside the any of those u like(doesn't matter since you'll swap them if wasn't good) in any of them the cost will be increased by one

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

      I thought of that too and implemented it WA 2 but for anykind of tc like this: 1 2 3 4 5 6 7 8 9 where the ans is n-1, it'd output n

      but just going on the paper for 5secs and it clicked instantly got the exact approach of editorial

      it was nice

»
2 years ago, hide # |
Rev. 2  
Vote: I like it +1 Vote: I do not like it

Very good competition!

2024 will be a good year, it seems to me because the competition was cool!

Thanks maomao90 for the competition.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

FastEditorialForces!

»
2 years ago, hide # |
 
Vote: I like it -6 Vote: I do not like it

russian???

»
2 years ago, hide # |
 
Vote: I like it +35 Vote: I do not like it

F1 statement: "There are n wizard's..."

Me: "Well.... okay, there are n Jesuses..."

P.s. really cool problems and thank you for super fast editorial!

»
2 years ago, hide # |
 
Vote: I like it +19 Vote: I do not like it

proofByAcForces

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

I solved F1 with sqrt decomposition. Why no mention about it in the editorial?

»
2 years ago, hide # |
 
Vote: I like it -48 Vote: I do not like it

Do better. B, C have 6 pages long proof.

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Does anyone have any material on ReLU Segment Trees? I solved F1 (and I will try to do F2 as well) using a bit of a different segment tree than first solution and don't know if it is just the second solution (although I don't think it is). Thanks in advance

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

    My F1 solution uses a ReLU segment tree. I wasn't able to adjust it to solve F2 in time though. I thought the problem was really terrible at first because it's an ugly mathy solution, but after seeing that the intended solution was optimized flow instead, I like the problem much more.

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +16 Vote: I do not like it

    I updated the editorial tp have slightly more details about ReLU segment tree. Hope it helps!

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

    Can one share the intuition behind the observation about remaining water = max suffix sum?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Great contest & fast editorial.

I'm glad that 2024 has had such a perfect start. Thank you!

»
2 years ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

Thank you for the contest! Best wish for 2024.

»
2 years ago, hide # |
 
Vote: I like it +49 Vote: I do not like it

This contest is a perfect example of how to set and prepare rounds. Well done to the author and coordinator!

The tasks were pleasurable to solve, balancing math and algorithms. I thought that every problem was on point in terms of difficulty, quality, and their respective subjects (not every task was "constructive math"). Overall, the round seemed thoroughly tested and well-prepared.

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

I think this contest could've benefited from weaker samples on A and B. They're very proof-by-AC able.

Other than that, best contest I've ever had the joy of participating in.

»
2 years ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

I was not sure about C, so tried other approaches and when they all failed, then tried the above greedy approach and to my surprise it worked. Greedy is hard to prove!

»
2 years ago, hide # |
 
Vote: I like it +12 Vote: I do not like it

Proof for D?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone tell me what's wrong with my solution for F1?

240597499

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

so fast tutorial

»
2 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it
»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

F1 can also be solved by first consider D&C solution (maintain sum of A, sum of B, sum of answer for each node, when merge(L, R), try to match L.A with R.B as much as possible), then put this dp into segment tree.

»
2 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

greedy on D was quite unexpected

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C and D is really hard.

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

The editorial proof of the greedy algorithm correctness in Problem $$$C$$$ is obviously not complete. It is not clear why the optimal splitting for a prefix coincides with the restriction of the optimal splitting of the whole array to this prefix, while this claim is implicitly used.

Does anybody understand a complete proof?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it -11 Vote: I do not like it

    I'm not sure what you mean by that. Why is this proof not complete? My understanding is that because it's a subsequence, and every element must be inserted into either array b or c, it is a complete proof.

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

      The question is why if you have the optimal splitting of $$$[a_1, \dots, a_k]$$$ to subsequences $$$B$$$ and $$$C$$$, then the optimal splitting of $$$[a_1, \dots, a_k, a_{k+1}]$$$ can be obtained by back inserting of $$$a_{k+1}$$$ either into $$$B$$$ or into $$$C$$$. In general, the optimal splitting for $$$[a_1, \dots, a_k, a_{k+1}]$$$ may have nothing to do with $$$B$$$ and $$$C$$$.

      The proof is written such that it looks like this fact is used, but may be I am just misunderstanding something.

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

        I think the idea is that the split doesn't really matter. The only thing that matters is the final number in each array. So suppose that X and Y are the final two numbers in array A and B respectively, then regardless of any of the prior decisions for splitting the numbers, based solely on the values of X and Y we can determine whether a number should go into array A or B. You might argue that X and Y could have been chosen to be greater which might lead to a more optimal result, but the algorithm maximizes the value of X and Y in the first two cases, and in the third case the editorial lays out an argument for why it is at least just as good putting it in the other array.

        Does this sound right?

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

    If you do let me know, the same is the reason I wasn't able to give this greedy approach a try

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

      I believe, the following was implied.

      Assume that you have any splitting of $$$[a_1, \dots, a_k]$$$ to subsequences $$$(B, C)$$$, which can be extended to a splitting $$$(B', C')$$$ of the whole array $$$[a_1, \dots, a_n]$$$ with penalty $$$x$$$. Then the splitting $$$(\tilde{B}, \tilde{C})$$$ of $$$[a_1, \dots, a_k, a_{k+1}]$$$ constructed greedily from $$$(B, C)$$$ also can be extended to some other splitting $$$(\tilde{B}', \tilde{C}')$$$ of the whole array $$$[a_1, \dots, a_n]$$$ with the same penalty $$$x$$$ or less.

      This works, and in order to prove it you need to construct these $$$(\tilde{B}', \tilde{C}')$$$ from $$$(\tilde{B}, \tilde{C})$$$, which is more or less done in the editorial.

  • »
    »
    2 years ago, hide # ^ |
    Rev. 4  
    Vote: I like it +5 Vote: I do not like it

    I agree with you. The proof is not a standard greedy proof. It only says: when the splitting of the first k elements is fixed, we can obtain the optimal (optimal under this condition, not globally optimal) solution of the splitting of the first k+1 elements. Edit: Actually there is a mistake, when the first k elements are fixed, then out of all optimal splittings of the remaining n-k elements, it's always not worse to choose the splitting (in the editorial way) of the (k+1)th element, so it's always chosen that way.

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

      Can you please elaborate on why optimal choices at each particular step eventually lead to optimal partitioning globally? Still don't understand :(

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

good contest

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hey can anyone share their intuition for problem D , i didn't understand the idea from editorial , and would appreciate if anyone can share their stack based idea

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

    For any element of the array, we need to make sure it can be absorbed into another element. This can only be done if eventually it can become adjacent to an element with value exactly one smaller than it.

    How can we check this? Like in the solution, we can take the larger element and trivially check if that's the case (it can either have a minus one element directly adjacent to it or have elements with similar value on its left and right that eventually reach a minus one element). This way, all max elements are deleted. Sane thing can be done for batch of the next bigger elements, etc until we are done. Make sure than only a single 0 element is permitted initially.

    Now, this process can be automated. By the above, it is enough to check that all the elements are eventually adjacent. To assure this, we need to make sure that, for every element, for the closest element to either the left or right that has a smaller value of our element, its value needs to be minus one and not less.

    This works, because we make sure this condition is true for every element. As on the above analysis, we can use it to get rid of elements of progressively smaller value and all the bigger elements will be deleted. Then, this element will be ready to be absorbed by this chosen smaller target. Ofc, if this target has a smaller value, the absorption will fail and this element will stuck there forever, so no tree can be found in this case. You can work out some examples and make sure why a sequence of elements with same value doesn't impede this solution.

    Here is where the stack idea comes. We can use stacks to solve this problem, like outlined here: https://www.geeksforgeeks.org/find-the-nearest-smaller-numbers-on-left-side-in-an-array. Do that for both directions and make sure that the element found on the stack is exactly one smaller.

    Also, there should be exactly 1 zero in the array. Check that as well and you have the solution.

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

    Consider "growing" the tree node by node. Consider the list of current leaves.

    In one move, we can replace the subsegment of a single element [x] with [x, x+1] or [x+1, x]

    In other words, choose any element x in the array, and insert to the left or right of it x+1.

    Problem is now to find if the final array of leaves can be generated via these moves.

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

thank you , but I am sb

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Again ,C was too much for me

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For problem c I tried to separate longest decreasing subsequence (indices) from all elements, then I tried to compute a[i]<a[i+1] for rest of the elements but couldn't pass. Can anyone explain why?

»
2 years ago, hide # |
Rev. 2  
Vote: I like it +42 Vote: I do not like it

I had a very different solution to D, I think.

Note that there must be exactly one leaf node with value $$$0$$$. Call this node $$$i$$$ and consider the path from the root to $$$i$$$. Then, for each node $$$v$$$ along this path, let $$$S_v$$$ be its subtree on the side not including $$$i$$$ (the other edge). Then, if we subtract $$$1$$$ from the distance for each node $$$j\in S_v$$$, this must also form a valid dfs ordering of distances.

This gives the following recursive definition: a height-$$$h$$$ tree has DFS ordering $$$[L, h, R]$$$ where $$$L$$$, $$$R$$$ are concatenations of height-$$$h + 1$$$ trees' DFS orderings.

For example, consider the first sample $$$[2, 1, 0, 1, 1]$$$. Then, $$$L=[2, 1]$$$ must be a concatenation of height $$$1$$$ trees and $$$R=[1, 1]$$$ must also be a concatenation of height $$$1$$$ trees.

Checking if $$$[2, 1]$$$ is a concatenation of height $$$1$$$ trees is the same as checking if $$$[1, 0]$$$ is a concatenation of height $$$0$$$ trees. Recursing gives that this is indeed valid.

Similarly, $$$[1, 1]$$$ being a concatenation of height $$$1$$$ trees is the same as checking if $$$[0, 0]$$$ is a concatenation of height $$$0$$$ trees (which it obviously is).

So, we can use Divide & Conquer to check if an array corresponds to a concatenation of height $$$h+1$$$ trees, by (for example) using a RMQ to find all positions with value $$$h + 1$$$ and recursing on their subranges. We also check that the root only has one $$$0$$$.

Overall, the code looks like this:

def dnc(i, j, h):
   if min entry in [i, j) != h, then not a valid height-h tree
   if j - i == 1, return true
   get positions pos in [i, j) at height h
   split [i, j) into ranges [lo, hi) corresponding to parts between height h values # concatenation is basically unique
   for each [lo, hi), check dnc(lo, hi, h+1) and return true if all succeed 

Since we look at each position as a root exactly once (utilizing the RMQ for this speedup, or some sort of binary search structure), the runtime is $$$O(n\log n)$$$ (coming from building the RMQ).

My code: 240589892

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The sample of D was too weak..

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Finally, decent contest! Could've given more examples in D though:(

»
2 years ago, hide # |
 
Vote: I like it +55 Vote: I do not like it

For F2 I feel you can just imagine a substring of towers as some mechanism that:

  1. produces ans wine for free.

  2. produces water water at the end.

  3. accepts at most magic water to turn into wine at the front.

  4. additionally accepts cap water at the front and routes it to the end.

Then it is just one segtree and no need for flow...

My sol: 240574390

»
2 years ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

Why is the TL of E 1 second?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Easy A,B,C problems.

»
2 years ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

2024 gave me:

Spoiler
»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Beautiful contest Enjoyed it

Thanks to the creators, coordinators and testers for such a great contest

thanks for the fast editorial too

»
2 years ago, hide # |
 
Vote: I like it +16 Vote: I do not like it

I had a square root decomposition solution for F1.
Submission.

I made blocks which store three values.

struct node {
    int ans = 0;
    int need = 0;
    int give = 0;
};

ans is the value of how much wine we get just from this block.
need is the value of how much extra water we need from the previous block to utilise the full capacity of wizards.
give is the value of how much extra water we have left (which we can give to the next block)

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

The "NO" tests on G were pretty weak, looks like two of the three submissions in contest didn't check enough conditions. (It's pretty easy to fix, you can just write the DP to compute the matrix corresponding to your tree and make sure it's equal to the input.)

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

thanks for the fast editorial

»
2 years ago, hide # |
 
Vote: I like it +18 Vote: I do not like it

Why i was solving D like this the edge is 0 or 1, not one of the edge is 0 and the other is 1 :⁠'⁠(

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Incase someone wants an easy implementation for C-https://mirror.codeforces.com/contest/1919/submission/240529072 The idea is to maintain the endpoints of two sets with minimum answer and maximum endpoints. If current element is A(i) and endpoints are a,b. If b>=A(i),then assign A(i) to b and no need to increase answer. Else if a>=A(i),then assign A(i) to a and no need to increase answer.(Note-a>=b) if A(i)>a and b,then swap the smaller endpoint(i.e. b) with A(i) and increase answer by 1. Easy to implement, though night be slightly tricky to prove correctness

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

    If b < A(i) <= a, Though assigning A(i) to b increases the penalty by 1, it will maintain the maximum endpoints and might lead to better answer in future right?

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

      At a later time , the maximum endpoint can at max decrease answer by 1 as as soon as we replace the max endpoint then it will no longer be used (it will be replaced). Hence we have to greedily decrease the answer wherever we can... Since b<A(i)<=a then replacing a by A(i) decreases the penalty by 1,which is also the maximum decrease that the endpoint a can contribute

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

Intuition in C:

We process from

Unable to parse markup [type=CF_MATHJAX]

to $$$n$$$ and maintain the final element of each subsequence. When processing $$$a_i$$$:
  • The subsequence we add $$$a_i$$$ to obviously ends with $$$a_i$$$.
  • The key is to maximize the ending of the subsequence we didn't add to.
    • But, we must prioritize minimizing the penalty first.
  • »
    »
    2 years ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    can you explain what does this mean ?

    This is because if we consider making the same choices for the remaining elements ai+1 to an in both scenarios, there will be at most one time where the former scenario will add one penalty more than the latter scenario as the former scenario has a smaller last element after inserting ai . After that happens, the back of the arrays in both scenarios will become the same and hence, the former case will never be less optimal.

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

      Think of this two cases.

      1) two sequences ends with 10 and 8.

      2) two sequences ends with 10 and 20.

      And you still have some elements to put into them.

      The optimal answers for both cases will differ by at most 1.

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

    I think people get confused why prioritize minimizing the penalty first will give the optimal result.

    I believe the reasoning is that, no matter which choice you made, the final elements will always contains $$$a_i$$$, so the two final elements will differ at most by one element. And in both cases, the future penalties can only differ by 1.

    So avoiding penalty now and take penalty later is always a better option.

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

(Problem C)I still don't understand why the optimal choice(in a greedy algorithm) at each step eventually leads to the optimal answer at the end

»
2 years ago, hide # |
 
Vote: I like it +13 Vote: I do not like it

rainboy has solved problem H. You can update the editorial.

240612222

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone share his O(n) approach for D??

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    This runs in O(n) for D. I maintain and update nxt[i] and bef[i] to store the next and previous undeleted indices for each element. There's no logn factor of time lost in searching for indices.

    240624316

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

      thank you!

    • »
      »
      »
      2 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it
      if(!help){
         if(a[nxt[x]]==i){
      	continue;
         }
      }
      

      can you explain this part? what does it checks?

      • »
        »
        »
        »
        2 years ago, hide # ^ |
         
        Vote: I like it +1 Vote: I do not like it

        So that part was honestly a very inefficient way of checking that the condition for i is satisfied. The bool help is "True" if the previous instance of i had an instance of i-1 immediately before it.

        The lines you mentioned say that if the previous instance of 'i' did not have an i-1 before it, then there must be an 'i' to follow the current value(if not an i-1), as otherwise there's no way this block of i's could have been generated.

        A much cleaner way to do this would have just been to iterate one element at a time and kept updating the nxt[x] and bef[x] values.

»
2 years ago, hide # |
 
Vote: I like it +34 Vote: I do not like it

What is ReLU segment tree? There seems to be no resources about it in English side of internet... (I would appreciate resources in any language though)

»
2 years ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

Can someone please explain why the greedy approach works for PROBLEM C?

I did not get the third case where x < ai < y. what is the proof that adding to y to avoid +1 penalty is always optimal?

Part where I did not understand:

This is because if we consider making the same choices for the remaining elements ai+1 to an in both scenarios, there will be at most one time where the former scenario will add one penalty more than the latter scenario as the former scenario has a smaller last element after inserting ai . After that happens, the back of the arrays in both scenarios will become the same and hence, the former case will never be less optimal.

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

    Say you take the penalty now, and you have to take at least X more penalty in the future.

    Then if you avoid the penalty now, then you will have to take at most X + 1 penalty in the future.

    So there is no benefits to take the penalty now.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I had the same solution for D, but AC is quite tough in Python

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Editorial code for D says YES to this test case: N=9, A=[2,1,1,1,1,1,1,1,0]. It seems incorrect to me?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

My solution for E. I think it works in $$$O(n)$$$ time.

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Proof for $$$D$$$:

First note that any most-distant leaf will have a parent edge weight of $$$1$$$, because otherwise there would exist a more distant node under the other child (edge-1 child) of the parent. Now we need to prove that for any $$$2$$$ adjacent array values, one of them is a maximum and the other is less by $$$1$$$, if there is a binary tree solution for that array (but it does not have such $$$2$$$ leaves sharing the same parent), we can still convert it to another binary tree conforming to the same array with the $$$2$$$ leaves sharing the same parent:

Using the same previous principles, the maximum leaf will be the edge-1 child. We can always cut its parent edge-1, remove the other edge-0 and merge the $$$2$$$ nodes connected by it, then go to the other leaf adjacent in the array (with distance less by $$$1$$$), add $$$2$$$ edges under that node, one of them will be the moved the maximum node (under edge-1), and the other will be the less-by-1 node.

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Great problems D and F1! Finally became International Master. E is also excellent; it's a pity that I struggle with counting.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone explain why the answer for F1 is the maximum suffix sum ? I cant figure it out :<

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

    Here's my understanding. Let's use this case I just came up with.

    a = [3, 3, 7, 5, 6, 4]
    b = [2, 2, 10, 3, 10, 3]
    
    pA = [3, 6, 13, 18, 24, 28] (prefix sums of A)
    pB = [2, 4, 14, 17, 27, 30] (prefix sums of B)
    

    Let's say that all $$$pB[i] \lt = pA[i]$$$ for some prefix. Then the answer would just be the last $$$pB$$$ of the prefix. For example let's take the the first two items in the array. Since $$$pB[0] \lt = pA[0]$$$ the wizard can take as much water as he wants from the first tower and since $$$pB[1] \lt = pA[1]$$$ as well, the wizard can take as much water as he wants from the first two towers. So the answer for that prefix is $$$pB[1] = 4$$$.

    But now $$$pB[2] \gt pA[2]$$$. This means that the wizard is trying to take out more water than is possible. In effect, from positions $$$[0, 2]$$$ the wizard can only take out as much water as is in $$$pA[2]$$$. So let's decrease $$$B[2]$$$ by an amount so that $$$pB[2] == pA[2]$$$. That is, $$$pB[2] - pA[2] = 1$$$. Now the prefix sums would effectively become this:

    pA = [3, 6, 13, 18, 24, 28]
    pB = [2, 4, 13, 16, 26, 29]
    

    Now we repeat this same process, looking for the next time that the wizard tries to take out more than is possible, and reduce the $$$B$$$-value again. In this case, $$$pB[4] \gt pA[4]$$$ so we need to reduce $$$B[4]$$$ by $$$pB[4] - pA[4] = 2$$$:

    pA = [3, 6, 13, 18, 24, 28]
    pB = [2, 4, 13, 16, 24, 27]
    

    Now we find that the answer would be 27. But to simplify this process, we know that the effects compound in such a way that the final $$$pB$$$ value will be decreased by the highest $$$pB[i] - pA[i]$$$ before any modification. So we just need to find the global maximum of a segment tree formed by $$$pB[i] - pA[i]$$$ and subtract it from $$$pB[n-1]$$$ as long as it is positive.

    To check this, let's return to the original $$$pB$$$:

    pA      = [3, 6, 13, 18, 24, 28]
    pB      = [2, 4, 14, 17, 27, 30]
    pB — pA = [-1, -2, 1, 1, 3, 2]
    

    And $$$pB[n-1] - 3 = 27$$$ so it does indeed work! I hope this makes sense!

»
2 years ago, hide # |
Rev. 2  
Vote: I like it +22 Vote: I do not like it

I have some questions about problem F1

For the conclusion "the remaining amount of water remaining at tower $$$n$$$ is the maximum suffix sum of $$$v$$$", firstly here is my proof:

proof

Then here is my question:

Is there a more universal unstanding of the solution? or are there some ideas summarized from the problem? i don't come up with the solution because i alway think of those legal situation, and the suffix of $$$v$$$ may seems "contradictory" to the actual situation, which even didn't come to my mind.
However, i also have seen some problems with a solution like, "the answer is exactly the best one, so any illegal case cannot cover the answer, then just take all cases into consideration". i don't know whether there exist some underlying principles of these problems?
Sorry for my poor English, I have tried to express as clear as possible :)

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

how to promote my poor datastructure QwQ

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C — be like DP is bluff, greedy is approch

»
2 years ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

C was good. I initially thought that I had to find the longest increasing subsequence. I was expecting this type of question to be D

»
2 years ago, hide # |
 
Vote: I like it +37 Vote: I do not like it

Interesting alternative idea of D.

We can associate every (this binary 0-1 tree with n leafs) with (a simple not binary 1 tree with n vertices). Because we can decompress every (vertex with k children) to (a right hand bamboo with depth of k), where every its left child is a child of the original tree (with edge 1) and final right child -> the parent.

So, you need to check, whether there exists a usual tree with this DFS-order depth of vertices (where parent depth can be pasted between each to children or in the beginning/at the end).

There is my O(n) solution.

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    I believe the idea of your solution is equivalent to the editorial's but from a different perspective.

    Assuming the maximum distance in a binary tree is $$$max$$$, the equivalent N-vertices tree you are looking for is a tree where every consecutive run of leaves with distance $$$max$$$ are children of the leaf with distance $$$max-1$$$ which is adjacent to the run's left or right, then after excluding the leaves with distance $$$max$$$, every run of leaves with distance $$$max-1$$$ are children of the leaf with distance $$$max-2$$$ which is adjacent to the run's left or right, and so on.

»
2 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

In D, if I have $$$[\dots, x-1, x, x-1, \dots]$$$, I can't see why it's okay to choose either left or right.
Can someone pls explain?

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

    it doesn't matter which one do you choose, because after an operation array will be $$$[..., x - 1, x - 1, ...]$$$

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone explain the kind of segment tree that is used to solve F1 (Solution 1, Not ReLU segtree)

i.e. you need to compute range-max as well as allow range updates, any resources to study such segment trees?

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

    This is a segment tree which uses lazy propagation. It's a pretty common technique used in lots of problems involving range queries, you can even use it in conjunction with other techniques on trees and such (Although I'm not sure if it's worth spending time learning about a lazy segment tree below expert or even CM, if your aim is to gain rating).

    If you want to learn about it, there's lots of resources online. I personally used this site. Then I used CSES to practise a bunch, I just completed all the problems under 'Range Queries'.

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

For C, in the third case, it says if u always insert an element x in the one with larger last element, and you shift any other arrangement from this point such that x is in the one with larger last element (with the remaining choices unchanged), then it cannot be any worse. But is that true?

Say A, B upto a point was [8] and [12] respectively. And say x is 10 at that point (8<x<12, so we are in case 3), and one possible final arrangement is [8,10,9] and [12,11] (assume 9 and 11 are future elements). But if we shift 10 to the second array instead (which the algorithm would do), the new arrangement is [8,9] [12,10,11]. This would have an extra penalty overall, since [8,9] and [10,11] — 2 new penalties are being created, and only [8,10] — 1 penalty was destroyed (You can count trivially too, there was only 1 penalty before and 2 penalties in this one).

I'm not saying the algorithm is incorrect, I just have a problem with the way the proof is written.

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

    I encountered the exact same question and here's the solution I came up with.

    We're only interested in the last case, when there is an inequality $$$x \lt a[i] \le y$$$. We know that with the current arrays $$$A$$$ and $$$B$$$, we can complement them in such a way as to achieve the optimal solution. Let this optimal solution be achievable if we add $$$a[i]$$$ after $$$x$$$, not $$$y$$$. Let's demonstrate that we can obtain a solution no worse by adding $$$a[i]$$$ after $$$y$$$.

    For better understanding, the optimal solution looks like: $$$[\ldots,\ y,\ \ldots],\ [\ldots,\ x,\ a[i],\ \ldots]$$$.

    First, try adding all numbers from $$$a[i + 1]$$$ to $$$a[n]$$$ into the same arrays (as in the editorial). The penalty will differ only by the penalty between $$$y$$$ and the next number added after $$$y$$$ (if it exists) and by the penalty between $$$a[i]$$$ and the next number added after $$$a[i]$$$ (if it exists). Because all other numbers go exactly in the same order in the same arrays.

    Let's denote the number after $$$y$$$ as $$$next_y$$$ and the number after $$$a[i]$$$ as $$$next_{a[i]}$$$ in optimal solution. Thus, the penalty may differ from the optimal by no more than 1 and only in the case if $$$y \ge next_y$$$ and $$$a[i] \ge next_{a[i]}$$$, but $$$a[i] \lt next_y$$$ and $$$x \lt next_{a[i]}$$$. Because optimal solution has $$$1$$$ extra penalty from $$$x \lt a[i]$$$.

    For better understanding, the optimal solution looks like: $$$[\ldots,\ y,\ next_y,\ \ldots],\ [\ldots,\ x,\ a[i],\ next_{a[i]},\ \ldots]$$$, our solution looks like: $$$[\ldots,\ y,\ a[i],\ next_y,\ \ldots],\ [\ldots,\ x,\ next_{a[i]},\ \ldots]$$$.

    Such a situation can indeed occur: it's exactly what was described by flakes24. So if we try to use the method described above, our arrays will look like this: $$$[\ldots,\ y,\ a[i],\ next_y,\ \ldots],\ [\ldots,\ x,\ next_{a[i]},\ \ldots]$$$. Inequalities arise: $$$a[i] \lt next_y$$$ and $$$x \lt next_{a[i]}$$$.

    In this case, we'll swap the suffixes of the arrays $$$A$$$ and $$$B$$$, without changing the relative order of the numbers from $$$a[i + 1]$$$ to $$$a[n]$$$. Since $$$a[i] \ge next_{a[i]}$$$, we will achieve no more than $$$1$$$ penalty (maybe $$$x \lt next_y$$$) in our solution. Optimal solution has at least $$$1$$$ penalty ($$$x \lt a[i]$$$), so our solution is no worse.

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

For E: can someone explain why the method from the editorial works? Basically:

  • Why could each array with the sum $$$s$$$ and the given prefix sums be constructed this way?

UPD: it is true, that any such array could be constructed this way, because any such array could be converted into $$$[1, 1,1, ..., 1, -1, -1, ..., -1]$$$ by removing all the $$$(-1, 1)$$$.

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

maomao90 What was the original solution to H?

  • »
    »
    2 years ago, hide # ^ |
     
    Vote: I like it +10 Vote: I do not like it

    I used $$$n$$$ type 1 queries to find all the edges on the diameter, then I group edges based on which componenent they belong to if all the edges on the diameter are removed using $$$3n$$$ type 1 queries. Then, I solve for each component separately by first determining each vertex depth using $$$n$$$ type 2 queries, then finding the parent of each edge using $$$n$$$ type 1 queries. If I remember correctly, I eventually managed to optimise it to use a total of $$$3n$$$ type 1 queries, but I don't think there is any way for me to get to $$$2n$$$ or even $$$n$$$ type 1 queries as my solution consists of 3 steps.

    Code
»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

I doubt about the Wine Factory question. After each update, we can just calculate the sum of the water array(sum_Water) and the sum of the wizard array(sum_wizard). and if sum_wizard >= sum_water: the answer is sum_water, else answer is sum_wizard. I don't find any wrong in this approach. Am I missing something in understanding the question?

  • »
    »
    2 years ago, hide # ^ |
    Rev. 5  
    Vote: I like it +18 Vote: I do not like it
    • Water array: $$$a = [1, 100]$$$
    • Wizard array: $$$b = [100, 1]$$$

    According to your logic, the answer is $$$\min(1+100, 100+1) = 101$$$. In reality, the answer is $$$2$$$.

    Let's follow the statement carefully:

    $$$i = 1$$$:

    • Wizard $$$1$$$ removes at most $$$b_1 = 100$$$ liters of water from tower $$$1$$$ and converts it to wine. Tower $$$1$$$ has only $$$a_1 = 1$$$ liters of water, so only $$$1$$$ liter of water gets converted to wine. Now tower $$$1$$$ has $$$0$$$ liters of water left, and we have created $$$1$$$ liter of wine in total.
    • Tower $$$1$$$ has $$$0$$$ liters of water, so no water gets transferred to tower $$$2$$$.

    $$$i = 2$$$:

    • Wizard $$$2$$$ removes at most $$$b_2 = 1$$$ liters of water from tower $$$2$$$ and converts it to wine. Tower $$$2$$$ has $$$a_2 = 100$$$ liters of water (no water got transferred from tower $$$1$$$), so $$$1$$$ liter of water gets converted to wine. Now tower $$$2$$$ has $$$99$$$ liters of water left, and we have created $$$2$$$ liters of wine in total.
    • $$$i = n$$$, so no water gets moved forward.

    In total, we created $$$2$$$ liters of wine.

»
2 years ago, hide # |
Rev. 2  
Vote: I like it +5 Vote: I do not like it

On problem E, could anyone provide a proof on why every legal sequence with sum $$$s$$$ can be generated by adding consecutive pairs of $$$(-1,1)$$$ from the base $$$a=[1,1,...,-1,-1]$$$? I understand that all generated sequences are legal, and that legal sequences with sum $$$s$$$ must contain $$$a$$$ as a subsequence, but I can't seem to prove that all legal sequences with sum $$$s$$$ can be reduced to $$$a$$$ by removing consecutive pairs of $$$(-1,1)$$$.

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

O(n) solution for problem D soln

»
2 years ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

Is there any solution to the D that has something to do with split the distance sequence into two part?

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can someone plz explain how this part works? (From problem E's code)

"for (int i = 2; i < MAXN * 2; i++) { ifact[i] = MOD — MOD / i * ifact[MOD % i] % MOD; } for (int i = 2; i < MAXN * 2; i++) { ifact[i] = ifact[i — 1] * ifact[i] % MOD; } "

»
2 years ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

in 'D' when you delete the maximum there can be case when the maximum dont have a leaf sibling . it has a subtree further instead of a leaf, i.e case when 2,1,2.i am confused.

»
2 years ago, hide # |
Rev. 2  
Vote: I like it +5 Vote: I do not like it

I can't understand problem C's solution 2, there is a $$$dp_{i - 1, a_{i - 1}}$$$ in formula $$$dp_{i, a_{i - 1}} = \min(dp_{i - 1, a_{i - 1}} + [a_{i - 1} \lt a_i], \min_{1\le x\le n}(dp_{i - 1, x} + [x \lt a_i]))$$$

according to the solution, it means split the array to two subarrays where the last element of one subarray is $$$a_{i-1}$$$, and the last element of the second subarray is also $$$a_{i-1}$$$ what does this mean?

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

    Array $$$a$$$ can contain duplicate elements, so it is possible that the last element of one subarray has a value equal to $$$a_{i - 1}$$$, but not necessarily from index $$$i - 1$$$, and the last element of the other subarray is from index $$$i - 1$$$.

    Not having the first term will still result in AC because of the greedy algorithm, but if we do not want to prove any greedy having both terms is more "correct".

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For D, sure any two leaves sharing the same parent will be adjacent in the dfs order and their depths will differ by 1. But not every adjacent pair, even if one of them has the largest ai value can be siblings ryt? Like for a being 1,0,1 and for the tree you construct by clubbing first 2 leaves , the last two leaves in the dfs order won't be siblings even though they were adjacent in the original dfs order and one of them had the largest ai value. I understand that clubbing the last 2 leaves first in this example will construct a different tree and the answer returned by the algo will still be yes, however my point is that what if in some instance , we pick up some such pair for which those leaves can never be siblings for any tree satisfying the same dfs traversal, then the algo would return a NO instead of a possible yes. Maybe such faulty pairs can never exist but the editorial doesn't seem to consider or prove the non-existence of such pairs and frankly I was (during the contest) and still am stuck at this. Can someone help providing a proof? Thanks

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

    I’m also struggling to convince myself why that solution is correct. That’s why I really appreciate editorials which show proofs for their solutions (which isn’t the case for D unfortunately)…

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

    Edit: that was wrong unfortunately

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

    Having the same problem with the provided solution, if someone has a proof that would be nice :)

»
2 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is the problem H really 2000 rated? Or is it some bug?

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

Very helpful. Thank you for that..!