Vovuh's blog

By Vovuh, history, 3 months ago, In English,

1195A - Drinks Choosing

Idea: Nebuchadnezzar

Preparation: Nebuchadnezzar

Tutorial
Solution

1195B - Sport Mafia

Idea: ?

Preparation: MikeMirzayanov and _kun_

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: Nebuchadnezzar

Preparation: ima_ima_go

Tutorial
Solution

1195F - Geometers Anonymous Club

Idea: senek_k

Preparation: Nebuchadnezzar

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

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

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

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

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

»
3 months ago, # |
  Vote: I like it +2 Vote: I do not like it

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

  • »
    »
    3 months 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. :)

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

      I don't know if I'm correct but dpi,3 the same but we didn't take any student from position i−1 should be dpi,3 the same but we didn't take any student from position i. Then the solution actually makes a whole lot more sense to me. Am I correct ?

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

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

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

»
3 months ago, # |
  Vote: I like it -15 Vote: I do not like it

here is explanation of problem C btw its not my channel https://youtu.be/0E9bj0CLOo4

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

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

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

Can someone explain the formula used in problem D2 ??

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

my solution for problem A fail after the system test can anyone tell me what is the mistake ,i just greedily consider the frequency of type of drink by taking the max occurrence and see if the number of the set that we have took already is less than the max number of set that we are allowed to take here is the code https://codeforces.com/contest/1195/submission/57215601

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

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

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

    Even i think that's a typo ! And why are they specifically computing ((n-m)*(n-m+1)/2)-m > k and not using (m*(m+1)/2)-(n-m) < k and change the if part accordingly ! Is there any specific reason ?

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

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

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

»
3 months 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))
  • »
    »
    3 months 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))_

    • »
      »
      »
      3 months 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
      
  • »
    »
    3 months ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    I WASTED LIKE 5 MINUTES THINKING ABOUT THIS

»
3 months 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]"

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

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

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

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

Any similar problem like C for practice as written in editorial, C was standard problem of dp Thanks...

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

Interesting Problem E

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

Problem E is not clear to me. Can anyone please explain problem E more clearly?

and also help me to understand Test Case 2

3 4 3 3
4 4 0 5

How ans of this test case is 2?

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

    after constructing the matrix according to the formula gi=(gi−1⋅x+y)modz the matrix will be:

    • 4 1 4 1
    • 4 1 4 1
    • 4 1 4 1

    now what you need is to find all the sub-matrices of size 3 * 3, which are:

    • 4 1 4
    • 4 1 4
    • 4 1 4
    • 1 4 1

    • 1 4 1
    • 1 4 1

    both sub-matrices have a minimum of 1 which leads to 2 as the ans

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

Is Problem F's description of the example wrong?

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

In problem B, using Binary Search,

if ((n — m) * (n — m + 1) / 2 — m > k) ## Doubt l = m; else r = m; how the condition is formed ?? can anyone please explain??

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

Hi Everyone,

I was referring to your the editorial of Sports Mafia Problem.

My question is that how can we guess that it will be solved by binary search?? What the hunch??

Thanks

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

can somebody upload the solution of the problem c in python using dp im not able to understand the one in the editorial

»
2 months 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.

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

I'm a beginner , can anyone explain how we come up to this formula in problem B.

»
6 weeks 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

»
6 weeks 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