### vovuh's blog

By vovuh, history, 2 years ago, 988A - Diverse Team

Tutorial
Solution (Vovuh)

988B - Substrings Sort

Tutorial
Solution (Vovuh)

988C - Equal Sums

Tutorial
Solution (Vovuh)

988D - Points and Powers of Two

Tutorial
Solution (Vovuh)

988E - Divisibility by 25

Tutorial
Solution (Vovuh)

988F - Rain and Umbrellas

Tutorial
Solution (Vovuh)
Solution (step_by_step) Tutorial of Codeforces Round #486 (Div. 3) Tutorial of W10 novice contest 486, Comments (89)
 » Can you explain a bit about what the Convex Hull Trick and Li Chao tree are ? Most Div 3 contestants like myself have little-to-no idea about what they are :)
•  » »
•  » » Also check the solution by step_by_step which uses Convex Hull Trick and Li Chao tree (I added it to the tutorial)
•  » » » 2 years ago, # ^ | ← Rev. 3 →   Are you sure that it works in n*log^2(n)? In my opinion it's n*log(n). Why is it log^2?Does it also use Convex Hull in some way? In my opinion the tree can: 1. Given a and b, insert a new linear function. 2. Given x0, print the maximum value f(x0).and it is done in a*log(a)
•  » » » » It depends on solution. Solution by step_by_step works in O(n*log^2(n)), but there are another one solution that works in O(n*log(n)) (of course there are many solutions which works in O(n*log^2(n)), O(n*log(n)) and with many other complexities). I was describe one of them and i do not deny that in this problem exists solution in O(n*log(n)).
•  » » » » » Thanks for reply. Could you tell me why it’s log(n)^2? Does update on the tree take log^2? I can’t see why.
•  » » » » » » This segment tree does the following:1) splits the query segment into log(n) segments (like all segment trees do)2) From each of these log(n) segments it goes down until it reaches the leaf, so for each segment it spends log(n) operations (the height of tree) If n = 2^k and the query segment is [0, 2^k — 2] then the number of operations will be (log(n) — 1) + (log(n) — 2) + (log(n) — 3) + ... = O(log(n)^2)
•  » » » » » » » Thanks, I understand now. So all Li Chao trees work in log(n)^2?
•  » » » » » » » » 2 years ago, # ^ | ← Rev. 2 →   No, if the size of segment tree is power of two and you update all elements then it works in O(log(n)) So you can use it when you need Convex Hull Trick and the query coordinates are not big. Time complexity will be log(a) per update/query, where a is the minimal power of two greater than maximal coordinate.
•  » » » » » » » » » So if I make updates on whole tree, then updating works in log(n), because I divide it into one segment and it goes down one way to leaf. So log(n). And query costs log(n) (from root to leaf). If I change updating to whole tree, then it will work in a*log(a).
•  » » » » » » » » » Yes, it will be O(a*log(a))While writing my solution I didn't notice it)
•  » » » » » » » » » Can we use this when the query coordinates are negative but their absolute value<=10^5?
•  » » » » » » » » » Yes, you can add 10^5 to all your coordinates and change linear functions:k*x+b -> k*(x+10^5)+b-k*10^5
•  » » » » » » » » » Thank you,just one more question suppose there are n linear functions and for a given query we consider only functions from l to r. So can we use lichao tree in this problem?
 » 2 years ago, # | ← Rev. 4 →   Hard contest for Div3, I think that it is Div2. Statistics of successful solutions in comparison with previous Div3 and Div2 confirms this  | #486 (Div. 3) | #485 (Div. 2) | #484 (Div. 2) | #483 (Div. 2) | #482 (Div. 2) | #481 (Div. 3) ------------------------------------------------------------------------------------------------- A | 3308 | 4140 | 2291 | 3879 | 3503| 2493 B | 2460 | 2763 | 2175 | 2952 | 636| 2508 C | 1313 | 1963 | 1416 | 743 | 1362| 2139 D | 292 | 600 | 370 | 392 | 130| 777 E | 64 | 501 | 14 | 9 | 7| 1066 F | 18 | 8 | 3 | - | -| 824 G | - | - | - | - | -| 261 
