Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

vovuh's blog

By vovuh, history, 17 months ago, translation, ,

1042A - Benches

Tutorial
Solution

1042B - Vitamins

Tutorial
Solution

1042C - Array Product

Tutorial
Solution

1042D - Petya and Array

Tutorial
Solution

1042E - Vasya and Magic Matrix

Tutorial
Solution

1042F - Leaf Sets

Tutorial
Solution (O(n log n))
Solution (Small to Large, O(n log^2 n))
Solution (O(n))

• +57

 » 17 months ago, # |   -12 so fast :/
•  » » 17 months ago, # ^ |   +4 So not available :/
•  » » » 17 months ago, # ^ |   +1 now it is ;)
 » 17 months ago, # |   0 gone in a flash :o
 » 17 months ago, # |   0 Wow, I failed C because of writing >= 2 instead of >= 1
 » 17 months ago, # |   +2 Loving the quick editorials !
 » 17 months ago, # |   +2 F*cking Monikaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa........
 » 17 months ago, # |   +16 Back to Back contest. And Back to Back fast editorial. I'm loving it.
 » 17 months ago, # | ← Rev. 2 →   -12 I'm loving quick editorials. Really helpful.
•  » » 17 months ago, # ^ |   0 Hey can you please explain the line "It's easy to see that the answer for the fixed L is n∑R=Lpref[R]
•  » » » 17 months ago, # ^ |   0 I didn't understand that Question's Editorial much.(Mostly because i have little experience with BIT and Segment trees).I did that question D after the contest using a modification of the merge sort(I read that in a comment somewhere). You can read this comment It might be helpful.But overall you need to learn BIT or Segment tree to make sense of the editorial at all.
•  » » » » 17 months ago, # ^ |   0 Thanks a lot :) . In that comment the algorithm used is BIT right? and not merge sort?
 » 17 months ago, # |   +2 For F,(untested)I was thinking of fixing the centers of subtree formed by the leaves. For fixing centers, we greedily move from lowest to highest depth(from bottom to top) and pick centers when necessary. Also to avoid problems, we can assume k is even (If not add some dummy vertices and make it even).Now from a center we want distance to farthest not yet taken leaf node to be k/2 (which is what I mean by when necessary).Does this work?
•  » » 17 months ago, # ^ |   +3 i think it works but it's hard to code(for me)
 » 17 months ago, # |   0 Thanks for quick editorial!
 » 17 months ago, # | ← Rev. 2 →   -13 Got TLE on tc 68 in C, DAAH! :(((((((((((((((( . . . . . .. Because of cin,cout DOuble DAAH! :(((((((((((((((((((
•  » » 17 months ago, # ^ | ← Rev. 2 →   +6 I feel u bro....damn fastIO
•  » » 17 months ago, # ^ |   0 I never use cin
 » 17 months ago, # |   +5 Can anyone explain me solution to Problem D?
•  » » 17 months ago, # ^ | ← Rev. 2 →   0 Let's say the input is A[1...n]. You first take the cumulative sum of A. Let this sum is S[1...N].So by the property of S, you know that the summation of any subarray from j + 1 to i is S[i] - S[j], right?Now, for each i, you want to find out how many j < i are there such that S[i] - S[j] < t. We rewrite the above as S[i] - t < S[j] i.e S[j] > S[i] - t.So, for each i, you take S[i] - t and find number of previous values S[j] where j < i. This can be done in Segment Tree or Fenwick Tree by doing coordinate compression, or simply you can use C++11 Order Statistics Tree from gnu pbds.
•  » » » 17 months ago, # ^ |   +3 Thanks!
•  » » » » 17 months ago, # ^ |   0 how to apply coordinate compression in BIT . I have just learnt about BIT .can anyone explain please .
•  » » » 17 months ago, # ^ | ← Rev. 2 →   0 How to apply coordinate compression in segment tree in this problem? I wrote a solution with only segment tree and got TLE. 43006558Update: I had made a big mistake. Solved using this quora ans about KQUERY
•  » » » 17 months ago, # ^ |   0 my code with Order Statistics Tree, makes the code very simple: 43010933
•  » » » » 17 months ago, # ^ |   +3 can you pls explain the second parameter in order_of_key?
•  » » » » » 17 months ago, # ^ | ← Rev. 2 →   0 During the implementation I relized that a multiset is needed, in order to store more than one instance of key in the tree. So I gave every key anoter parameter that distinguishes it from the others and doesn't affect the order of the actual values.It's a nice trick, if someone has a better solution to make it a multiset please share.
•  » » » 15 months ago, # ^ |   0 Great explanation, but I think I lost myself in some point. How this algorithm would work on this test case ?3 0 -1 2 -3The cumulative sum would be?-1 1 -2If yes, just in the -2 I would find 2 S[j] greater than -2. In the other two I don't find any.The answer for this test case is 4.
•  » » » » 7 months ago, # ^ |   0 NO, the partial sum, in that case, would be,0, -1, 1, -2.This might clear your doubt
•  » » 17 months ago, # ^ |   +7 you can also use a merge sort tree on prefix sums and binary search after.
•  » » » 17 months ago, # ^ | ← Rev. 2 →   +1 i did that . but my soltuion failed on test case 8 . 43007201 [ UPD : Found my bug & Got ACCEPTED ]
 » 17 months ago, # |   0 can someone help me generate some hacks for my C solution 42986208 it looks quite similar to editorial
