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

Автор chokudai, история, 5 лет назад, По-английски

We will hold AtCoder Beginner Contest 217.

The point values will be 100-200-300-400-500-500-600-600.

We are looking forward to your participation!

  • Проголосовать: нравится
  • +134
  • Проголосовать: не нравится

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

Feels like a huge difficulty gap between E and F .

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

    It sure does, at first it seemed solvable but as the time progressed (and 3 WAs) I started to give up. Hope it goes better for the others :)

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

      I felt the same , thought it was solvable , then coded then ran on Sample TC , 2nd one gave WA , realised it is much more tougher than I thought , Didn't made any submission as I knew the solution (even thinking) is completely wrong.

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

    It is a nice exercise to learn range DP and you can also learn it from the USACO guide.

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

      Can you please provide link to the USACO Guide covering such DP idea.

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

      Could you elaborate, as I did use $$$O(N^3)$$$ range DP, but probably wrong transitions.

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

        Let $$$dp_{l,r}$$$ denote the number of ways to pair up all the students between l and r. Now to find the transition between dp states, we consider which student between l and r would be paired up with l. Let this student be x. It should be evident that the number of students between l and x has to be even. Also, l and x has to be on good terms. If the above conditions are satisfied, then we recursively find $$$dp_{l+1, x-1}, dp_{x+1,r}$$$. It should be clear that removing pairs between l+1, x-1, and x+1, r is independent and also the pair removal between l+1 and x-1 has to come before removing the pair l and x. Now, we have to figure out the number of possible orderings of the pairs. Let n1 be the number of pairs between l+1, x-1, and n2 be the number of pairs between x+1, r. $$$n1 = (x-l-1)/2, n2 = (r-x)/2$$$. The number of ways to order all the pairs is $$${{n1+n2+1}\choose{n2}}$$$ (to prove this use stars and bars). Therefore, $$$dp_{l,r} = \sum_{x=l+1}^r dp_{l+1, x-1}*dp_{x+1, r}*{{n1+n2+1}\choose{n2}}$$$ where x satisfies the mentioned conditions, and $$$dp_{l+1,l} = 1$$$.

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

          Can you please give the link to your solution .

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

          Thanks, I missed the stars and bars part and that's why my solution didn't work. I overlooked the fact that if there is a way to remove a segment it takes its length divided by two moves. :(

          Thanks again.

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

            can u explain why we need the starts and bars part

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

              Let's suppose we are calculating $$$DP[i][j]$$$. We need to calculate the number of ways if we decide to pair $$$i$$$ with $$$k$$$.

              Notice these three properties:

              For every segment $$$l, r$$$ the total number of operations done is equal to $$$(r - l + 1) / 2$$$, because we remove 2 elements per every operation.

              To remove $$$i$$$ and $$$k$$$ we obviously have to remove every element that's between them $$$({i + 1}, {i + 2}, ..., {k - 1})$$$ because otherwise they are not adjacent.

              Elements greater than $$$k$$$ are not dependent on the ones $$$\leq k$$$, because we can't pair them with any elements in range $$$[i, k]$$$.

              Combining these facts we can notice that we can independently solve for range $$$[i, k]$$$ and range $$$[k + 1, j]$$$. Let the first range take $$$p$$$ operations and second one take $$$q$$$ operations to completely erase. Since these ranges are independent, it implies that we need to find the number of ways to reorder the given $$$p + q$$$ operations, such that all the operations of type $$$p$$$ can't be distinguished between themselves and the same holds for the $$$q$$$ operations. This results in the provided stars and bars formula.

              Since we need to do this for every possible sequence of operations, we multiply it by $$$DP[i + 1][k - 1]$$$ and $$$DP[k + 1][j]$$$.

              I hope this was clear, if not hit me up and I'll provide an example.

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

          Thanks for this, that's really cool. I was doing the same thing, I was considering the same idea, the only difference was I was considering all pairs of elements in range l to r. Leading to O(n^4) dp. Didn't realize the position of (l,x) only matters.

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

          by doing this

          dp(l+1,x−1)∗dp(x+1,r) we will get the all combinations, can u elaborate why we need the combinations
          (n1+n2+1 n2 )

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

            You can imagine a sequence of $$$n_1 + n_2 + 1$$$ "gaps". You need to fill $$$n_2$$$ of these gaps using the second sequence. The number of ways to do this is $$${n_1 + n_2 + 1} \choose {n_2}$$$. The remaining gaps will be filled by the first sequence followed by the pair $$$(l, x)$$$.

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

          why is dp[l+1][l]= 1 ? why not 0 ?how do you come up with that edge case ?

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

            Consider $$$dp_{l,l+1}$$$ where l and l+1 are on good terms. It should be obvious that $$$dp_{l, l+1} = 1$$$,and by our dp transition shown above, $$$dp_{l, l+1} = dp_{l+1, l}*dp_{l+1, l}*{1\choose 0}$$$ (substituting x = l+1, r = l+1). From this equation, it is clear that $$$dp_{l+1, l} = 1$$$.

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

          Is there any solution with time complexity lesser than O(n^3). Would be eager to know if there is some.

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

      I realized that F was range dp, however still got WA. Can you elaborate on the transitions please?

»
5 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -9 Проголосовать: не нравится

For problem E) sorting queries I used a deque and got a TLE.