•  » » Can you share the statistics ?
•  » » » Updated
•  » » Maybe it's not that this round was too hard but the previous ones were too easy?
•  » » » 2 years ago, # ^ | ← Rev. 2 →   Almost all my school mates are complaining about the round.I think maybe it's a bit too hard for division 3?Easier than div.2 rounds, but definitely harder than what I thought div.3 round should be.
•  » » » » There are no really hard problems, even the problems E and F are close to typical Div2C problems, but not harder than Div2C. If i will make Div3 rounds much easier, it will lead to easy way to get 1600+ by participating in Div3 rounds.
•  » » » » » All problems were doable and I liked them, but I think there is too little difference in difficulty form C to F
•  » » » » » I would agree with you that the difficulty of E and F were quite normal.However C and D might be of inappropriate difficulty.What's more, STLs like map, vector or set shouldn't appear so frequently since the beginners may not make their way to STL so fast.
•  » » » » » » My solutions for C and D are almost don't use STL expect sort and standard binary search method. This is not too hard to implement your own sort or your own binary search to find the element in O(log n) instead of O(n).But I agree with you that C and D should be much easier that they were in this round. And in these problems must not be too difficult ideas or algorithms as fast sorting or binary search.
•  » » » » » 2 years ago, # ^ | ← Rev. 3 →   Reading your comment Vovuh : I wonder , why Div-3 was even created!
•  » » » » » » Reading your comments Player.01: I wonder that you first ask to make the problems harder, and when I did it, you wonder that they are too hard.
•  » » » » » » » My intention was to remind the setters that the logic behind creating Div-3 , which is not surely pulling them to Div-2 comfortably , but to set problems that would help them to train logically & rigorously. I mean , it should work as a guideline , don't you think? I believe , setters must be flexible to any feedback & take only the logical ones into considerations rather blindly defending his rounds. Thnx for reading my comments. Have a nice day!
•  » » » » » » » » Please don't think that I'm blindly defend my rounds. I'm accept some good ideas from comments and draw my conclusions to do the rounds better than they are now. Have a nice day and sorry if I'm offended you.
•  » » You also doesn't notice that there are different parts of community participate in div2 and in div3.
•  » » » From your post : " Probably, participants from the first division will not be at all interested by this problems. And for 1600-1899 the problems will be too easy." I am pretty sure Not all Div-2 participants solved All the "too esay" problems , even some Div-1 participants struggled with E & F .
•  » » » » Maybe some div-1 participants didn't solve E & F. But that doesn't prove that they struggled with E & F. It may also happen that they found E & F too easy to try. I personally found D to be tougher than E & F. F was very basic DP problem. E was also simple, just handling corner cases were difficult. I haven't learnt anything by solving E & F. But I have learnt from D.
 » Can anyone please explain the use of npos in solution B — "if (s[i + 1].find(s[i]) == string::npos) " ?Cplusplus.com says that it is the greatest possible value for an element of size_t , What does it means ?
•  » » 2 years ago, # ^ | ← Rev. 2 →   It is almost the same as -1 (means "not found" or some similar). I use it just because of my habit. You can replace it with -1 and nothing will change.I can be wrong and maybe someone else will explain it better than me.
•  » » Just complementing Vovuh's answer: since npos is unsigned, -1 actually corresponds to the largest possible integer value. Since it's greater than the size of any string, it never equals a valid position, and is thus returned to indicate a failed search.
 » In solve 988D:  if (isl && int(res.size()) < 2) { res = { lx, x[i] }; } if (isl && int(res.size()) < 2) { res = { x[i], rx }; } Maybe it would be more correct?:  if (isl && int(res.size()) < 2) { res = { lx, x[i] }; } if (**isr** && int(res.size()) < 2) { res = { x[i], rx }; } 
•  » » Woooops, yes, you are right! But surely this solution is also correct and the second if statement is useless :D It is because of you will check this pair of points when x[i] will be the right point and lx will be the left point.I will fix it anyway, thank you :)
•  » » » Nothing, but you again made mistake....:  if (isr && int(res.size()) < 2) { res = { lx, x[i] }; } if (isl && int(res.size()) < 2) { res = { x[i], rx }; } You need to swap first line and 4th....
•  » » » » Oh, I made the second mistake only in the Russian version of editorial, in English version all is fine :D Thank you, fixed again (I hope :D)
 » There is any other way to think/solve D?