•  » » 17 months ago, # ^ |   +3 3 -1 -2 -3
 » 17 months ago, # |   +29 In F, I get AC by following greedy algorithm but I don't know why it can get AC:Let the center of the tree is vertex c.Let the leaves are x1, x2, ...xm and dis(x1, c) ≥ dis(x2, c) ≥ ... ≥ dis(xm, c). for i from 1 to m: if x_i is not selected, make all vertices y that are not selected and dis(y,x_i) <= k be a beautiful set. Can someone prove or disprove it >__
•  » » 17 months ago, # ^ |   +8 http://codeforces.com/contest/1042/submission/42991099This seems to be O(n^2) worst case (calling f() multiple times), do you know why it isn't?
•  » » » 17 months ago, # ^ |   +33 The time complexity of my solution is . When , each vertex will be visited at most k times because I write if(r<=ma[x])return; and ma[x] is range from 0 to k. Thus, the function f only visit k × n state.When , a tree has at most O(n / k) leaves such that their distance is over k each other. So we only call function f at most times.I use the tightest tests I can think out to test my program and it takes about 2.6 s in Codeforces to calculate the solution.
•  » » » » 17 months ago, # ^ |   +8 The greedy algorithm can be sped up to O(nlogn) using a segment tree http://codeforces.com/contest/1042/submission/43079231
•  » » 17 months ago, # ^ |   0 I think it is correct.In your algorithm, you find leaf x with maximum distance from the root c first. Which means one of the longest path of the tree must start at node x. We assume path(x, u) is the longest path of the tree.Then, consider all y where dis(y, x) <= k. It is true that dis(y_i, y_j) <= k as well. Otherwise both dis(y_i, u) and dis(y_j, u) > dis(x, u) which is not possible. Therefore, the beautiful set found by your algorithm should be valid. After you found one beautiful set, you ignore those nodes in further processing, which is similar to delete the subtree containing those nodes. Therefore, all the beautiful set found by your algorithm should be valid as well.
•  » » » 17 months ago, # ^ | ← Rev. 2 →   +3 I get that the sets made by the algorithm are valid but why the total number of sets is minimum? You mean if our main vertex was not the center of the tree the algorithm would be correct either way? sorry if i asked a stupid question. sorry for my poor English.
•  » » » » 17 months ago, # ^ |   0 Let the first node (the node use for comparing the distance) we pick for each set be x_1, x_2, x_3 ... x_mAccording to the algorithm, It must be true that dis(x_i, x_j) > k otherwise they will be put to the same set. Therefore the algorithm should attain the optimal answer.
•  » » » » » 17 months ago, # ^ |   0 It was a stupid question I'm sorry. Thanks by the way.
•  » » 17 months ago, # ^ | ← Rev. 2 →   0 Maybe we can use exchange argument to prove it.The first thing to notice is that there is no reverse order pair in the sequence,if the optimal answer T *  has reverse order pair in it(without loss of generality we sort all the set by the depth of it),then we can always exchange it to get an answer which is not worse than T * .
 » 17 months ago, # |   0 IN array product my answer fails on 7 th test case . but i think my code is right.
