awoo's blog

By awoo, history, 3 years 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

| Write comment?
»
3 years 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).

  • »
    »
    3 years 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.

»
3 years 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 :))

»
3 years 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 !!)

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

Finally I became a cyan !!

»
3 years 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

  • »
    »
    3 years 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.

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

      i did selection sort

    • »
      »
      »
      3 years 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.

      • »
        »
        »
        »
        3 years 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

        • »
          »
          »
          »
          »
          3 years 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.

        • »
          »
          »
          »
          »
          3 years 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);
          
          • »
            »
            »
            »
            »
            »
            3 years 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

»
3 years 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!

»
3 years 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?

  • »
    »
    3 years 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.

»
3 years 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?

  • »
    »
    3 years 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 .

  • »
    »
    3 years 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.

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

      Yeh Sure, lingfunny !

      • »
        »
        »
        »
        3 years 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.

        • »
          »
          »
          »
          »
          3 years 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)  
          
»
3 years 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?

  • »
    »
    3 years 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.

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

      Where is this section?

      • »
        »
        »
        »
        3 years 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

»
3 years 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?

  • »
    »
    3 years 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

»
3 years 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

»
3 years 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).

»
3 years 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?

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

    +1

  • »
    »
    3 years 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

    • »
      »
      »
      3 years 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 :(

»
3 years 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://mirror.codeforces.com/contest/1681/submission/158267779

»
3 years 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.

»
3 years 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.

  • »
    »
    3 years 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.

    • »
      »
      »
      3 years 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.

»
3 years 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

  • »
    »
    3 years 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
    
»
3 years 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.

  • »
    »
    3 years 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.

  • »
    »
    3 years 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.

»
3 years 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?

  • »
    »
    3 years 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

»
3 years 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.

»
3 years 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

»
3 years 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

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

    If you use bubble sort you will do at most n*(n-1)/2 swaps. If you construct your algorithm by using bubble sort twice, at most it will do n*(n-1) swaps. For n=100 and a threshold of 10^4, it's more than enough.

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

In code for problem D. Why we are not updating the dist[w] with dist[k] + 1 when w exists in the map?

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

    As we only need the shortest distances, if we have already visited a node, we won't visit it again.

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

Hi! For the Problem D, could anyone tell why the first one gives WA, but not the second? The first uses a !map[x], while second uses !map.count(x). Why do these behave differently?

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

Alternative Approach for Problem D

Instead of simply choosing the largest digit to multiply with the number, I opted to select a digit from among the first log2(x) largest digits. Reasoning:

For Smaller Values of x: When x is small, log2(x) is also small. This results in fewer recursive calls as the search space is reduced.

For Larger Values of x: Although log2(x) increases for larger x, selecting digits from this range allows the answer to be reached faster.

This approach surprisingly works well in practice!

Submission Reference: 295392140