awoo's blog

By awoo, history, 5 weeks ago, translation, In English

1681A - Game with Cards

Idea: BledDest

Tutorial
Solution (BledDest)

1681B - Card Trick

Idea: BledDest

Tutorial
Solution (awoo)

1681C - Double Sort

Idea: BledDest

Tutorial
Solution (awoo)

1681D - Required Length

Idea: BledDest

Tutorial
Solution (BledDest)

1681E - Labyrinth Adventures

Idea: BledDest

Tutorial
Solution (awoo)

1681F - Unique Occurrences

Idea: BledDest

Tutorial
Solution 1 (awoo)
Solution 2 (awoo)
 
 
 
 
  • Vote: I like it
  • +63
  • Vote: I do not like it

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

Why does my submission(158304624) for C give WA ("Arrays are not sorted")? My approach is to store each pair of $$$a_i$$$ and $$$b_i$$$ for both sorted and original arrays and compare all pairs, since pairs can't change, and then use a simple bubble sort to print swaps. I tried testing it on cfstress but it didnt give me any counterexamples(link).

  • »
    »
    5 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    here is your accepted code

    you must do swaps in both arrays as the problem say but you did swaps in only one array. you can compare your previous code with this new code to see the few changes.

»
5 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

Through many hacks and further FST's on problem D, my rating is now 1899 :))

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

can any body tell me why this happened? these submissions are shared the same code 158313258: GUN C++ 17 158313168: GUN C++ 14 158313129: GUN C++ 20 (!! TLE !!)

»
5 weeks ago, # |
  Vote: I like it +4 Vote: I do not like it

Finally I became a cyan !!

»
5 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

For problem C the name "Double Sort" gives the hint to use Bubble Sort

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Exactly!!! I used Selection Sort in this problem during the contest and unfortunately I got WA in both the submissions, but now when I implemented the exact same logic in Bubble Sort, it got accepted. This indicates that implementation with selection sort has some complications which bubble sort doesn't have.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      i did selection sort

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It's easy to see that any comparison based sorting algorithm is entirely valid for this problem. If you're getting WA that means you didn't implement it correctly.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Not sure about that. What we need is not comparison-base but stability. Any stable sort should do

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          If you sort the values as pairs $$$(a_i, b_i)$$$, you don't need stability.

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          A comparison based sort on both array values doesn't need stability, as all entities in question will already be considered by the comparator.

          something like this works just fine:

          bool compare_first(const std::pair<int, int> &a, const std::pair<int, int> &b)
          {
            if(a.first == b.first)
            {
              return a.second < b.second;
            }
            return a.first < b.first
          }
          //----------
          bool compare_second(const std::pair<int, int> &a, const std::pair<int, int> &b)
          {
            if(a.second == b.second)
            {
              return a.first < b.first;
            }
            return a.second < b.second
          }
          

          Just make two passes as follows (and track the inversions):

          any_sort(arr.begin(), arr.end(), compare_first);
          any_sort(arr.begin(), arr.end(), compare_second);
          
          • »
            »
            »
            »
            »
            »
            5 weeks ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I got it from Bleddest's response.

            It's just that my solution relied on stability

            Then again, there is no need for it to be comparison-based, you can implement this with any sorting

»
5 weeks ago, # |
  Vote: I like it +10 Vote: I do not like it

Something really unfair happened to me in this contest... I got accused of cheating in this round ... But this is not true. I guess this happened to me because I use Python and problem A and B where really straight forward , for example in problem B , most of the line of the code is for taking input and the actual solution is only of 1 line , which can be easily be similar to someone else. (158180193 my solution of B). This kind of things should be avoided at all cost , because it demotivates the falsely accused person!

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

Can someone explain how we are getting min operations with BFS in problem D. Shouldn't we sort the string before multiplying?

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

    Basically, we are using BFS because we don't know the future digits coming into our number after multiplication. Hence we can't pick any particular path before reaching the destination(i.e number of n digits) so we will visit the all the possible numbers(the numbers that we get after multiplying the current number with all distinct digits present in the current number) if we haven't visited them yet. As we are looking for all the possibilities sorting the string won't help.

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

