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

Автор Monogon, история, 4 года назад, По-английски

I hope everyone enjoyed the contest!

UPD: Added implementations for all problems.

1405A - Подделка перестановки

Author: antontrygubO_o

Tutorial

Implementation

1405B - Уничтожение массива

Author: hugopm

Tutorial

Implementation

1405C - Сбалансированная бинарная строка

Author: Kuroni

Tutorial

Implementation

1405D - Догонялки на дереве

Author: JettyOller

Tutorial

Implementation

1405E - Уничтожение фиксированных точек

Author: hugopm

Tutorial

Implementation

1404D - Игра Пар

Author: Ari

Tutorial

Implementation

1404E - Кирпичи

Author: Monogon

Tutorial

Implementation

Разбор задач Codeforces Round 668 (Div. 1)
Разбор задач Codeforces Round 668 (Div. 2)
  • Проголосовать: нравится
  • +421
  • Проголосовать: не нравится

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

Thanks for a good round and fast editorial!

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

Thanks for a good round and fast editorial!

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

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

    I had to google how during the contest and actual real-life trees kept popping up lol

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

      smh this guy didn't do the tree basics set, what a cyan...

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

        Can't do the tree basics set if it isn't open for virtualing yet :/

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

          The problems should all be publicly available, but how do I open it for VP?

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

            This should come up in the "Edit" tab of the contest (I think)

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

              Here's what I see:

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

                On the same page, there's also this, which should do the trick

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

                  What do you mean by "on the same page"? I literally just posted what the page looks like for me and that isn't there.

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

                  Doesn't it already say that, in case the visibility isn't private, the following will be set to defaults, where "virtual allowed: yes" is in the list

                  UPD: Btw, I am able to click on "virtual participation" under the contest, and it asks me to register. So, I'd say it's some issue on ScarletS's side.

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

        I'm just a cyan with a good keyboard tbh, will watch your vids later tho :)

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

        I only got AC because I saw your video and reused the code that I submitted for your contest. You are a prophet orz

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

        I got the diameter idea just because I did those gym questions. Thanks!

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

Hi, I believe that it is not necessary to use binary lifting for problem d1C. We could use a segment tree of pairs (v, -index), and do a range minimum value on this.

This will automatically tiebreak for the larger-indexed nodes to come first.

Edit: Thanks for the round, the problems were interesting!

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

    Yup it's true, a lot (including me) solved it like this. However, the tutorial's solution can be implemented using only Fenwick tree, and we feel like it's more beautiful to include such a solution.

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

    can u explain more how can we do that? i seen your submission when u take query why u have change your b to N-b

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

      I’ll assume you mean why I insert the value v=mp(s-A[s],-s) as your question.

      The reason why the first value of the pair is s-A[s] is as stated in the editorial. When we query, to prevent numbers from going below zero, we would like to find the maximum index that is zero. Since c++ compares values by the first digit, then second digit, and that we have a minimum value segment tree, we acquire the maximum index by inserting -s as the tiebreaker. This means the largest index is reflected as the minimum value.

      Hope this helps!

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

STRONG PRETESTS

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

nice problems

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

Good problems! Congrats on great round!

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

In the tutorial of tree tag in case 1 why are we saying that alice tags bob in the first move? Did the question mean that even if alice and bob share a vertex once ..alice wins??But i don't think it was mentioned as such??Can u please answer[user:Monogon]

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

    The statement said in at most 10^100 moves, meaning Alice can win before that many moves are done. I hoped the explanation of sample 1 would make this clear. Sorry that the statement wasn't more clear about it.

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

      I got confused by the last line saying that if after 10^100 moves they occupy the same place. Thanks for the clarification. Great round.

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

My Code using Vectors gave Runtime on Test 4 while the Same code using Array Passed!

Someone please help?

Edit: Got it

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

    You call v.clear() after every test, which means your f(i,0,k+1) v[i]="" and so forth only work for the first test. After that it's UB / crash. See 92079157.

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

      Oh! Got it Thanks!

      Now I'm wondering how it

      • worked on my local compiler (Sublime Text 3)

      • passed the first 3 pretests lol

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

        Undefined Behaviour means that anything is possible. Unlike some more programmer-friendly languages, C++ is rather harsh and won't check for things like "out of bounds access" because that would be a waste of time for a correctly-written program (unless you enable the debug mode!).

        The first two tests seem to have small sizes of $$$n$$$, and the third one only had one sub-test.

        Data in a vector lives in some block of memory allocated to you by the OS, and it will be at least a couple of kB. It just so happens that this block was large enough to fit the small tests (you were still going out of bounds, but it kinda worked because there was accessible memory there). But when your program tried to reuse over 1 MB of memory that had no longer belonged to it, you were out of luck.

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

