vovuh's blog

By vovuh, history, 5 years ago, In English

1195A - Drinks Choosing

Idea: budalnik

Preparation: budalnik

Tutorial
Solution

1195B - Sport Mafia

Idea: ?

Preparation: MikeMirzayanov and cdkrot

Tutorial
Solution (binary search)
Solution (formula)

1195C - Basketball Exercise

Idea: meshanya

Preparation: tsarn

Tutorial
Solution

1195D1 - Submarine in the Rybinsk Sea (easy edition)

Idea: MikeMirzayanov

Preparation: MikeMirzayanov

Tutorial
Solution

1195D2 - Submarine in the Rybinsk Sea (hard edition)

Idea: meshanya

Preparation: sava-cska

Tutorial
Solution

1195E - OpenStreetMap

Idea: budalnik

Preparation: ima_ima_go

Tutorial
Solution

1195F - Geometers Anonymous Club

Idea: craborac

Preparation: budalnik

Tutorial
Solution
  • Vote: I like it
  • +30
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

For G, can you explain how "count the number of distinct values on the given segment of the given array" can be solved with persistent segment tree?

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

    I googled "the number of distinct values on segment persistent segment tree" right now and the answer is in the first link.

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

    As an aside, we can actually solve this problem offline using a standard segment tree. Sort the queries (L, R) in increasing order of R. Then, we apply a similar idea to two pointers. The segment tree stores the number of values we've seen most recently within the range corresponding to each segment. (That is, position i in the segment tree contains the number of values we've seen most recently in polygon i, and based on this data we can compute the remaining values as a standard sum segment tree.)

    For each query, start by updating the tree for any polygons up to position R that we haven't processed yet (i.e. with index greater than the R of the last query we processed). For each value in the polygon, subtract 1 from the segment tree position corresponding to this value's last appearance and add 1 to the node corresponding to the polygon's location. Then, we can get the answer by performing query(L, R) on the segment tree, as this computes the number of values we've seen at least once at or after position L.

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

      Yes, this idea was used in two author's solutions and actually you can replace segment tree with BIT (fenwick tree) as far as I know.

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

I'm unable to understand the tutorial provided for problem C.

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

    I am sorry to say but it is very unlikely for most people to understand that editorial unless you are already comfortable with the concept of dynamic programming and already know how to apply dynamic programming to solve problems. It takes some time to understand dynamic programming. Like you must have taken some time to really understand recursion. Dynamic programming is one step ahead of recursion. I would say dynamic programming is unleashing the true power of recursion. So, I would suggest you to learn and practice some dynamic programming problems before trying to solve the problem C. :)

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

    Let h[1][x] be the height of the x-th student in the first row, and h[2][y] be the height of the y-th student in the second row (x, y <= n)(our arrays are 1-indexed). Let we see what we'll have to do if we already have maximal sum for the first i-1 columns. What can we do in the i-th column? So, we can add h[1][i] if and only if we took the element from the second row (h[2][i-1]) or we didn't take any element from the previous column. Similarly, we can add h[2][i] if and only if we took the element from the first row (h[2][i-1]) or we didn't take any element from the previous column. Or, we can only skip the i-th column(we do not take any element from the i-th column). That can be done using dp method. Let dp[0][i] be the maximal sum for the first i columns, if we didn't use any elements in the i-th column, dp[1][i] be the maximal sum for the first i columns, if we took an element from the first row in the i-th column, and dp[2][i] be the maximal sum for the first i columns, if we took an element from the second row in the i-th column. Now, we can only iterate through the all possible values of i (from 1 to n) and do the following: 1. dp[0][i]=max(dp[0][i-1], dp[1][i-1], dp[2][i-1]) 2. dp[1][i]=max(dp[0][i-1], dp[2][i-1])+h[1][i] 3. dp[2][i]=max(dp[0][i-1], dp[1][i-1])+h[2][i] Our final result wil be max(dp[0][n], dp[1][n], dp[2][n]). So, we can do this in time complexity O(N), and memory O(1), but memory O(N) (with the dp array, which I used here) is also good.

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

      Thanks for your great explanation! But I still have one small question left. You said, that we can take h[1][i] if we didn't take nothing in the previous column (and same for h[2][i]), but how is that if we can't take 2 players from the same row? Clearly we can take h[1][i] if and only if the last taken player is h[2][j] and vice versa for h[2][i]. How can we be sure that after adding h[1 or 2][i] to dp[0][i-1] our rule to choose from different rows won't break?

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

        I am not sure what you are exactly asking about, but I can try to explain: We can take h[1][i] if we did take h[2][i-1] or if we did not take any elements from the previous column (if our last taken element is from the j-th column, where j<=(i-2)). The same thing with h[2][i]. We are sure that we didn't breach the rules because, in dp[0][j] we store the maximal sum for the first j columns, but we did not take any element from the j-th column.In dp[1][j] we store the max sum for the first j columns but we took the first element from the j-th column in our maximal value (we took h[1][j]), and vice versa for dp[2][j]. If I didn't answer on your question, sorry, please try to explain it better, I'll try to answer.

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