Can anyone explain to me F better? What are the transitions, mentioned in the editorial?

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

    I think it is basically building Splay tree and then normal DP . So , if you don't know splay tree just go through it once . You will find a blog and video on it in CF and also on CC .

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

    If you like, I can give another solution to you.

    But it solves the problem from an aspect differ from the tutorial.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yeh Sure, lingfunny !

      • »
        »
        »
        »
        5 weeks ago, # ^ |
        Rev. 2   Vote: I like it +6 Vote: I do not like it

        Sorry for the late reply and my poor English.

        Let's try to calculate the answer from the different weights.

        For every kind of weight of an edge, try to figure out the value that this kind of edge contributes to the answer.

        For example, in the following illustration, the contribution of edges weigh $$$2$$$ is $$$3\times1+3\times3=10$$$.

        illustration_1

        Why?

        If we cut the every edge weighs $$$2$$$(red edges), we will get $$$3$$$ new trees.

        For the edge $$$(6, 7)$$$, the new trees it connected are $$$4,5,6$$$ and $$$7$$$, so answer will add $$$1\times3$$$. Because for the paths from $$$u$$$ to $$$v$$$, where $$$u=4,5,6$$$ and $$$v=7$$$, the value $$$2$$$ will definitly appear exactly once.

        For the same reason, edge $$$(4, 1)$$$ will contribute $$$3\times 3$$$ to the answer.

        Obviously, the edges with different values can't influence each other, so You can calculate the different values of edges partly.

        Here is how to calculate the answer of edges with same value.

        Firstly, get the 'bracket order' of the tree. Let $$$L[u]$$$ and $$$R[u]$$$ be the begining and ending indices of $$$u$$$ in the 'bracket order'.

        It's another kind of Euler Tour of Tree. Instead of output the number as soon as you visit a node, you will output the number only when you first visit the node or finish visiting the node.

        I don't know how to translate 'bracket order' to English since I'm Chinese and my English is poor. For example, the 'bracket order' of the tree in the illustration is $$$\texttt{4 5 5 6 7 7 6 1 2 2 3 3 1 4}$$$, hope you can understand what I want to express from the example. Or maybe you can see my code in the end.

        Then, for a tree, cut the edges in dfs order and split the subtrees out, and cut the edges in a subtree recursively.

        function Cut(int CurL, int CurR, int &CurE):
            for CurE in [CurL, CurR]:
                cur_real_size -= size[v]
                append value of Cut(L[v], R[v], CurE) to real_subtree_size
            for val in real_subtree_size:
                answer += val * cur_real_size
            return cur_real_size
        // edges are sorted in dfs order, so an edge have a dfs order, you can judge whether the edge is in the subtree or not.
        // CurE means the index of the edge you are visiting
        

        When you cut the subtree recursively and update CurE, CurE is increasing and always less than $$$O(n)$$$. So the time complexity is $$$O(n)$$$.

        You can see my submission to have a better understanding, I think it's quite short: 158222894

        Actually I don't know whether it's violation of rules for me to type the solution in the comment. If it is, please tell me and I will delete my comment at once.

        Apologise again for my poor English.

        • »
          »
          »
          »
          »
          5 weeks ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          May below code can explain what is bracket order.

          def bracket_order(node):  
              print(node.val)  
              for child in node.children:  
                  bracket_order(child)  
              print(node.val)  
          
»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

What is the solution to the dynamic connectivity problem mentioned in F? Is there a good tutorial or video about it?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Codeforces EDU DSU section last lesson where the guy talks about DSU Mo's, offline dynamic connectivity using rollback DSUs. Go check that out.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Where is this section?

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          Vote: I like it +20 Vote: I do not like it

        Damn I either overestimated the average people’s ability to navigate codeforces or codeforces is not as user-friendly as I thought for people whose English is not their first language, based on this comment. It’s under codeforces EDU DSU step 3 theory. link

»
5 weeks ago, # |
  Vote: I like it -13 Vote: I do not like it

Apparently C++14 log10 doesn't work properly for counting digits of numbers like: $$$10^{k} - 1, (k>=15)$$$ :(

Anyone know why?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +4 Vote: I do not like it

    Precision issue? Doubles and long doubles have limited precision, so the rounding could be off when the value is very close to a power of 10 for large numbers. It’s better to not use doubles/long doubles if possible. Just stick with integers manipulation

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

If someone solved F using HLD, could you please share your approach? Here's the submission SSRS_ made using HLD: 158189118

»
5 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

If you are/were getting a WA/RE verdict on problems from this contest, you can get the smallest possible counter example for your submission on cfstress.com. To do that, click on the relevant problem's link below, add your submission ID, and edit the table (or edit compressed parameters) to increase/decrease the constraints.

If you are not able to find a counter example even after changing the parameters, reply to this thread (only till the next 7 days), with links to your submission and ticket(s).