<rant> Ahhhh I got the first 2 cases for D1B / D2D, but didn't expect such as simple algo for 3rd and 4th cases. I guess I need to work on proofs for next time. </rant>

Great contest, hope to see more like it :)

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

Can someone explain me the meaning of problem Div2.D (Tree Tag). I read it again and again for about one hour and yet could not relate the meaning of the problem.The problem statement is so unclear.

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

    I'll try to explain in graph theory terms.

    Alice and Bob are each located at some node in a tree (undirected fully connected graph without cycle). Each edge has length 1.

    On Alice's turn, she starts at her current node and can take any walk any path with length <=da to end up at her next node. On Bob's turn, he does the same (with path length <=db instead of da). Alice wants to end up in the same node as Bob at some finite time. Bob doesn't want to be in the same node as Alice.

    Print whether Alice reaches same node as Bob or not.

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

      Thanks, I got it now. I thought that for alice to win,alice and bob had to be in thier same respective initial nodes .

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

    The variables used for notation were unclear to me at first as well.

    We have a tree T on n vertices. Alice is initially at vertex 'a' of T, and Bob is initially at vertex 'b' of T. Now, Alice has a jump-power of "da" (this da is unrelated to the vertex a, nor is it d*a). da (better written as d_{Alice}) is the maximum distance Alice can jump in one move. Similarly, db (better written as d_{Bob}) is the maximum distance Bob can jump in one move.

    Now they take turns jumping from vertex to vertex. In each move, Alice can move from her current vertex (say u) to a vertex v such that

    dist(u,v) <= d_{Alice}.

    The legal moves for Bob are similar, but with d_{Bob} instead of d_{Alice}

    If within 10^{100} moves, Alice can force herself and Bob to be on the same node, then she wins, else Bob wins.

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

somehow i feel prob B is the hardest :v

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

Very cool problems! Thank you!

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

STRONG PRETESTS..

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

Please attach codes also

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

For Div2D, my code mysteriously fails on pretest 9... : https://codeforces.me/contest/1405/submission/92071460. I have the same logic/cases as the editorial, and I calculate the diameter of the tree with two runs of bfs. Any help would be greatly appreciated.

I'm a little bummed because I was so close to solving 4 questions which was my goal for this contest.

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

The real question is how did your checker for problem D work if they choose to play as the first player? It sounds way easier to solve than to verify...

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

In problem DIV2(D) Tree Tag , nowhere in the problem statement it is mentioned that if Alice catches Bob then Alice wins or vice versa. I think that the problem statement should be updated! Correct me if I am wrong :)

Edit: Finally understood what "If after at most 10^100 moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins." means :)

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

    Alice and Bob occupy the same vertex, then Alice is declared the winner.

    It was quite obvious from the above line that Alice will try to catch Bob (occupy the same vertex) and Bob will try to avoid.

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

Can Anyone tell me Why Did Codeforces compiler gave Run Time Error in my Solution to Problem B Div 2.

My Submission: 92070449 while other compilers like https://www.onlinegdb.com/ gave correct answers?

I wasted the whole 1 and a half hours to debug this still can't. Can anyone tell me why did it happen?

I just Used Lower Bound to find the position of the lowest index greater than my present positive index and after operation erases it.

Please Help !!!

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

    I think itr = -1 causes a problem; what do you do when neg becomes empty?

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

      neg will never become empty because the sum of all elements of array is 0.. So vector pos and neg will become 0 at the same time.

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

        Actually, I printed itr immediately after

        auto itr=lower_bound(all(neg),pos[0])-neg.begin(); if(itr>=sl(neg)) itr=sl(neg)-1;

        and I did see itr = -1.

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

          Ok, you're right.. When I added if(itr<0) break, the code worked, Thanks for Help !! b/w how did you debugged the code.. When I was trying to print it was giving run time error and I don't know how to use debugger..

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

            You said your code worked on onlinegdb, so I tried it there. It did not give a runtime error there because of how it handles neg[-1], perhaps.

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

My submission for Div2 D Tree Tag gives WA on pretest 2.

I checked the cases in below order:

  1. If db < 2*a + b. Alice Wins

  2. If distance of Bob from Alice <= da. Alice Wins.

  3. If diameter >= db. Bob wins, otherwise Alice Wins.