For F I am trying following — First I calculated number of distinct slopes till i'th polygon. Then for each slope store the polygons which contains this slope using stack such that lower index are at top. Solve queries offline in increasing order of left endpoint of queries with the help of segment tree which does operations — 1. Subtract on a segment & 2. Query for a point. When we reach a polygon we answer queries just by quering point and after that subtract -1 from [i,next_id] where next_id = next polygon which contain same slope as that of current polygon.

I recieved TL 5 for that solution which I think is O(qlog + nklog). Can someone help in finding the complexity of my algo — 57289296

UPD: ACed it. Just store the next index with same slope.

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

In (c), I took a two state dp where dp[i][1] shows that last element included is i and is from 1st row. dp[i][2] shows the similar thing for row2. Now dp[1][1]=arr1[1]//first element of first row dp[2][1]=arr2[1]//first element of second row. Then //L1 if(dp[i-1][1]+arr2[i]>dp[i-1][2]) dp[i][2]=dp[i-1][1]+arr2[i] else dp[i][2]=dp[i-1][2]// I simply excluded that element //L4 Implemented similar thing for dp[i][1] and I got an AC. But now looking at the editorial and other topcoders' solution,everybody implemented using 3 different states, I am confused. Was my implementation correct or the test cases weren't strong enough?

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

Can someone explain the formula used in problem D2 ??

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

    it tells you to calculate the contribution of each number for each length by creating the number which would have been made if the given number was present on left side of the function and also if the number was present at the right side of the function. the difference arises in condition when length is not equal.you will have to create that number by getting the example in the question

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

Problems were fairly standard, except last one, but I liked it. Good test samples with edge tests.

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

I think there's a typo in the explanation of B. It should be $$$\frac{x(x+1)}{2}-(n-x)$$$ instead of adding $$$(n-x)$$$.

»
5 years ago, # |
  Vote: I like it +8 Vote: I do not like it

In Problem G, why does not this situation appear: In the resulting polygon, three(or even more) sides intersect at one point so that the number of points decreases by 1.

Can anyone give an explanation? Thanks in advance.

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

    it will never happen as Minkowski addition always does vector addition of sides of two or more convex polygons in a manner such that the resulting polygon is always convex. you can read more about Minkowski addition here for more clear understanding.

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

IMHO, this round really looks like the educational one: formula, dynamic programming, binary search, "quite typical task" with minimums.

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

In problem B, the second solution(fomula) should be

round(n + 1.5 - sqrt(2 * (n + k) + 2.25))

It should be 2.25, not 2.75

Answer = n - (-3 + sqrt(9 + 8(k+n))) / 2  
       = n + 1.5 - sqrt(2.25 + 2(k+n))
  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    how did you come up with this formula: Answer = n — (-3 + sqrt(9 + 8(k+n))) / 2

    and how did this result in _ = n + 1.5 — sqrt(2.25 + 2(k+n))_

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

      From the formula given in the tutorial:

        x(x + 1) / 2 - (n - x) = k
              x(x + 1) / 2 + x = k + n
             (x² + x + 2x) / 2 = k + n
                       x² + 3x = 2(k + n)
            x² + 3x - 2(k + n) = 0
      

      Using the formula

       x = (-b ± sqrt(b² - 4ac)) / 2a
      

      We can get

       x = (-3 + sqrt(9 + 8(k+n))) / 2
      
  • »
    »
    5 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    I WASTED LIKE 5 MINUTES THINKING ABOUT THIS

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

Can't see the Tutorial B :"Unable to parse markup [type=CF_MATHJAX]"

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

Can someone explain D solution? I don't get it at all

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

i solved problem B C D1 D2, still reading problem A and still don't understand.please translate me.

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

Interesting Problem E

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

Is Problem F's description of the example wrong?

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

Can somebody help me with the precision on F? I am getting WA on test 3 and my answers are pretty far off.

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

Problem E: OpenStreetMap is tough on the JVM architecture. The boxing of integers inherent in STL lists and deques will cause TLE. I basically had to implement my own deque, backed by primitive arrays, in order to pass, but once I did, I easily pass in <900 ms

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

Can't the problem Div2 E be done using two pointers and a c++ stl set. we put element and remove element from set in same way as described by queue

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

Can someone why my code for C is failing? I think I've used the same logic as the editorial, using memoization instead of tabulation.

117470118