»
5 weeks ago, # |
  Vote: I like it +9 Vote: I do not like it

Can someone explain the solution for problem E that uses segment tree, please?

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    +1

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    The idea is to maintain a structure in each node (let's say this node represents layers $$$[i,j]$$$), that contains the following information:

    • shortest distance starting in front of top door of $$$i$$$-th layer and ending in front of top door leading to $$$(j+1)$$$-th layer

    • ...3 other similar values for other combinations of top/bottom doors.

    It is possible to merge the structures for ranges $$$[i,j]$$$ and $$$[j+1,k]$$$ easily (maybe not, but my code was quite sloppy).

    To answer queries, if the cells are in the same layer, it will just be the distance between them. Otherwise, we can find the answer for going from $$$[\text{starting layer},\text{ending layer})$$$ and then find the time taken to get to top/bottom doors for each layer. Then the answer is just the minimum of the possibilities $$$+1$$$ (to take care of crossing the door to the $$$j$$$-th layer).

    By the way, the identity element for the structures (or answer for $$$[i,i]$$$) is not all zeroes, it must be $$$\infty$$$ for top->bottom or bottom->top to maintain consistency. And some other stuff, implementation requires care I guess...

    Submission: 158239579

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it +6 Vote: I do not like it

      Interesting solution, thank you for sharing!

      After some time thinking about it, I realized that it's just a waste of time to implement this :(

»
5 weeks ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Can someone please tell me why, for C — Double Sort, the simple bubble sort that swaps every time there is an adjacent inversion for either array A or array B works? For more details, see my submission. I spent some time trying to come up with a proof, but cannot.

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

    Ok if at all someone is interested, I claim the following.

    Claim 1: If there exist indices $$$i \neq j$$$ such that $$$A[i] < A[j]$$$ and $$$B[i] > B[j]$$$, then both arrays are unsortable, i.e., the answer is $$$-1$$$.

    Claim 1 is easy to prove.

    Claim 2 (The other direction of Claim 1.): If there do not exist indices $$$i \neq j$$$ such that $$$A[i] < A[j]$$$ and $$$B[i] > B[j]$$$, then both arrays are sortable.

    Claim 2 is also not very hard to prove.

    Now, we can prove by induction that Bubble sort always keeps sortable pairs of arrays sortable and that after outer iteration number $$$i$$$, the $$$i$$$-th maximum of both arrays is pushed to the end in its proper place.

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

omg!why my Problem C code always runtime158341737

»
5 weeks ago, # |
  Vote: I like it +19 Vote: I do not like it

In problem F,if you use link cut tree to maintain the size of subtrees to solve this problem,it will be much easier.(

https://codeforces.com/contest/1681/submission/158267779

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

In problem D,you can use IDA* and you don't need to think about the problem.And it's also can solve the problem.

»
5 weeks ago, # |
  Vote: I like it +10 Vote: I do not like it

Can someone explain the O(n^2) solution of F in a more detailed way? What is i in dp(v,i)? How does the transition work? What does the phrase when you consider gluing up paths from different children mean?

And also, why are the Tutorials on the challenging problems not elaborative enough? Those are written in a way that is tough to follow for Pupils or Specialists.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it +49 Vote: I do not like it

    I think that Pupils or Specialists just won't gain anything from upsolving the problem. You won't develop the intuition for similar problems by retyping the transitions in dp from the editorial. It's probably better to go solve easier tree dp problems first, learn different ways of counting the paths in trees, and then tackle this problem.

    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it +14 Vote: I do not like it

      Thank you for the reply. As a modest suggestion, I think it would be a good thing to keep some prerequisite sections in the tutorials (or link to a blog[in case of common techniques/Algo/DS] or the same kind of easier problem) to understand the concepts behind the main problems better.

      Once again, thank you for writing these problems, it's always good to learn new things through solving problems.

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

Problem C:

Can Someone Explain me ! Why My Code Failed

Wrong Answer on test 2 (test case 65) 158363800

  • »
    »
    5 weeks ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    It is usually beneficial if you can find a smaller test case where your code fails, and then try to figure out where your code is wrong.

    Edit — Test case:

    1
    6
    5 4 5 5 4 5
    3 2 2 5 2 3
    
    • »
      »
      »
      5 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      okk thanks i will try to find my bug but well i already solved the problem using different method

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

Hi, I would really appreciate it if somebody could tell me why my code doesn't work for C.

Let me explain my approach.

  1. Bubble sort $$$a$$$ and for every $$$i$$$, $$$j$$$ that is swapped in $$$a$$$, swap it in $$$b$$$ as well. Store all pairs $$$(i,j)$$$.
  2. After this bubble sort, $$$a$$$ is guaranteed to be sorted
  3. If $$$b$$$ is also sorted then we are done and dump out the swapping indices stored in step 1
  4. If $$$b$$$ is not sorted, we can still sort it if all the swaps necessary to make $$$b$$$ sorted, have the property that $$$a_i = a_j$$$.
  5. Run bubble sort again on $$$b$$$ and check if a swap has been made for which $$$a_i \neq a_j$$$, if so, report $$$-1$$$, else continue with the sort
  6. If our algorithm has still not terminated it is guaranteed that $$$b$$$ is sorted and $$$a$$$ is also sorted, so it is possible and we dump out all the indices we had stored.

The judge says my algorithm produces a case in which $$$b$$$ is apparently not correct. My code is here.

If you're able to come up with a counter-example, I'd really appreciate it if you can tell me what motivated it, I'm still improving my ability to think of counter-examples.

  • »
    »
    5 weeks ago, # ^ |
    Rev. 3   Vote: I like it +3 Vote: I do not like it

    The following approach usually helps me in finding out a counter example:

    First of all, try to go through the approach and at each step, ask if what you are doing is correct. Is there some case where your assumption is failing? Is there some case where your expected result will not hold.

    Once you are confident that your approach is correct (on paper), try to go through the code and check if each block is behaving exactly how you want it to behave. You can usually check by taking some small examples.

    The above process usually helps me finding out the blocks where my approach might be wrong, and gradually helps in getting a counter case.

    Try with the above approach once for some time. If you are not able to find some counter case after enough efforts, this test case generated by CF Stress might help.

  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have solved the problem with exactly the same logic: 158188911

    So, algorithm is ok, check your code.

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

Has anyone solved D using DP?

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

D is one of those rare question where thinking about the brute force solution is not at all tricky/difficult but computing its time- complexity is ! i thought that at each node(in bfs) we can have atmost 8 choices to multiply with (2,3,4....,9) and in worst case scenario we need to multiply x 64 times....(2^64 is of order of 1e19) ..hence leading to (8^64) operations which i though will surely TLE,MLE , hence didn't even try the approach:( Lovely question tho , to know whether u know time complexity well or not ... :) Can any one link out similar question ? looks like i am bad at computing Time complexity!!!!

»
5 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

In editorial of D: I don't understand that is sufficient to say time limit won't exceed. We have 1.5 million states in total. But they can be called multiple times.

Can somebody explain its time complexity please?

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

    Each 1.5 million states can have at most 9 outgoing edges because you could multiply that state with 1,2,3,...9

    So number of edges in the graph is upper bounded by 1.5*9

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

It seems that tourist solved F — Unique Occurences with rollback DSU (submission). Can somebody explain this method?

As a side note, it seems that all top participants did not use the intended solution for F.

»
5 weeks ago, # |
  Vote: I like it +26 Vote: I do not like it

I've got a significantly shorter $$$O(n)$$$ solution for F.

I'm really bad at explaining, but let me try. The basic idea is similar to the second solution in the editorial. If we split the graph by each edge weight at a time, the answer is the sum of products of sizes of each pair of components that were formerly connected by an edge (neighbour components from now on).

This can be calculated in a single DFS. Now, when we are processing vertex $$$u$$$, which is connected with it's parent with an edge of type $$$x$$$, we're gonna calculate the answer for the pairs of components (the component that contains $$$u$$$, it's "son" components aka all components it neighbours except the one that contains its parent) when split by edges of type $$$x$$$. This value can be calculated pretty simply — the size of the component with $$$u$$$ is the size of $$$u$$$'s subtree — the sum of sizes of subtrees of descendants of $$$u$$$ whose parent edges are of type $$$x$$$. The neighbour components are similar, just shifted by one more level.

In the end we need to explicitly handle the root as if it had all types of edges going upwards.

Hopefully reading the code should clear it up: 158219114

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

I got the logic behind the problem D, but, how to get the idea in the contest that we can traverse to all the states possible than applying the greedy approach of going with the maximum possible, as mentioned in the editorial 'optimal locally' ? Is there any constraint below which we could always apply BFS?

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

code why am i getting RE for problem B

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

Can anyone give me a hint on how can we minimize the number of swaps on Problem C? thanks