Is there anything I'm missing(as per editorial I think not) or my implementation has a bug?

submission : 92063203

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

Can someone please help me find the type of input my solution is failing in Div2C. It shows wrong answer on 2343rd case in test case 2.My Solution

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

    would you explain what you are doing in your code?

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

      I'm grouping the characters in the string with respect to their position modulo k. If in any of these groups both O and 1 is present output NO. If only 0 is present add the size of this group to a variable called zero. If only 1 is present then add the size of the group to variable called one. Finally if n is even check and if either one or zero is greater than n/2 answer is NO or if n is odd and either one or zero is greater than n/2 + 1 answer is NO. Else the answer is always YES.

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

    I think your code fails the following case

    1 6 4 110011

    Your code gives No whereas the answer is Yes. Hope this helps!!!

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

In problem D is was stated "Note that when performing a move, a player only occupies the starting and ending vertices of their move". Then shouldn't Alice win on his second move when 2 * da >= dist(a, b)?

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

    I think it means that the players 'teleport' instead of walking along the tree

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

      If after at most inf moves, Alice and Bob "occupy" the same vertex, then Alice is declared the winner.

      I assumed I have to consider both starting and ending node of one's last move :facepalm:

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

I think I have superpower, no matter how much time do I have, I can still solve a problem in a minute AFTER the contest ends))

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

Why didn't this work in div2 D 92059623?

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

Div2 C has appear in earlier Contest on Codeforces itself(k periodic string)

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

I just don't get it. Why cant I solve problems like today's Div2-B? I mean Mathy questions like these. What should I do to solve such questions? What tags should I solve, or what question types? In fact, what is even this category — Greedy + math, Math, Problem solving?

Any one who was weaker on such problems and now comfortable in them, can you share what you practiced for these?

I am not so bad at CP that I should not even be able to solve it in 2 hours. Its just the approach doesn't strike me. I look at the test cases, develop some method to at least have a functional code for the visible test case then try some cases of my own. Finally when I cant think of any test case that might fail for my code, I submit just to see Wrong answer on Pretest 2. I know this is not the best of the methods but that is just how it happens with me. :(

Some insight please!

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

    Dude, you need to relax. If you try to rush things during the contest you will not solve the problem!!! When you read a problem, 99% of the time you will have no idea how to solve it. But don't panic!!! First thing you should do is to read statement very slowly, identify all details that look important. Then stay away from the computer and take a sheet of paper to try some test cases. Always solve a problem on paper first!!!

    You need to be organised with your notes, otherwise you will get confused. Use as much paper as necessary, don't restrict yourself to tiny margins.

    You need to look for patterns you have seen before. This requires PERSISTENCE, leave no stone unturned. If making an assumption helps, do it! Breaking the problem in cases just gives you powerful assumptions to work with.

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

In div2D Tree Tag

initially, we have to check case 1 that is fine as Alice has the first move

why do we need case 2 as for every test case will fall in either case 3 or case 4?

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

    If $$$2da \geq \text{tree diameter}$$$, the answer is Alice even if $$$db > 2da$$$

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

      Thanks! I got it, db will not matter as bob is limited by the diameter of the tree so he can't use his db to full extent to escape.

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

s=[f(1,r),f(2,r),…,f(n,r)] ?

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

For problem Div2 D my logic was similar to the editorial. Yet, I got WA on 274th case in the second test case.

I would be much obliged if someone could go through my code or give me some test case where my code fails.

Code

Also adding the link to my submission Link

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

The contest was well balanced. Thank you for organizing it.

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

92082520 I’m very confused about this wrong submission. Anyone can help me? Thanks!

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

