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

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

Hello CodeForces community,

SnackDown 2017 Pre-Elimination Rounds have finally concluded. We saw over 12K teams joining us for the Pre-Elimination Rounds, out of which 3403 teams (who were in top 1000 ranks of each of the Pre-Elimination Rounds) have been qualified for the Elimination Round.

Now, while we prepare to step into the Elimination Round, I would like to introduce the problem setting panel for the contest.

Problem setters: arjunarul (Arjun Arul), kingofnumbers (Hasan Jaddouh),Pepe.Chess (Hussain Kara Fallah), nssprogrammer (Snighda Chandan), Mediocrity (Fedor Korobeinkov), mgch (Misha Chorniy), fudail225 (Fudail Hasan)

Problem testers: kingofnumbers (Hasan Jaddouh), mgch (Misha Chorniy), Mediocrity (Fedor Korobeinkov), Alex_2oo8 (Alexey Zayakin)

Translators: Mandarin : huzecong (Hu Zecong), Russian : CherryTree (Sergey Kulik), Vietnamese : VNOI team.

Contest Admin: PraveenDhinwa (Praveen Dhinwa)

I thank each and everyone of them for their contribution in preparing the problem set.

Given below are the details of the contest

Additionally, we are also conducting a replay round of the Elimination Round for the rest of the community. It will take place at https://www.codechef.com/SNEL17RP and the top three winners of the contest will win cool SnackDown 2017 t-shirts. I would take this opportunity to invite the entire community to come and join us for the Replay of SnackDown 2017 Elimination Round.

Details of the SnackDown 2017 Elimination Round Replay:

Accepted Languages: https://www.codechef.com/wiki/list-compilers

Note: The Elimination Round will follow ACM ICPC style ranklist.

We hope you will enjoy the problems and we welcome your feedback in the comments below.

Good Luck!

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

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

How many problems will there be?

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

n = 1 case in Add or Multiply problem... I got 10 WA for that!

I solved 2 problems, what do you think, is this good for my color?

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

Just curious, were tourist and qwerty787788 participating both or just one of them? or maybe participating while driving? (just curious) :D

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

Replay round in tomorrow, so we can't discuss and no editorials, right?

EDIT: We can look at submissions anyway

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

How to solve ANCESTOR ?

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

    Hints:

    1. Calculate the eulerian tours of both trees

    2. Now the problem is like finding intersections of prefixes

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

    It's like a euler tour on trees technique. First, So we keep starting and ending time of each node in the first tree. Now, when we are doing dfs on the second tree, so while visiting a particular node say x, we will add 1 to all the nodes who are in the subtree of x in the first tree, i.e add 1 to all the nodes whose starting time  >  start[x] and ending time  ≤ end[x] [Remember start[x] and end[x] are for the x in the dfs of first tree]. We can do this with BIT, and when we're done with dfs of x in second tree, we will reverse the operations i.e. add  - 1 to the above stated nodes for x. So, answer for a particular node i will value of 1's added to start[i].

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

    I have a little different approache for the task. We can calculate eulerian tours for both trees, than we will have four different times for each node : (start1, end1), (start2 , end2) — coming and leaving times for both trees. Now let's represent each node as point in the plane with coordinates x=start1 and y = start2. We have n points in total. Also we can represent recetangle with left-bottom point (start1, start2) and right-up point (end1,end2) for all nodes x. All points (nodes) in the recetangle will have x as ancestor in both trees.

    Now we need to do inverse task — calcualte amount of recentagles which contains certain point and this is known problem.

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

It looks like this problem: https://www.codechef.com/SNCKEL17/problems/SPCLN is almost an exact copy of this problem: https://www.codechef.com/problems/RIN

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