EDIT Well, here it is

 deque<ll> d;
 
    for(ll i=0; i<n; ++i) {     
        ll op;
        cin >> op;
 
        if(op == 1) {
            ll x;
            cin >> x;
            d.push_back(x);
        }
        else if(op == 2) {
            cout << d.front() << "\n";
            d.pop_front();
        }
        else {
            sort(d.begin(), d.end());
        }
 
    }
 
»
5 лет назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

I feel like G was easier than F in terms of implementation.

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

    What was the solution of G?

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

      Let $$$dp_{i,j}$$$ be the number of ways to split up 1~i having j groups. Then there are two cases for our transitions:

      1. Make a new group
      2. Add it to an existing group

      For the first case, it is simply $$$dp_{i,j} += dp_{i-1,j-1}$$$. For the second case, we just have to figure out how many groups already contain a person that has the same id in modulo m as i. Obviously, each group can have up to one person that has the same id in modulo m due to the restriction, so the number of "bad" groups is actually just $$$\frac{i-1}{m}$$$, the number of people that have the same id in modulo m as i. So the transition here would be just $$$dp_{i,j} += dp_{i-1, j}*max(0, j- \frac{i-1}{m})$$$. This results in a nice $$$O(n^2)$$$ solution

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

      I broke the solution into three parts:

      • Figuring out the ways to split the numbers into at most $$$k$$$ labeled groups for $$$k$$$ in $$$1,2,...,N$$$.
      • Figuring out how to split the numbers into exactly $$$k$$$ labeled groups by using the above array and inclusion/exclusion (with binomial coefficients)
      • Converting from labeled groups to unlabeled groups (i.e. just multiplying by the inverse of $$$k!$$$).

      I also did this one before prob F after not immediately seeing the DP for F. Fortunately I had time to come back to F and got it working with about 5 minutes to spare.

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

    Yes, I also think that G seemed easier. Switched to it after I noticed that rainboy took less time to solve G than F. Still couldn't make it in time and only got a correct answer for the 2nd sample so far. Trying to upsolve it now.

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

Not being able to solve H is what I get for delaying to learn slope trick.........

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

Any idea how to solve E? I used set for the sorted items and queue for the unsorted. Implementation was full of bugs I suppose as I got 8 WA.

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

    You should have used Multiset

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

    use multiset and queue

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

    Maintain a queue of elements that are inserted and two sets (one for the sorted part and the other for the non-sorted part). When sorting query is applied, just use small to large merging and the rest two cases are pure implementation and case work in 3-4 lines.

    EDIT: Small to large merging is not needed lmao. IDK why I opted to use it, but as others described you can just do single multiset and queue.

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

    I think I did what you are describing. Basically, use a set to maintain the prefix of the sequence that is sorted, and a queue for the rest. For type 1 queries, you either add an element into the set if the queue is empty and the element is greater or equal to the highest value of the set, or you add it to the queue. For type 2 queries, you either remove the smallest element from the set or if the set is empty, just remove the front element of the queue. Lastly, for type 3 queries, you simply insert all the elements in the queue into the set. Make sure to use a multiset

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

how to solve F?

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

    Consider a subsequence l,r

    if l and r are a good pair we can solve pair l,r, then the seq l+1,r-1.

    And, we can solve l,l0 (in (l0-l)/2 steps), then l0+1,r

    We need to combine all these combinations, with stars'n'bars formular.

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

solved first 5 in 30 min then kept staring the screen for the rest 1 hour 10 min.any idea how to solve F.

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

I'm shocked to see so many people solving H and in such a short time. Is maintaining a convex function really popularized nowadays or does it have easier solutions?

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

Samples for F were poor.

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

Can anyone tell me when will the editorial be posted? There's no editorial last ABC, either. This ABC is a really exciting one! I got 5 questions right!

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