•  » » 2 years ago, # ^ | ← Rev. 2 →   I think that the max size of set wont larger than 3,so you can enumerate each xi and each 2^n where n <= 32 #include #include #include #include #include #define ll long long using namespace std; ll a; ll ans; ll tem; ll pow2; void init(){ ll base = 1; for(int i = 1; i <= 32; i++){ pow2[i] = base; base *= (ll)2; } } int main(){ init(); int n; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int a1, a2, a3; int len = 1; bool flag3 = false, flag2 = false; for(int i = 0; i < n; i++){ for(int j = 1; j <= 32; j++){ ll dif = pow2[j]; int pos1 = lower_bound(a, a+n, dif+a[i]) - a; int pos2 = lower_bound(a, a+n, 2*dif+a[i]) - a; if(a[pos1] == dif + a[i] && pos1 < n){ flag2 = true; a1 = a[i]; a2 = a[i] + dif; //cout << pos1 << endl; //cout << a[pos1] << " " << a2 << endl; if(a[pos2] == 2*dif + a[i] && pos2 < n){ flag3 = true; cout << 3 << endl; cout << a1 << " " << a2 << " " << a[i] + 2*dif << endl; return 0; } } } } if(flag2){ cout << 2 << endl; cout << a1 << " " << a2 << endl; } else{ cout << 1 << endl; cout << a << endl; } return 0; } 
 » Vovuh,In problem C why did you use stable_sort? Normal sort also passed. What is the difference? In the future at what kind of situations should we use stable sort?
•  » » Hi, I've the same doubt, were you able to find an answer?
•  » » » Nope
 » Vovuh in F we can use another dp with O(a) memory and O(a2) time: in case we have umbrella in i-th position: dp[i] = min i <= j (dp[j] + (j - i) * w[i]) if we don't: dp[i] = min(dp[i + 1], dp[i]) and when doing convex hull trick on this dp, we can see that slopes are decreasing, so we don't need additional structures to maintain convex hull. so this can be solved in O(a * log(a)) time link: 38904415
 » Problem F — Someone can help me?, i can't find mi error :(. My codeThanks in advance c:
•  » » 2 years ago, # ^ | ← Rev. 2 →   If you have more than one umbrella at the same place, you should get the one with minimum weight
•  » » » You're right. Oh my god, i can't believe that i didn't consider it. Thank you. :D
•  » » » @Emiso i was having the same mistake. thanks a lot
 » In problem D tutorial, how is k equal to L ?
•  » » 2 years ago, # ^ | ← Rev. 3 →   If we transform to the binary representation, any power of two is represented by 1 and number of 0's (may be zero) to the right of that 1, for example: 1 and 1000, which are equal in decimal to: 1 and 8, respectively.So, when you sum two different powers of two together, you will get a number which is represented in binary as two 1's and number of 0's (may be zero), for example:10000+00100=====10100So, the result is not a power of two.But there is one case that will give us a power of two from summing two powers of two, and it is summing a power of two to itself, like:01000+01000=====10000Hope it answered.
•  » » » 2 years ago, # ^ | ← Rev. 2 →   Thanks for the answer, it cleared a lot. But why does the answer not exceed 3? it could have been dist(a, d) * 4 instead of 3.upd: ok, I got it, in case of abcde there's still a abcd triplet in there, so the distance between a and c will still be 3*2^k, and it will be still invalid.
•  » » » » Welcome :).The equation dist(a, d) = dist(a, b) * 3 came from the proofs before it.In the same way in which the tutorial proved that dist(a, b) = dist(b, c), we can prove that dist(b, c) = dist(c, d). So, the following equation is hold:dist(a, b) = dist(b, c) = dist(c, d) ( = x, for example).And we know that: dist(a, d) = dist(a, b) + dist(b, c) + dist(c, d) = x + x + x = 3 * x. And from here, 3 came.And any changing in this 3 means that some changes in the previous parts of the proof were happened, for example:dist(a, d) = 4 * x can come from: dist(c, d) = 2 * x, which means that: dist(b, d) = dist(b, c) + dist(c, d) = x + 2 * x = 3 * x, which isn't a power of two, but in the problem, we need dist(b, d) to be equal to a power of two.
•  » » » » » Can you maybe also answer how the term of 10^9 came in the equation when calculating the complexity?
•  » » » » » » Sorry, I cannot reply now. I'll try later (may be toworrow).
•  » » » » » » You iterate over all powers of 2, from 20 until 2x with 2x ≤ 2 * 109.There are many of these powers.
•  » » » » » » » Thank you, it would have taken with me much more words to answer it :)
 » Will the vector in problem C's solution too large? Cuz it maybe 2 * 1e5 * 2 * 1e5 elements...?