Does anyone have an intuitive solution for Div2 B?

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

    The solution I wrote in editorial is not very intuitive, but I chose it because it is easier to prove. Here is another way to view things:

    You can visualize $$$a_i > 0$$$ as a pile of $$$a_i$$$ tokens, and $$$a_i < 0$$$ as a "gap" that need to be filled with $$$|a_i|$$$ tokens. You want to make everything flat.

    Free operations move tokens to the right. Imagine walking on the array from left to right, having a "bag" of tokens. When you encounter a pile ($$$a_i > 0$$$), put all these tokens in your bag. When you encounter a gap ($$$a_i < 0$$$), empty your bag here as must as you can.

    This is equivalent to doing $$$\text{sizeBag} := \max(\text{sizeBag} + a_i, 0)$$$.

    It is "intuitive" that you use free operations as best as you can. The final answer will be the final size of your bag (the number of tokens you couldn't put into gaps).

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

    For free operation ->

    i<j and you are to increment aj and decrement ai, so by decrement if you have to make ai zero then it should be positive number right. so a negative number can make positive numbers of past towards zero in free.

    	int positive = 0;
    	for (auto i : a) {
    		if (i > 0)positive += i;
    		else positive -= min(positive, abs(i));
    	}
    	cout << positive << endl;
    
    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +4 Проголосовать: не нравится

      Thanks. I find the backward version also fairly intuitive :

      long long sum_pos = 0;
      long long sum_neg = 0;
      for (int i=n-1; i>=0; i--)
      {
          if (a[i]<0)
          {
             sum_neg += a[i];
          }
          else if (a[i]>0)
          {
             long long free = min<long long>(a[i], -sum_neg);
             sum_neg += free;
             sum_pos += (a[i] - free);
          }
      }
      cout << sum_pos << endl;
      
      • »
        »
        »
        »
        4 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Backward version can be a bit clearer.

        Walk through a, if last (current) element is negative then accumulate it to accum — there will be "free of charge" turns to make it zero. If it positive and accum + last is positive then we need "paid" turns.

        My submission 92031517 (I use Kotlin). Note reversed() method in the read data part.

        val n = readInt()
        val a = readArr(n).reversed()
        
        var accum = 0L
        var ans = 0L
        for (ai in a) {
            accum += ai
            if (accum > 0) {
                ans += accum
                accum = 0
            }
        }
        println(ans)
        
»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can't we do 'C' using the Sliding window technique?

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

Nice problems and thanks for fast result and fast editorial!!

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

Why I was getting TLE plz can anyone help in test case 9 Code:https://codeforces.me/contest/1405/submission/92085005

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

    prob B

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

      the complexity of the algorithm is n squared and do not write such shit code because no one will ever help you look for errors in such code

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

        How to make a code simple? Is my way of writing code shit or my logic is shit???

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

          u code style is shit :), check example my code or code of a famous coder (red mb)

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

This round made me blue, so can't complain. Nice and fast editorial :)

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

Authors solution for E is pretty cool, but there were a similar (but much harder) problem in ptz winter 2020 which inspired me for this solution:

Let's decide for each cell will we cover it by a vertical (V) line or a horizontal (H) line. For test form the statement it will be like this:

V.VV
VHH.
VH..

We can see that we pay for horizontal line once in its beginning. So we pay for ".H" or "VH". Similarly for vertical lines. So we have to choose one of two options for each cell, and we pay something for choosing different options for some pairs of cells and also something fixed for each choice. That's standard min-cut problem.

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

Why this solution fails My submission I am trying to debug it from the last 1.5 hours, Fully exhausted now, Any help will be highly appreciated! UPD: I got it!

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

Can someone please help me understand what is the error in my code?

The submission id is: 92058038 .

Any help would be appreciated.

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

what if we have to tell "weather Alice can capture Bob in almost k turn" or "what is the guaranteed min turn in which Alice can capture Bob", in question D Tag Tree.

the following will be the ans, right ? or Am I missing something

case 1 : 1 turn.

case 2 : 2 turn .

case 3 : -1 (never)

case 4 : ceil(dis(a, b) / da).

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

In div2A if I reverse the array of odd length then the middle element remains in the same position, can anyone explain how reversing gets the correct answer, I know I am sounding dumb but I got this doubt. I actually didn't get a thought about it when I submitted my code.

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

    The permutations are considered different if they disagree at some index, not all indices. $$$[1,2,3]$$$ and $$$[3,2,1]$$$ are different permutations.

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

please help me with this. Div2 D. it's showing runtime error on test-2. https://codeforces.me/contest/1405/submission/92077516 variables meaning- ans=initial dist from alice to bob. path=dia of tree.

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

What to do if I can't understand the proof of div2-b task? How do you upsolve if you can't understand the editorial?

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

In problem 2E, what exactly is the BIT you want to maintain? Because s is already weakly decreasing, binary search on s for the lmax will be log(n), but the tutorial said naive binary search needed log^2. Very confused. Can someone help me

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

    An operation on BIT takes $$$\mathcal{O}(\log n)$$$ time, and binary search do $$$\mathcal{O}(\log n)$$$ operations on the BIT. Hence, finding $$$l_{\max}$$$ with this method $$$\mathcal{O}((\log n) \cdot (\log n)) = \mathcal{O}(\log^2 n)$$$ time.

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

      I mean, wouldn't BS on s just be fine? Or is s just the bit?

      Do you mean s[i] = f(1, r)+f(2, r)+....+f(i, r)?

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

      My current understanding:

      For each step, (if necessary) use binary search on s to find max, and use a BIT which is the prefix sum of s to increment [1, l] by 1?

      So I don't know why binary search is on the BIT? Is something wrong?

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