•  » » 17 months ago, # ^ | ← Rev. 2 →   +8 Try this test case:6-8 -9 -7 2 3 0
•  » » » 17 months ago, # ^ |   0 yes. Now I got it.
•  » » » 17 months ago, # ^ | ← Rev. 2 →   0 my output for this test case is1 5 41 3 62 61 1 21 2 4 which in turn equals to 432(-8*-9*2*3). but still i am not passing test case 7 . according to you what will be its answer?.
•  » » » » 17 months ago, # ^ |   0 Try this: 3 -1 -2 -3
 » 17 months ago, # |   0 http://codeforces.com/contest/1042/submission/42995015Please, Can someone see my solution? Codeforces says wrong answer but it is producing output as expected.
•  » » 17 months ago, # ^ |   +4 Your logic is incorrect, you are forgetting that you cannot move the people who have occupied the benches, hence not necessarily you can make equal people sit on all the benches to find the minimum.
•  » » » 17 months ago, # ^ |   0 Thank you. I did mistake while reading question. :)
•  » » » » 17 months ago, # ^ |   0 You are welcome :)
•  » » 17 months ago, # ^ |   0 I think your solution is wrongHere is the sample 2 3 1 10The answer is 10 13 Your code output 7 13Cause the three people can sit on the second benchSo for each time where not all the people were sat , should find the min people of all bench and let this person sit here( people who is already sat can not move , maybe you should read this problem again :) Hope this can help you
•  » » » 17 months ago, # ^ |   0 It did. My bad. Thank you for reply. :)
 » 17 months ago, # |   -25 I am the BEST ！！
 » 17 months ago, # |   0 Am I the only one who thought of DP at the first glance of B?
•  » » 17 months ago, # ^ |   0 I also solved it using DP.
•  » » » 17 months ago, # ^ |   0 Can you please explain the solution and the dp state you chose, I couldn't understand your solution.
•  » » » » 17 months ago, # ^ |   0 see mine, it should be easier to understand.
•  » » » » » 17 months ago, # ^ |   0 Your approach seems quite interesting. Can you please explain it? I could not figure it out. Just a brief explanation of what you did.
•  » » » » » » 17 months ago, # ^ | ← Rev. 2 →   0 list the state in 3-bit binary numberif the state includes the bit 001 it means it contains vitamin Aif the state includes the bit 010 it means it contains vitamin Bif the state includes the bit 100 it means it contains vitamin CThe answer is ofc dp[(1 << 3) — 1].The rest is just simply knapsackThis technique is called bitmask DP if ure interested
•  » » » » » » » 17 months ago, # ^ |   0 Cool, the dp solution in prob B is quite elegant in my opinion. I implement dp too, btw my implementation is quite awkward
•  » » » » » » » 17 months ago, # ^ |   0 Got it. Thank you :)
•  » » » » » » » » 17 months ago, # ^ |   0 You owe him so much now :P
 » 17 months ago, # |   0 Could anyone please explain for me the basic idea of F. It is not so vivid to me. I understand the technique here but the idea....
 » 17 months ago, # |   0 By the way, how to write the judge code for problem C? (I think this is not trivial.) Does anyone have an idea?
•  » » 17 months ago, # ^ |   0 I can give you both checkers for this problem if you need :)
•  » » » 17 months ago, # ^ |   0 Oh, I missed the last paragraph of tutorial C (very sorry...!). And I'm interested in both checkers. Could you give me these sources...?
•  » » » » 17 months ago, # ^ |   +4 Here they are :)FourierMultisets
•  » » » » » 17 months ago, # ^ |   0 Thank you very much! I like Fourier one :)
 » 17 months ago, # | ← Rev. 2 →   0 Why does this D solution gives TLE on 46 test case?