How to solve F and G? Any ideas? Help me!

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

    F: dp[L][R]: answer to the problem when you start with students numbered L, L+1, ..., R-1. (R-L must be even). dp[1][2N+1] is the answer.

    For transition, iterate over all possible way of pairing student L. Let that student be M. We can't have any student between L and M paired with a student outside of the range [l, M]. Therefore, we can independently count those two separate range, and merge.

    G: For fixed, k, we can solve it in O(k) with PIE. If we require z (0<=z<=k) groups to be empty, there are k choose z ways to choose such groups. Looking at a specific remainder R modulo M, there are either floor(N/M) or floor(N/M)+1 numbers with remainder R mod M. Each of those numbers contribute to the answer the exact same way. Do little bit of combinatorics to count the exact contribution.

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

Source Code

Why TLE in E?

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

Still wondering how in the world authors decided that both E and F are worth 500 points.

»
5 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +1 Проголосовать: не нравится

I used segment tree to solve E

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

Why is my solution giving TLE for Problem E?

Query 1 and 2 takes O(logN) while query 3 Takes O(N) to copy the elements .

Link : https://atcoder.jp/contests/abc217/submissions/25610922

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

Nice contest! I love problem D and E, they are tricky. And I found a lot of different solutions of E, they are of great educational value, and I have gained a lot.

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

set.upper_bound(element) is much faster than upper_bound(set.begin() , set.end() , element)

Today while giving AtCoder Beginner Contest 217, I noticed that set.upper_bound(element) is faster than upper_bound(set.begin() , set.end() , element). During the contest I used upper_bound(set.begin() , set.end() , element) and got TLE. But after the contest I resubmitted my code with set.upper_bound(element) and got AC.

contest link : https://atcoder.jp/contests/abc217/tasks

Problem link : https://atcoder.jp/contests/abc217/tasks/abc217_d

TLE solution link : https://atcoder.jp/contests/abc217/submissions/25614807

AC solution link : https://atcoder.jp/contests/abc217/submissions/25614873

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

    This is because upper_bound does not take advantage of structure of sets, as far as I know, upper_bound is implemented in a way such that it acts like binary search in an array but in sets elements are arranged differently so binary search doesn't works here and the function need to go for every element in order to return the desired result, giving worst case complexity of O(N). While set.upper_bound is implemented in a different way, so it works faster in O(logN).

    Same is true for lower_bound and set.lower_bound too. :)

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

ABC's give us some really cool classical DP, which are really rare in other contests nowadays.

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

Can someone tell in E how solutions using a priority queue(min heap) isn't getting TLE? When sorting is needed, I see the solutions moving all elements from a queue\list into the heap. Isn't the complexity now q*( q log(q) ) with q=2e5 and q*log q for inserting all elements in heap?? How does that passes??

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

    There can't be more than Q elements total to be inserted into the heap during the whole lifetime of the program. The amortized time complexity is $$$O(Q\ log\ Q)$$$.

»
5 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится -8 Проголосовать: не нравится

The questions D) and E) on this contest are mainly about using functions from data structures.

For D, I used a map to sort the marked cuts and retrieve the two marked cuts surrounding each query. My solution

For E, I used a map to record the front-and-sorted section of the sequence, and a queue to note the parts after the sorted section. Here's my solution

These problems can help new programmers to understand and learn more about functions and uses of Maps and other similar Data Structures.

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

Video tutorial for problem A, B, C, D, E : https://youtu.be/seoYYZiZL-0

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

Problem F

I didn't update DP data in the way explained in official editorial. My solution did it like this:

$$$dp[i][j]$$$ is the number of ways to tackle all students in segment $$$[i, j]$$$. Let $$$l = i - j + 1$$$. It is obvious that $$$l$$$ must be even.

  • Case 1, the $$$i^{th}$$$ student is paired with the $$$j^{th}$$$ student. It is possible only when the segment $$$[i + 1, j-1]$$$ has been tackled, and the two are on good terms.
  • Case 2, the whole segment is devided into two seperate segments, that their operations can be implemented respectfully. Let the length of the first sub-segment be $$$2k$$$. We pair all students in $$$[i, i + 2k - 1]$$$ and $$$[i + 2k, j]$$$.

Therefore $$$dp[i][j] = dp[i + 1][j - 1] * friends[i][j] + \sum\limits_{k=1}^{l / 2 - 1}{dp[i][i + k * 2 - 1] * dp[i + k * 2][j] * C_{l / 2}^{k}}$$$.

I wonder if there are any mistakes in my solution. Have I overlooked anything? Here is my submission.