A nice trick of divC!

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

https://www.youtube.com/channel/UCBStHvqSDEF751f0CWd3-Pg?view_as=subscriber

Video Editorials of all latest contests , all questios . Even courses for various coding techniques will be taught . Do share , subscribe and like

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

sorry hugopm for bothering again:

In tutorial: Note f(l, r) the maximum number of elements we can remove in the subarray a[l...r] (zero if l>r). During our iteration over r, we're going to maintain the answers for each l: s=[f(1, r),f(2, r),...,f(n,r)] <-------- here do you mean f(l, r)? Also, do you mean for each l we maintain s_l=sum of everything in the bracket?

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

Would it be possible to solve Div1C using Mo's algorithm ? I misread the ai = i condition and thought that ai = i should hold for the sub-array [L, R] (with indexes from the sub-array).

For Mo's algorithm, addition/deletion from the end point should be pretty simple while addition/deletion from the beginning should be doable by using a deque(I'm still a bit confused about this part)

The TL of 4s seemed pretty generous so I got stuck on thinking about Mo's Algorithm ;-;

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

    Mo's Algorithm doesn't work there.Because when deleting/adding from the begining , the effect will be quite complex!Though deleting/adding from the end is easy!

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

i'm still not able to understand the tree tag question and then it is obvious not get the tutorial to can someone please suggest me what are the prerequisite in need to know or if someone is uploading it's video tutorial then please share the link ... thank you in advance

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

    Some points to note in this question are-

    ->If Alice can catch Bob in first move then she wins.

    ->Bob wont try to move unless Alice comes to catch him.

    ->If the length of longest path on the tree (i.e. the diameter of tree) is <= twice the length of range of a (i.e. da) i.e. Alice can reach the midpoint of the diameter and if then Alice has her range such that she can reach both the endpoints of the diameter in one jump then she will definitely catch Bob because Bob must be in her range (as diameter is already the longest path).

    ->If Bob can in a single jump go from a vertex u to vertex v such that Alice can only have a single vertex (either u or v) in her range then Bob can keep jumping from u to v (when vertex u is in range of Alice) and v to u (when vertex v is in range of Alice).

    ->If Alice can have both the vertices u and v in her range in a single jump then she will catch Bob in the end.

    ->Alice will try to stay (da-1) distance away from Bob in order to maximise the distance that Bob needs to escape her.

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

One more hack case for D: 1 7 3 6 2 3 1 2 2 3 3 4 4 5 5 6 5 7

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

Could someone please explain in brief how the diameter of tree has been calculated with only one dfs in the above implementation in Div 1-B / Div 2-D problem Tree tag. Thanks in advance.

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

    For each node, you basically calculate the sum of the two longest paths from it (or one if it's a leaf) except for the path to it from the initial vertex. If the initial vertex is a part of a diameter-length path, it's obvious this will return the right result.
    If the initial vertex is not a parth of such path, consider the first vertex you encounter during the DFS that is indeed a part of such path. Let us denote that first vertex by u. The counter intuitive part is that you don't consider the path from the initial vertex to u — BUT, since all the vertices on the path to u were, by definition, not a part of any diameter-length path, they do not need to be considered. You will then take the sum of the two longest paths from the current vertex, not including the path to it, and that will be the diameter.

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

is it rated? my change has gone away..why?

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

I have a problem for Div1 B: if $$$tree\ diameter\leq 2*da$$$ then Bob can't win.But why if $$$tree\ diameter > 2*da$$$ and other cases are all satisfied ,Bob is sure to win?

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

    If diameter > 2*da then bob every time go to that long diameter place and if distance between them <=da then bob goto to the other side of that diameter..so alice can not touch him anyway.

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

      But can you make sure that Bob can walk to the diameter?

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

        Bob will run to the leaf node away from Alice(it can be proved that there will always be one), from there he will make a jump,if alice cant catch him in first move,he can't catch till he reaches leaf node as db>da and he is running in opposite direction.

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

          But if Bob reach a leaf ,he can't find a node which he can reach but Alice can't?

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

            sorry I didn't get it, bob reaches the leaf node and which node should he find?

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

              The node that Bob can reach but Alice can’t reach

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

                I am not sure if I get your question properly, but once bob reaches the leaf, Alice should be at a distance of atmost a(if greater than 'a' we will wait for him to come nearer), then we make a jump, since we are at the leaf node we can jump at a distance of min(db,diameter) and this distance should be greater than 2*da, as it should cross Alice and he should not catch us in the next move also. Root the tree at bob's initial position, it will be easier to visualize

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

          Or can you prove it can't happen?

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

It's a bit late, but I have a solution for Div2E/Div1C that isn't that practical, but I find rather cute (it was also the first thing I thought of).

Instead of doing what is in the editorial, consider solving the problem without constraint of $$$x$$$ or $$$y$$$. The following greedy algorithm works: At every step, choose the highest $$$i$$$ to remove if possible. The reason is obvious: if there are multiple with $$$i=A[i]$$$, the only one that won't force the others to become $$$i<A[i]$$$ is the largest. Therefore define a prototypical sequence of removals $$$R$$$, from the greedy algorithm. (To actually find $$$R$$$, I used a lazy propagation segtree with pairs — there may be an easier method.)

When there is the constraint of $$$x$$$, notice that after the first element $$$R[i] \leq x$$$, the rest of $$$R$$$ after it cannot be removed. We can have prefix min on $$$R$$$ and binary search for this element, let's call it $$$k$$$. Also, for the constraint of $$$y$$$, all elements where $$$R[i] \geq y, i<k$$$ cannot be removed as well by definition (but our inability to remove them doesn't affect our ability to remove other elements). To get this count we can answer queries offline in increasing order of $$$y$$$ using BIT.