•  » » If you read Problem C carefully it says :"The sum of lengths of all given sequences don't exceed 2⋅10^5".So, vector.size() is guaranteed to be <= 2⋅10^5
•  » » » oh...thanks
 » 2 years ago, # | ← Rev. 2 →   Why am i getting TLE on div 3 D when using map, and AC when using original binary search ?Using binary search: https://codeforces.com/contest/988/submission/38916484Using STL Map : https://codeforces.com/contest/988/submission/38916424
•  » » 2 years ago, # ^ | ← Rev. 2 →   I have made the same mistake. See this comment please.
•  » » » Oh thanks alot bro
 » " You can also notice that the order of swaps doesn't matter and you can rearrange them in such a way that no leading zero appears on any step. " How to prove this or any kind of intuition behind this ?
•  » » 2 years ago, # ^ | ← Rev. 2 →   You can rearrange the operations in such a way that first of all you move the leftmost non-zero digit in the number to the left in order to keep the number correct after each of the next operation.But in general case you don't know which digit will be leftmost non-zero (there are too many cases that are depend on the digits you choose), so to make this problem much simpler you firstly move digits you choose to the right and then find the leftmost non-zero digit and move it to the left.
•  » » » Yeah Cool, Thanks a lot :)
 » can anyone explain the 3rd case of 988F .
•  » » Test Case: 10 1 1 0 9 1 5 the segment 0-9 is under rain and only haft an umbrella on the position 1 so you never can pass the position 0 without getting wet, so the output is -1
 » 2 years ago, # | ← Rev. 2 →   Can anyone tell, why http://codeforces.com/contest/988/submission/38851322 this solution didn't work for Problem C?The approach given in the Editorial also fails in Python.
 » 2 years ago, # | ← Rev. 2 →   Why does the memory blow up on 7th test case? 38888128 (Problem B)I know the complexity is O(n2)...but why does memory blow up?Is it something to do with sorting string array?
•  » »
 » guys can u give me link reference + solution using cordinate compression, how do you compress coordinates
 » I am sorry to bother every one .But I have something trouble in this Div.3 C.This is my code : https://codeforces.com/contest/988/submission/38961776As you see , I hack my code ( use the DEBUG macro ) .And find this code output is correct . The testcase is too long to see , that is why I wonder someone help.My way to sovle this problem :Enum the sequence i and j , and enum the elements of sequence i .As well known , if I confirm the Sum of I , the Sum of J , and the element in i , then I can calculate what the element in j is .And I binary_search the j which I calculated . And then , I can know is "YES" or not . I clearyly know my code is too complex to pass , but I want to know , why I got the "Wrong answer" , rather than "Time Limited Exceed". Thank you very much for everyone help :D!
•  » » I know why I got an wrong answer. If I use the binary search , I have to sort the vector .But I sort the vector , the index of the vector have changed .
 » Probably a simple solution to E : http://codeforces.com/contest/988/submission/38965567 As we have only 4 options 00, 25, 50, 75 at the end Just count the place by which these digit is to be moved obviously taking care of leading zeroes at the beginning in case the swap is made.
 » 2 years ago, # | ← Rev. 2 →   Anyone explains me why my code is TLE? #include #define ll long long #define fi first #define se second using namespace std; int n,m,cnt; vector>> a; int main() { cin>>n; for (int i=0;i>m; vector x(m); for (int j=0;j>x[j]; ll s = accumulate(x.begin(),x.end(), 0); for (int j=0;j
