### vovuh's blog

By vovuh, history, 14 months ago,

1195A - Drinks Choosing

Tutorial
Solution

1195B - Sport Mafia

Idea: ?

Preparation: MikeMirzayanov and _kun_

Tutorial
Solution (binary search)
Solution (formula)

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

Preparation: ima_ima_go

Tutorial
Solution

1195F - Geometers Anonymous Club

Idea: senek_k

Tutorial
Solution

• +30

 » 14 months ago, # | ← Rev. 3 →   0 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?
•  » » 14 months ago, # ^ |   +33 I googled "the number of distinct values on segment persistent segment tree" right now and the answer is in the first link.
•  » » 14 months ago, # ^ |   +19 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.
•  » » » 14 months ago, # ^ |   +21 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.
 » 14 months ago, # |   +2 I'm unable to understand the tutorial provided for problem C.
•  » » 14 months ago, # ^ |   +2 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. :)
•  » » » 14 months ago, # ^ |   0 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 ?
•  » » 14 months ago, # ^ |   +1 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.
•  » » » 14 months ago, # ^ | ← Rev. 3 →   0 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?
•  » » » » 14 months ago, # ^ |   0 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.
 » 14 months ago, # |   -15 here is explanation of problem C btw its not my channel https://youtu.be/0E9bj0CLOo4
 » 14 months ago, # | ← Rev. 2 →   0 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 — 57289296UPD: ACed it. Just store the next index with same slope.
 » 14 months ago, # |   0 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?
 » 14 months ago, # | ← Rev. 2 →   +1 Can someone explain the formula used in problem D2 ??
•  » » 11 months ago, # ^ |   0 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
 » 14 months ago, # |   0 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
 » 14 months ago, # |   0 Problems were fairly standard, except last one, but I liked it. Good test samples with edge tests.
•  » » 14 months ago, # ^ |   +5 I think F is also fairly standard for people who knows Minkowski sums. I just opened Berg's Computational geometry and it contains the algorithm to solve that problem.
•  » » 14 months ago, # ^ |   +13 F is standard too: https://en.wikipedia.org/wiki/Minkowski_addition#Two_convex_polygons_in_the_plane (which is really easily easy to find) spoils the fact that you can reduce the problem down to distinct values in a range, and then that too is google-able :/
•  » » » 14 months ago, # ^ |   +3 I even did not try search the solution. I do it almost never during contest. But it is good problem for self solving.
 » 14 months ago, # |   0 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)$.
•  » » 14 months ago, # ^ |   0 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 ?
 » 14 months ago, # |   +8 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.
•  » » 14 months ago, # ^ |   +5 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.
 » 14 months ago, # |   0 IMHO, this round really looks like the educational one: formula, dynamic programming, binary search, "quite typical task" with minimums.
 » 14 months ago, # | ← Rev. 2 →   0 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)) 
•  » » 14 months ago, # ^ |   0 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))_
•  » » » 14 months ago, # ^ |   +1 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 
•  » » 14 months ago, # ^ |   +1 I WASTED LIKE 5 MINUTES THINKING ABOUT THIS
 » 14 months ago, # | ← Rev. 2 →   +2 Can't see the Tutorial B :"Unable to parse markup [type=CF_MATHJAX]"
 » 14 months ago, # |   0 Can someone explain D solution? I don't get it at all
 » 14 months ago, # |   0 i solved problem B C D1 D2, still reading problem A and still don't understand.please translate me.
 » 14 months ago, # |   0 Any similar problem like C for practice as written in editorial, C was standard problem of dp Thanks...
 » 14 months ago, # |   0 Interesting Problem E
 » 14 months ago, # | ← Rev. 2 →   0 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?
•  » » 14 months ago, # ^ |   0 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
 » 14 months ago, # |   0 Is Problem F's description of the example wrong?
 » 14 months ago, # |   0 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??
 » 14 months ago, # |   0 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
 » 14 months ago, # |   0 can somebody upload the solution of the problem c in python using dp im not able to understand the one in the editorial
 » 14 months ago, # |   0 Can somebody help me with the precision on F? I am getting WA on test 3 and my answers are pretty far off.
 » 13 months ago, # |   0 I'm a beginner , can anyone explain how we come up to this formula in problem B.
 » 13 months ago, # | ← Rev. 2 →   0 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
 » 13 months ago, # |   0 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
 » 8 months ago, # |   0 How would we solve the problem C if an additional condition of k members in a team is imposed
•  » » 7 weeks ago, # ^ |   0 I would add a third condition on the dp. That way whenever I check for maximums, I check dp[i][0/1][whatever k].