Write-up is long, but the idea is really simple. However, implementation is some mess.

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

    Yeah, I actually came up with this simulation solution before the official BIT-only one.

    You may want to check my implementation, which is short and fast. The trick is that, in order to decrement a suffix beginning at $$$i$$$, we can decrement the whole array, and when we go down the path leading to the leaf $$$i$$$, each time we go to the right child, we add $$$+1$$$ on the left child. Hence, we can do "descent in the tree" (finding rightmost zero) and "decrement on suffix" in a single function "pop".

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

    Instead of using Fenwick tree to get around the constraint on $$$y$$$, one can instead build the segment tree of $$$R$$$ (as defined in parent comment by astoria). Each node (covering the range of indices $$$[l, r]$$$) stores the array $$$[R[l], R[l+1], R[l+2], ..., R[r]]$$$, but sorted in ascending order.

    Then, for each query, one can use the segment tree of $$$R$$$ to query the number of array indices $$$i$$$ of $$$R$$$ in the interval $$$[1, k-1]$$$ such that $$$R[i] \leq n-y$$$, where $$$k$$$ is the smallest index $$$k$$$ such that $$$R[k] \leq x$$$. Of course, we need to be careful of the edge case when $$$k = 1$$$.

    Now, the solution works with online querying (without any persistent data structures contrary to what is mentioned in the official editorial), but in a slightly worse overall time complexity of $$$O(n log n + q log^2 n)$$$.

    My code is rather messy, and it can be found here.

    Please correct me if I made a mistake.

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

Is this algorithm to find the Diameter of a tree is correct

  • Find a leaf node, say N

  • Do DFS to get the farthest node to it

  • This node will be one endpoint of diameter, say p

  • Find second diameter endpoint by doing a DFS again from this endpoint p

But for me it fails, I don't know why My Submission

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

    Your code is long, so I couldn’t totally tell, but did you make sure that the first dfs and second dfs don’t overlap? If they do, the actual diameter is shorter than it would output.

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

    There are two bugs in your code:

    1) BFS is wrong. Line no 26 should be x.d = 0;

    Testcase

    2) DFS2 is wrong. Line no 103 should be x.d = 0;

    Testcase

    Bonus: In DFS1 also, line no 64 should be x.d = 0; but it won't affect the final solution because x.d is increased by 1 for all nodes and here you only concern about the node number with max x.d.

    AC Submission : 92202644

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

Can someone please tell where did I go wrong in this solution for div2C?

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

.

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

Fast Editorial & Ver Concise....!

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

can someone explain sample 3 of div2D

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

I didn't understood why the suffix max sum is the answer of problem b? can anyone help

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

Since we are not in case 2, there is at least one vertex with distance at least da from Alice. If Bob is at such a vertex at the start of his turn, he should simply stay there.

I think it should be "at least greater than da from Alice". Because, if distance is equal to da, then Alice can reach it.