•  » » 17 months ago, # ^ |   0 It seems that the complexity is O(N*logN*logN)
•  » » » 17 months ago, # ^ |   0 How? When I'm iterating with i, every time I call 2 log(4e14) functions. So complexity is O(2*N*log(4e14))=O(N*log(4e14)).log(4e14)=14. Did I miss something?
•  » » » » 17 months ago, # ^ |   0 In query(ll k) and add(ll k) functions, you used for loop in O(logN) time. For each k, you used map, the complexity was O(logN) time then total complexity for each function is O(logN * logN)
 » 17 months ago, # | ← Rev. 2 →   +36 I have another solution for problem D(Petya and Array) which doesn't need any data structure and I think it is really easier. It's using the merge sort.let the given array be a. we make array A such that A[0]=0 and for i>0 A[i]=A[i-1]+a[i-1]. (it's the partial sum!)now we must find number of pairs i,j (0<=i
•  » » 17 months ago, # ^ | ← Rev. 2 →   0 It can be further reduced to O(n*logn) by calculation while merging. My Soln .
•  » » » 17 months ago, # ^ |   0 but your solution got AC in 233 ms and mine in 156 ms ;)
•  » » » » 17 months ago, # ^ | ← Rev. 2 →   0 Because the constant factor of my algorithm is more than your algorithm. I copied elements and created more vectors than yours, whereas you have only swapped them. And for calculation of time complexity, we generally ignore constant factors.Edit — I have removed some unnecessary commands including clock() and switch to array (which you have used) instead of vectors. Now it takes 124 ms.
•  » » » » » 17 months ago, # ^ |   0 Ok alishahali1382, I know you can do it in 123ms.
•  » » » » » 17 months ago, # ^ |   0 OK I was just kidding! :)
 » 17 months ago, # |   0 Can any one explain me what am I doing wrong in question C? I am continuously getting wrong answer verdict on test case 7.My solution link is:-http://codeforces.com/contest/1042/submission/43011863Thanks in advance
•  » » 17 months ago, # ^ |   0 If you have odd number of negative values and one zero element then you are multiplying all negative value with zero insted of just multiply maximum value of negative with zero and remove it. I think in this test case you are getting wrong answer.
 » 17 months ago, # | ← Rev. 2 →   0
 » 17 months ago, # |   0 About Problem E, what is the "real" answer for sample case 2 (that can be represented by a rational number P/Q?
•  » » 17 months ago, # ^ | ← Rev. 2 →   +9 It is .There can be some paths, with their respective possibilities: (1, 2) → (1, 1) — probability of , final score of 1 (1, 2) → (2, 3) — probability of , final score of 2 (1, 2) → (2, 1) → (1, 1) — probability of , final score of 3 (1, 2) → (2, 1) → (2, 3) — probability of , final score of 6 (1, 2) → (2, 2) → (1, 1) — probability of , final score of 3 (1, 2) → (2, 2) → (2, 3) — probability of , final score of 2 (1, 2) → (2, 2) → (2, 1) → (1, 1) — probability of , final score of 3 (1, 2) → (2, 2) → (2, 1) → (2, 3) — probability of , final score of 6 To summarize, the expected answer will be: .
•  » » » 17 months ago, # ^ |   0 Hey Akikaze, can you tell me what evi is, in the tutorial of E?
•  » » » 17 months ago, # ^ | ← Rev. 4 →   0 Why can't we calculate for [r, c] all the possible jumps from it to all the elements with value < val[r, c]? we ((sum them up) / number of such elements < val[r,c])? And similarly for all the other elements < val[r, c], summing them up until we no more find element < val[i, j]?(By summing them up, I mean summing the euclidean distance between them.)Here is my code. Would be awesome if somebody cleared this doubt.
 » 17 months ago, # | ← Rev. 3 →   0 Failed on tooooo many troubles. Sad.
 » 17 months ago, # |   0 Nice problemset and a clear tutorial!
 » 17 months ago, # |   0 can someone explain the part in D's editorial how segment tree or fenwick tree can be used
 » 17 months ago, # | ← Rev. 4 →   0 For the problem D, anyone can explain more details for me please. What do we do when call upd(k)? int npos = upper_bound(sums.begin(), sums.end(), pr - T) - sums.begin(); ans += (i + 1 - get(npos - 1)); int k = lower_bound(sums.begin(), sums.end(), pr) - sums.begin(); upd(k); 