•  » » if a.size() is 1e5 then 1e5*1e5=1e10 , it's bad : ).So , will give you TLE.
 » 2 years ago, # | ← Rev. 2 →   Can someone explain the problem with this approach for "Rain and umbrellas"?let - dp[x] --> indicates that umbrella was not used for segment (x-1) to (x) dp[x] --> indicates that umbrella was used for segment (x-1) to (x) Now, dp[x]= infinity (or invalid) --> segment (x-1) to x is under rain= min( dp[x-1], dp[x-1] ) --> segment (x-1) to x not under raindp[x]= (best wt umbrella uptil now) + dp[x-1] --> I continue carrying same umbrella = (best wt umbrella uptil now) + min( dp[x-1], dp[x-1] ) --> I drop heavy wt umbrella and pick more lighter umbrellaGetting wrong answer. Not able to find whats wrong with the approach.
•  » » The problem in your approach is best umbrella to be carried till position i may not be necessarily the best one for i+1 but other umbrella might help. For computing the optimal solution, you MUST consider all previous choices of umbrellas. This is the dynamic programming approach, what you BEST is actually greedy. Hope it helps.
•  » » » 2 years ago, # ^ | ← Rev. 2 →   I got the scenario where my solution would fail . Thanks :)
•  » » » Hello rahul, Can you please tell me that in third problem the solution provided by editorialist makes the assumption that the variety of sums while removing each element and at the same time inserting into the vector does not grow very large otherwise can there be a case that the vector still accepts upto 10^9 elements in it or there could be memory limit excedeed?
 » Can anybody help me with this: http://codeforces.com/contest/988/submission/38960067 I can't find my mistake. Is stuck in testcase 5.
 » 2 years ago, # | ← Rev. 2 →   In problem F I use:int dp[i] — the minimmum fatigue to get to position i,bool rain[i] — true if position i is into a raining interval,umbrellas[i] — the minimmum weight of an umbrella at position i, if there is no umbrella, umbrellas[i] = 0;In my approach I calculate for each position i, what's the minimmum fatigue of getting there using the umbrella at position j (j <= i):dp[i] = min( umbrellas[j] * (i — j) + dp[j — 1]) for each j from i to 0;I can't see where my idea fails, still stuck in testcase 5.
•  » » 2 years ago, # ^ | ← Rev. 2 →   Not reading carefully the problem caused me multiple WA5, specifically this lines:he must carry at least one umbrella while he moves from x to x+1 if a segment [x,x+1] is in the rain (i.e. if there exists some i such that li≤x and x+1≤ri).Hope you don't come across the same mistake
 » Can anyone help me out how sort function work here in 988C .i am beginner here.
 » Can anybody help with this code please, i don't understand why it is not working. problem 988F. https://ide.geeksforgeeks.org/cUWXmWZsWM
 » Can somebody explain the error in this code for Problem E? Or provide a test case where it fails to produce the expected output? It is giving me wrong answer verdict on some test case. Here is the link to my solution:- https://ide.geeksforgeeks.org/pA25wT6HpW
 » 2 years ago, # | ← Rev. 2 →   For problem C you can do it in O(k) time. Specifically O(n + k), but since sum of n = k it will be O(k). To do it, you can use the 2sum trick, i.e. reduce it to problem a + b = 0 where a and b are in two separate arrays.Let H be a hash map which maps an int to a pair of values (k, i) where k is the sequence and i is the element, i.e. h[s] = (k, i) which tells us the sum of the k'th sequence subtract the i'th element is equal to s.Iterate through each sequence, calculate the sum of the sequence, call it s. For each element in the sequence, a, check if s — a[i] exists in the hashmap. If it does, we have a solution. After we have checked the entire sequence, map all of these values we checked into the hashmap.i.e. the hashmap considers all sequences (and elements) from 0 to i — 1 (where i is our current sequence). We do not have to store every possible (k, i) in the hashmap since we only need one solution.
 » about Problem D: why the size of the answer set can be 1?
 » Can anyone please tell me how standard compare function works or refer me any article about it?
 » Problem 988B- Substrings Sort Can anyone please explain how this cpp snippet works.sort(s.begin(), s.end(), [&] (const string &s, const string &t) { return s.size() < t.size(); });It's functionality is to simply sort the entire vector of strings according to the size of individual strings. Thanks in advance :)