Approach for Waiting in Queue, anyone?

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

    Submit on Codeforces

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

      Where on codeforces? We've finished our source shortly after the end of the competition (well, not so shortly, maybe 25 minutes) and I'm curious whether it passes or not. However, I don't understand why there were so few AC sources. Our idea involved just a treap and some tricky information stored in it (it wasn't trivial), but still, there were much harder problems (from the point of view of their ideas) solved by more teams. Anyway, the problems were interesting and most of them were really nice. About the problem lewin has pointed out I'm not that happy, but apart from that, the contest was a complete success. Congrats!

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

        In my opinion it was more like implementation problem for people familiar with treap and can do lazy propagation.

        Store in treap not bi, but bi - i. What job will be completed first? The job with lowest index, such that bi - i ≤ 0.

        If you store in each node minimum of subtree, you can determine such node by recursively going down (recursive function: if there is a possibility to go left and minimum in left is small enough, then go there; otherwise try middle element; if still not go right)

        What to do when you found such node? Delete it, add some const to all elements before this node (so that it matches definition bi - i, add some const to all elements after this node, and rotate the array cyclically.

        One more case. If the whole array is greater zero, then you should advance time by few whole rotations. Thing like the minimum divided by size of treap and ceiled up will do the trick.

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

          That's exactly what we did. I think it was the only solution. However, we didn't need any sort of lazy propagation. Just storing minimum of bi — i for a subtree in the treap was enough. It's just that we considered what problems to think about next by looking which one was solved by more people and we let this one at the very end of the competition. When we read it, the solution was obvious but we didn't have time to solve it. I guess that could be a good lesson: not to judge a problem after the numbers of accepted solutions

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

    Suppose at time moment t the queue contains people with arrival times b0, b1, ... bn - 1. If b0 ≤ t then first customer in queue will be served now, otherwise if b1 ≤ t + 1 then first customer will be moved to the end of queue and second will be served now, ..., if bn - 1 ≤ t + n - 1 and bi > t + i for all 0 ≤ i ≤ n - 2, then n-th person will be served now and all others will be moved to the end of queue, preserving their order.

    If bi > t + i for all 0 ≤ i ≤ n - 1, then in seconds from t to t + n - 1 inclusive everybody gets moved to the end of queue and situation just repeats at moment t + n.

    So, what happens with queue? If , then everything comes full circle and t is increased by n. Otherwise, we need to find the leftmost position j, such that bj - j ≤ t, process customer at that position and move everybody before that position to the end of queue (preserving order).

    How many full circles will happen (let's call this number k)? If (if M ≤ t then k = 0), then exactly full circles will happen. To simulate them, just increase t appropriately.

    Now only the case when M ≤ t bothers us and we want to find smallest j such that bj - j ≤ t quickly. To do this, let's keep queue in implicit treap and in each vertex remember minimum value of bi - i in its subtree. Idea of remembering something that changes when we do operations on tree sounds kind of dangerous, but actually it is completely ok in this case. When we do merge and split we just need to do an update operation to some vertex (where info is no longer correct, but info for it children is up to date), we need to just decrease minimum in right subtree by 1 + (size of left subtree) and bi - i for root is just b - (size of left subtree). So this kind of information can be keeped while we do basic merge and split by length operations.

    To find smallest j that bj - j ≤ t, just do tree descend and check whether minimum of bi - i in subtree is still less than t. After that serve this customer, delete him and move all customers that stand before to the right (by splitting them out from the left and merging them on the right).

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

      Do we really need treap?

      When we delete a node x, we should add one to the values of by - y for all y > x, and set bx - x to infinity. Can't we do this with a segment tree?

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

        How can you find "first good" node, if order changes after each deletion?

        UPD: Oh, I see. If you already have previous element, you already know where new queue begins. So you can just find "first good one" at suffix after previous element first and on prefix after.

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

I'd be glad if someone could help me hack this code. here

I've first checked if all 4 points are collinear, then if 3 points are collinear. (Solution is trivial in these cases)

After that, I've checked if any of the point lies inside triangle formed by other 3 points. In this case, answer should be "NO".

After that, I've solved the problem for points which do not form parallelogram. (Take two points. Say points are a,b. Other points are c and d. Check if there exists any point e, such that c lies on a-e and d lies on b-e or vice versa)

For parallelograms, extend a-b by some margin, and do as mentioned in case of non-parallelograms.

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

Are Bangladeshi contestants allowed to have t-shirts if their rank is below 301?

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

How to solve the first problem ? PLUSMUL?

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

How to solve ROBOTDAG?

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

    Binary search + max flow inside it

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

    The constraints for this problem are tricky somehow. Especially when you first think of Max Flow. Create n versions of each node. Each node will have an extra copy for every time second. and for each edge u->v add it for all nodes [u][s] -> [v][s+1] where s is a second ranging from 1 to n. I think most people got AC with binary search, but you can throw throw out binary search and add edges to the sink representing each second while going on and after adding each layer call your augmenting path function. I think this runs in K * N * M. But in particular it's kind of impossible to force maxflow algorithms to reach worstcase.

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

    We can create a new graph where each node (i, j) represents that we are on node i at time j. Now when we are creating edges , we should create edges from a nodes (u, t) to (v, t + 1) if (u, v) edge exists in our original graph. So, our initial problem is transformed into finding the edge disjoints paths in our new graph which is a standard problem. Now we can binary search on minimized maximum length by creating a graph in which the nodes are stated as above and t is the length we want to check. To check for a particular length, we'll need to check if the total disjoint paths in our new graph  ≥ k and change the limits of binary search accordingly.

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

Prefix XOR: in the problem statement, we're asked to answer queries online rather than offline. But why? The solution is absolutely the same as for the offline version (precompute longest sequence for each position, then answer queries using a segment tree), except that one has to use persistent segment tree instead of segment tree. Is it a test for knowledge of "modern" data structures/googling skills/prewritten code?

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

Where can i find tests for the problems?

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

Can Chef and Pairs be solved using ternary search( finding x using ternary search )?

I know we need strictly increasing and later decreasing, but still is there any way to find extrema in function which may not be strictly increasing and then decreasing, but just increasing and then decreasing.

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

I really enjoyed problem about black subgraphs. A really cool one. Still, I don't understand why so many teams solved it. It was two leagues harder than the damn "copy paste persistent tree" problem.

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

What were the tricks with the "4 points" problem?

We considered the following and ran on randomly generated tests, but still WA.

4 collinear points — choose the furthest points pairwise to form a triangle, and any other given point in the triangle. 3 collinear points — the furthest two points out of 3 forming a triangle, and the fourth point. 4 points forming rectangle (answer is NO). One point inside a tiangle formed by the other 3 points (answer is NO).

Bruteforce permutations of 4 points. Calculate intersection point between 2 lines formed by these points. Choose 2 points in each segment for the triangle and the intersection. Check if the specified conditions were met — namely (a) all points are on the triangle sides; (b) intersection coordinates are within the bounds.

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

    4 points forming rectangle (answer is NO)

    it is not correct answer is "YES" in that case.

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

    There is answer for a rectangle... and it's kinda easy. The only case without answer is when one point is inside triangle made of 3 other. I had such cases: 3 (or more) collinear points, points forming a parallelogram, other cases. It's easy to consider every of them separately, hardest thing was about "find intersection of two lines".

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

    I tried to minimize the number of cases, so here's what I did :

    If three of the points are collinear, sort them by increasing order of x-coordinate, then y-coordinate. Let's say the points are P0, P1, P2 in this order. Now, we just take P0P3P2 as our triangle.

    Otherwise, build the convex hull of the four points. If the size is at most 3, the answer is NO. Otherwise, we have the points A, B, C, D as the vertices of a convex quadrilateral, in this order. In this case, take the lines AB, AD and the line parallel to BD passing through C as the sides of our triangle. The vertices are just the three intersection points of these lines.

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

Can someone please tell me the recurrence relation required to solve the first problem PLUSMUL

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

In problem BLACKCOM, how to do that DP ? I have no idea how to do it in O(N^2).

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

    A similar problem to this is problem "Barricades" from Algorithmic Engagement 2007. You can find a solution in the first half of the "Looking for a challenge" book (which you can download here). The book explained why the DP is amortized from O(n3) to O(n2) (which is the same case for this problem).

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

    I guess the estimation of time complexity for your DP is not strict.

    Let T(x) be the time complexity of a DP with x nodes. If you write a DP whose time complexity is like T(L+R) = T(L) + T(R) + L*R (Somehow you can convert the tree where some vertex could have three or more children into as a equivalent binary tree), the time complexity is O(N^2) in total (N is the number of nodes) as xuanquang1999 said.

    Why? Let's assume T(x) <= x^2, then T(L+R) = T(L) + T(R) + L*R ≦ (L+R)^2 = L^2 + R^2 + 2*L*R. You can find this is obviously true by comparing each term in the left side and the right side.

    This fact is a little amazing. I can't understand it intuitively actually.

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

      Let's introduce the potential function as the number of pairs of vertices in the same connected subtree. At first, the number is equal to 0 (all vertices are in distinct subtrees). In the end, the number is equal to .

      If you connect two trees of size L and R, respectively, you increase the potential by LR, which is proportional to the time of merging the trees. Thus, the total runtime is proportional to .

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

    Straight-forward DP has complexity N^2. You can read cheater2k's very simple explanation here in Vietnamese.

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

How to solve Chef and Pairs.

My approach was that as we are moving the points from left to right the maximum matching will increase and then decrease , but there can also be plateaus . Also I am not sure about the above claim . Can someone share their approach .

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

    That property of increasing and then decreasing doesnt hold !!

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

    Sort the arrays a and b.

    The problem is equivalent to finding the maximum matching of arrays a and b such that if m, M are the minimum and maximum value of A - B where (A, B) is a pair in the matching, then M - m ≤ 2y.

    My idea is since n and m are small, I can brute force all possible (i, j) so that we pair ai, bj and m = ai - bj. For each such pair, I can find the maximum number of pairs I can still make without violating m ≤ a - b ≤ m + 2y in O(n) time by greedy.

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

My team solved 4 problems and ranked 251. Nice contest

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

How to solve MEXDIV ?

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

    My solution : For each position i, find the leftmost position l such that the mex of the subarray [l..i] is at most k. Note that the mex is larger than k iff all numbers from 0 to k inclusive has appeared in this subarray. Note that if [l..i] has mex larger than k, then [l..i+1] must also have mex larger than k, so the sequence of leftmost position is non-decreasing. We can compute this leftmost position for each i with the help of two pointers. Call the leftmost position for position i as L[i].

    Once we build the array L, we just have to do a simple dp. dp[i] is the number of ways to divide the first i elements into subarrays satisfying the condition. dp[i] = dp[i - 1] + dp[i - 2] + ... + dp[L[i] - 1], and the latter can be computed in O(1) time if we maintain the prefix sum for dp[i].

    Complexity : O(n).

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

How to submit?

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

Top 3 teams will win cool SnackDown t-shirts.

You are kidding, right? Solutions were made public after contest.

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

    Hi, We noticed that we have missed making the solutions of the elimination round private. Now we are already half way through the contest and the damage is done. Keeping into consideration of the series of events and the spirit of the competition, we are calling back the prizes (of the replay round) that were announced earlier. We shall be more diligent in our future events and we sincerely apologize for the inconvenience caused. Thank you for bearing with us.

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

Regarding problem CHEFPRAD

I am not sure why no one mentioned this issue yet, but the "expected" solution is O(N * M * (N + M)) per test case. Given there are 10 test cases, this amounts to 3·108 ops, which seemed pretty marginal for 1 second TL to me(given CC servers performance).

I was pretty surprised when my solution with that complexity passed in 0.11 secs(clearly CC servers must be fast and I wasted 1 hour trying to optimise my solution for no reason), but I made some random large test data and it was quite easy to get my solution to get TLE on codechef ide and ideone.com(which is also on sphere engine).

Here is rng_58's team's submission which takes 1.74 secs to run on the test, and team rhaegal's submission which takes 3.36 seconds(!)

I do not blame the contestants for writing slow solutions, but my question is why, if this was expected solution, are the test cases so weak(and TL so tight), and in case it wasn't the expected solution, why do these pass?

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

    We had the same issue , our solution got TLE and we wasted time thinking a better logic. In the end , just changed vectors to arrays(optimized the constant basically) and it passed -.-

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

    Getting 3 * 10^8 operations in a second is not entirely unreasonable. Also, in this problem it is not easy to create test data to force this up. We had the slowest solution running in 0.23 secs.

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

      I thought codechef servers would TL that idea and wasted time trying to code N*M complexity.I feel there exist N*M soln.Is it right??

      So finally here comes the question how many operations can we expect to be done in 1sec on codechef(assuming basic addition and multiplication with input size around 10^5 ).

      And If that was the idea then what was the intention behind putting test cases around 10. Was it to not allow bit slow solutions with same idea??

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

      (replying after revision 2)

      Well, it's safe to say your generation was not strong enough and the test-data is very weak.

      Besides, is there even a genuine reason to have 10 test cases per file? I don't see any way it would affect the problem, having 5 cases should be enough to get anything slower TLE.

      I had written a very simple C++ rand() using generator: code

      I've tried 10 different manual seeds, each takes > 1.65 secs on rng_58's teams submission, and > 3.1 secs on rhaegal's submission.

      I know this contest is over and nothing can be done, but please don't add such un-necessary complications in problems in future that just spoil the main part of the problem. :(

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

        Not sure if this is relevant. But I have tested problems for a private contest on codechef before, and my experience was that codechef IDE seems to be significantly slower. i.e, my solution passed on the task, but it was too slow on codechef ide.

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

Any updates as to when would the selected indian teams be announced?

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

When will editorials be uploaded ?

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

Please look into my code for MEXDIV and get me the frustrating bug I am unable to find.

https://www.codechef.com/viewplaintext/14092592

Thanks!

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

Now That Both The Contest And The Replay Have Taken Place , Can You Please Make The Solutions Visible And Release The Editorials :)