•  » » 17 months ago, # ^ |   +1 It's actually bad editorial. It's hard to understand ( cost me one day )
•  » » » 17 months ago, # ^ | ← Rev. 4 →   +14 This is similar to the Inversion number problem, formally:Given an integer sequence A which consists of N elements, count the number of pair (i, j) where i < j and A[i] > A[j] are satisfied.It is known that this problem can be solved with BIT. The strategy is like: S = [], ans = 0 iterate j from 0 to N - 1, and do A), B), C). A) calculate the number of elements in S which is less than A[j]. Let this number be x. B) add j-x to ans C) add A[j] to S print ans The important part is A). if you count this value with straight forward algorithm, it will work in O(number of elements in S). To get this value efficiently, we can use BIT.BIT provides these operations: add(i, x): v[i] += x; (in the tutorial code, upd(i) := add(i, 1) ) get(i): return v[0] + v[1] + ... + v[i] O(N) memory is required and both operation can be done in time (where N is max i).Treating A[j] as i, we can perform A) and C) operations using BIT, like A): get(A[j]), C): add(A[j], 1)But there is a problem: A[j] can be big and it will cause MLE...So, we have to pre-calculate k, such that A[j] is the k-th smallest number in A. (This can be done with sorting, unique the array and calling lower_bound)After that, we can use k instead of A[j] for BIT.This solution works in where N is the number of elements in A.
•  » » » » 17 months ago, # ^ |   0 Best explaining ever. Thank you very much !
 » 17 months ago, # |   0 Hi Can anyone answer me why I got a runtime error while in my offline compiler I can run the code and get correct answers!? 43073036
 » 17 months ago, # | ← Rev. 2 →   +8 Solution (O(n log n)) has some problem for this data. 14 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14this solution's answer is 2.this is incorrect, yes?
•  » » 17 months ago, # ^ |   +11 There are two leafs(1 and 14) and distance between them is more than 1, so answer is 2.
•  » » » 17 months ago, # ^ |   0 thanks,I have a wrong Understanding for this question.
 » 17 months ago, # |   0 I solve D with wavelet tree, then realise the operation I use can be done with simple coordinate compression and fenwick tree.
•  » » 17 months ago, # ^ |   0 It can also be done with merge sort tree
 » 17 months ago, # | ← Rev. 2 →   0 Problem F, the constant of O(n) solution is so large that the real runtime between O(n) and O(nlogn) is similar. (1076ms for O(n), 1091ms for O(nlogn))
 » 17 months ago, # |   0 Every time I read the editorials I think I need to put lot more effort
 » 17 months ago, # | ← Rev. 2 →   0 Problem D can be solved with STL's red-black tree implementation (https://codeforces.com/blog/entry/11080): http://codeforces.com/contest/1042/submission/43157263.
 » 17 months ago, # |   0 Can someone please tell me what is the bug in my code (http://codeforces.com/contest/1042/submission/43167869) for problem D. I used sqrt decomposition technique and am not able to find any bug myself. My logic is basically to find all the r>=l for every l.
•  » » 17 months ago, # ^ | ← Rev. 3 →   +1 You have to check whether j is valid or not. (It is not guaranteed that (b+1)*M is less than n+1)UPD: If you use g++, passing -fsanitize=undefined and -D_GLIBCXX_DEBUG options to the compiler makes it easier to detect these types of bugs (I found the bug just to try Sample Input 3. Please try it!)
•  » » » 17 months ago, # ^ |   0 Thanks a lot and yes will use these in the future for detecting bugs.
 » 16 months ago, # |   0 In problem F, why the greedy algorithm is correct ?
 » 14 months ago, # |   0 Can anyone explain this solution 43084968 for problem 1042B, I solved the problem using tutorials logic, but I am failing to understand the above solution. Thanks.P.S I am new to DP.
 » 6 months ago, # |   0 Problem A can be easily solved with a Min Priority Queue.My Solution : 58882318
 » 6 months ago, # |   0 i used merge sort tree in question D (Petya and Array) but it got time limit exceeded. I know that the time complexity for a query in merge sort tree along with binary search is O(LogN*LogN). So this should not be a problem. Here is the link for my code.Please help — https://ideone.com/rKdDMD