### TheWayISteppedOutTheCar's blog

By TheWayISteppedOutTheCar, history, 5 years ago,
| Write comment?
 » 5 years ago, # | ← Rev. 2 →   0 Will the tutorial for Div 1-E be available in the future? Edit: it wasn't available just for a few minutes.
 » 5 years ago, # |   0 can someone who can give proof of C ????
•  » » 5 years ago, # ^ |   +10 Div 2 C:It doesn't depend at all if there exists any letter other than a or b in the given string. You can for sure ignore those letters, so the editorial says to erase them. Now, what you have is a string consisting only of a and b's. Also two consecutive b's can be merged as one. So your final string will look something like (a...a)b(a...a)b(a...)...You can now consider this problem as sum of all possible product of subsets of a given set, where each element in the set is the number of a's delimited by b.For example: In the string "aaabaabaaab", set formed will be {3,2,3,0} (0 can be ignored). Now if you have a set {a1,a2,...,aN}, then sum of all possible products of this set is equal to (1+a1)*(1+a2)*...*(1+aN)-1.Proof:Write the required answer as follows:S = Sum of products of subset with (size=1)+(size=2)+...(size=N)S = (a1+a2+...aN)+(a1.a2+a1.a3......+aN-1.aN)+...+(a1.a2.....aN)After factorization,S = (1+a1)(1+a2)...(1+aN)-1Hope it helps..:)
•  » » 4 years ago, # ^ |   +1 if you don't understand the math here is my dp solution codeBasically dp[i] stores the number of valid subsequences from first i characters of the string. If the current character is not a then dp[i]=dp[i-1] otherwise we need to find the index of rightmost a to the left of i such that at least one b follow that a then dp[i]+=dp[index]
 » 5 years ago, # | ← Rev. 2 →   0 Hey, can anyone explain why do we need to increase the blocks size by 1? :p (Div2 — C)
•  » » 5 years ago, # ^ |   +14 Because if we have x 'a' symbols in the block we can pick the first 'a' from this block or the second 'a' or the third or ... or the n-th or we can just not pick any of 'a' from the block.
•  » » 5 years ago, # ^ | ← Rev. 2 →   +4 For a given block you need to decide the number of ways to pick an "a". As it is possible that you do not choose any "a" from that segment we need to add 1 to each segment. Addidtionally you'll be required to subtract 1 from your thus obtained answer as it includes the case where no "a" is selected in all the segments and thus an empty subsequence.
•  » » 5 years ago, # ^ | ← Rev. 2 →   +4 a + b + ab = (1+a)(1+b) -1; a + b + c + ab + bc + ac + abc = (1+a)(1+b)(1+c) -1 in this fashion we can calculate our ans i.e sum of product of every possible combination
•  » » 5 years ago, # ^ |   0 Suppose we have a string aaabaabaaa which can be represented by set {3,2,3} which represent 3 consecutive a's followed by b, 2 consecutive a's followed by b and 3 consecutive a's. Now from the set {3,2,3} to choose a subsequence we can choose any one of the first 3 a's and we also have an option to choose non of the a's, therefore we have total (3+1) ways of choosing the first element in our subsequence, similarly for each consecutive a's we have one more way of choosing element from it i.e to choose nothing from it. Thats why we decrease total ways by 1 as it includes complete empty sequence where we chose nothing from all sets. Hope it clears your doubt.
 » 5 years ago, # | ← Rev. 6 →   +26 As a comment on problem Div1-B : consider the trie of all strings that made of only 'a' and 'b' chars, It's a complete binary tree of height n. actually we should choose k leave of this tree such that the strings of these leaves be in the range [s,t] and the minimal sub-tree that consists these leaves and the root of the trie is as large as possible the final answer is the size of this sub-tree!Ok! now we greedily add leaves and compute the answer! (counting,bfs or...) Now we can solve the more general case of this problem! :)) (for example: each string consists lowercase English letters!)
 » 5 years ago, # |   0 Can someone explain C with dp approach ?
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 I did using 3 parameters: i: the current position in the string; n: meaning if i have or not a 'b' in the current subsegment; p: meaning if i already formed a valid subsegment. Basicaly, if the current caracter is 'a' and i already have a 'b' i can start a new subsegment or continuos a previous; or, if the current caracter is 'a' and i don't have a 'b', i can just continuos the subsegment; if is 'b' i set that i have a 'b'; else, i just go. submission
•  » » » 5 years ago, # ^ | ← Rev. 4 →   0 Hello, In your approach as far as I can understand when n==0 it means we already have a 'b' in our subsequence and when n==1 it means we do not have a 'b' in our subsequence then why we call the function as solve(0,0,0) that is by setting n to 0 . Or Am I wrong somewhere ? Please explain why we will begin by assuming that we have a 'b'. UPD 1: I have figured it out that if we will not assume b in beginning than all the a's before the first 'b' will be ignored and our problem will not be divided into subproblems.
•  » » » » 5 years ago, # ^ |   0 It's because when we start a new subsequence we didn't need to have a 'b' before the first 'a'.
•  » » » » » 5 years ago, # ^ |   0 More formally 'n' represent whether we can start a new subsequence from the current index or not.
•  » » 5 years ago, # ^ |   0 I made it bottom up this way: The state will be the current index, with the guide question: "how many ways to make valid sequences 'til here?".the answer for dp[i], will be all the ways to dp[i-1], plus, if the current char is 'a', you can start one new sequence here (so, plus one), or extend all the previous valid sequences before the last 'b' (because you must have an 'b' between 'a's), which means that you can extend all the valid ways until the index of the last 'b', that turn out to be dp[index_of_last_b].
 » 5 years ago, # | ← Rev. 2 →   0 Anyone please help me to find the fault in my 46870177 of 1084B - Квас и Орехус.
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 Guy, I read your code and your error is make the division integer, if you variables is unsigned long long all your operation with your variables will give a result with this type,backing to the problem when you user (int) will give error when the division gives a large result. My submission 46889415 of your code.
 » 5 years ago, # |   +3 In Div2 D or Div1 ATest Case :35 24 241 2 161 3 14 Although, the answer would be correct by approach described above. But I am interested in calculating answer when the turning vertex is 1. By this approach it will be (8+10+5) where 8 will come from 24-16 and 10 will come from (24-14). But in actual that path is not at all possible. Because if we start from 2 and reach to 1 gas is = 13 but to go to 3 we need 14, cann't go to 3. same will be the situation when we start from 3 and want to reach to 2.Could anyone explain me why this is not affecting final answer to problem?
•  » » 5 years ago, # ^ |   +11 If at any moment the sum becomes negative then the path is not optimal since you can remove the negative part, so even if you incorrectly consider it, the answer wont change since there will be a better solution anyways.
•  » » » 5 years ago, # ^ | ← Rev. 2 →   0 Thanks!!But how can you guarantee that there will always be a better solution ?
•  » » » » 5 years ago, # ^ |   +3 Because removing the negative prefix leads you to a bigger sum
 » 5 years ago, # |   0 Why ⌊v−(s+n−1)/n⌋ not ⌊v − s/n⌋ + (s % n) ? 1 : 0 ?
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 That are the same thing.
 » 5 years ago, # |   +3 Can anyone explain in detail how to solve Div. 2 D?(I will be very grateful.)
•  » » 5 years ago, # ^ |   0 Google Kadane's algorithm (for finding maximum sub-array sum) and then compare that technique here also. It helps me!
•  » » » 5 years ago, # ^ |   0 great! it helps a lot!
•  » » » 5 years ago, # ^ |   0 Nice post! Can you help me out in this? I have used the in-out dp approach to solve this problem. I don't know why it is failing on test case 34. https://codeforces.com/contest/1084/submission/46897634
•  » » » » 5 years ago, # ^ |   +3 I don't actually know what's the in-out dp approach that you are referring to. But anyways, I don't think any other person would understand your code better than you do. So I'd suggest you to find out the reason for why it fails on your own. You might end up learning a new thing from that.
•  » » » 5 years ago, # ^ |   0 Will you please go through my test case just 6-7 comments above. I think that don't come in both of these categories.
•  » » » » 5 years ago, # ^ |   +6 Consider case 2 of my proof. Here, x = 1 and b = 3. The algorithm at vertex 1 considers the optimal path to be of value 13 + 10 = 23. This is the only possible path. But on this path, the fuel becomes negative. This happens at the edge (u, v) where u = 1, v = 3. Now, in this case, it is optimal to start at the vertex v = 3, where the answer = 24. I agree that it's an invalid path and the algorithm considers it. But that's exactly what case 2 states, if this would have been a path that our algorithm considered as the answer, the fuel would have become negative on the edge 1 -> 3 and we would have been better off starting off at 3. But yes you're right. Here the algorithm at the vertex 1 chooses a path that is not possible. So, we can't say that the algorithm actually finds a valid path passing through every vertex, but we can guarantee that it finds an optimal path based on the reasoning given by the above 2 cases.
•  » » » » » 5 years ago, # ^ |   0 Ok got it. Thanks for going through my test case.
 » 5 years ago, # |   0 Can someone please explain problem number D of Div. 2 ?
•  » » 5 years ago, # ^ |   -7 Look at my reply to the above comment.
•  » » » 5 years ago, # ^ |   +7 Why these downvotes ? I just wanted to ensure that he gets a notification in case he doesn't look at this blog post again thinking that he got no reply.
 » 5 years ago, # |   +10 Thanks for your fast Editorials. <3
 » 5 years ago, # |   +10 Prob div1E: Will a greedy solution work instead?For each rectangle I define net exclusive area to be: "exclusive" area of each rectangle (which is area not shared by any other rectangle) — a[i]. The greedy solution is to find the rectangle with the lowest exclusive next area and delete it(only if the value is negative) and update the neighbor rectangle values and continue the process.
•  » » 5 years ago, # ^ |   +8 If we have 3 rects and lowest negative value at middle. There is can be situation when optimal solution is only middle. But your algo will exclude it. For instance: 3 3 7 1 4 3 4 1 3If deleted rect will be always utmost than it will give correct solution.
•  » » » 5 years ago, # ^ |   0 "If deleted rect will be always utmost than it will give correct solution." Can you explain this?
•  » » » » 5 years ago, # ^ |   +8 On samples where you algo will always delete rect that is utmost among left ones, you will get correct answer. Otherwise result correctness is not guaranteed.
 » 5 years ago, # |   +5 Why do the rectangles in problem E have to be non-overlapping for the solution to work?
•  » » 5 years ago, # ^ |   0 They are overlapping, but not nested. If they were, finding area of intersection would not be as easy as xj·yi.
•  » » » 5 years ago, # ^ | ← Rev. 3 →   +5 Actually most of solutions will work without any changes. Obviously, optimal answer will not contain nested rectangles, so we can't get worse answer. Also if we left the same formula for intersection, pair of nested rectangles will give less score, than it should give actually. So we will not get the answer, that is bigger than optimal.
•  » » » » 5 years ago, # ^ |   0 Thanks, I hadn't thought of that. The only thing is that yi will no longer be in decreasing order. The nested rectangles can also be removed in linear time for convenience before doing dp, if I'm not wrong.
•  » » » » » 5 years ago, # ^ |   +5 Oh, I doubt that they can be removed, because they have different ai.The test for example: 3 2 4 0 3 5 6 10 3 0 
•  » » » » » » 5 years ago, # ^ | ← Rev. 2 →   0 I didn’t say to remove them, just keep the solution unchanged. My argument is that in the solution itself there shouldn’t be any nested rectangles, and the dp formula somehow compensates that by giving a negative score to having nested rectangles. Am I missing something?EDIT: Never mind, I misinterpreted your last comment.
•  » » 5 years ago, # ^ | ← Rev. 3 →   0 As I see only reason is that with this assumption simpler to build envelope.Even as sad in editorial total time "O(n) if rectangles are already sorted".
 » 5 years ago, # | ← Rev. 2 →   0 can anyone explain me this part in Problem C (DIV 2): For each i (1≤i≤k), spi= 'a'. For each i (1≤i
•  » » 5 years ago, # ^ |   0 We must create an array 'p' of indices that correspond to the letters 'a'. Between all adjacent elements of this array should be an index corresponding to 'b'
 » 5 years ago, # |   0 For Div1E I've wondered that my O(nlgn) solution get TLE for 2sec with n=1e6. Even after using vector instead map and releasing tie between cin and cout.
 » 5 years ago, # | ← Rev. 2 →   +28 There is an easier way to solve problem Div2 E.Let dp[i] represent the number of all distinct prefixes that end at i and are not less than S's prefix or larger than T's prefix, initially dp[0] is 1, and we treat strings as one based.Initially, dp[i] = dp[i — 1] * 2, because you can expand all previous options by an 'a' or 'b'.However, this recurrence counts prefixes larger than t and smaller than s, but the over counted strings can be at most 2, which happens if s[i] == 'b', or t[i] == 'a'. but in these cases you can remove them individually. (They can happen when you use S[1..i], and extend it by an 'a', or use T[1..i], and extended it by a 'b')Once all prefixes are calculated, the answer is the sum of the dp array, but any dp[i] can't contribute larger than K prefixes ( since we need at most K distinct strings), so instead dp[i] contributes by min(dp[i],k)
•  » » 5 years ago, # ^ |   0 This approach could also be extended if the question did not limit to using only 'a' and 'b' but had rather used the entire lowercase letters.
•  » » 5 years ago, # ^ |   0 Thanks man! I loved this solution
•  » » 5 years ago, # ^ |   0 so elegant !
•  » » 3 years ago, # ^ |   0 helped a lot
 » 5 years ago, # |   +10 In 1083E, shouldn't the formula be dpi =  dpj + xi.yi - xj.yi - ai. Otherwise it will just give the area covered by all the rectangles.And what is the convex hull trick? Can someone explain/provide a link.
•  » » 5 years ago, # ^ |   0 Never mind found it: Convex Hull Trick
 » 5 years ago, # |   0 Could someone explain what sequences do I need to count in Div2-C? Are they "aaa..." or" aaa...baaa..."?
 » 5 years ago, # |   0 Can anyone explain div 2E ? In the editorial, I understood till the part where problem is reduced to (k-2) strings and the common prefix of S and T are removed. But after that (from second paragraph), I can't understand much. I have understanding of both tries as well as binary tree. Can anyone help?
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 Each string is a path in trie. We want to choose k pathes between s and t and maximize c, which is equal to number of vertexes in union of them. For every "a" in string s and every "b" in string t we have complete binary tree in trie with some height.In complete binary tree with height h we can consider multiset {h, h - 1, h - 2, h - 2, h - 3 (4 times), ...}. It can be proved that maximal union of l vertical ways in complete binary tree is sum of first l elements of this array, and if we want to choose some number of ways in two trees, we can combine their multisets. Let's store this huge set as array p, where pi is count of element h - i. For complete binary tree p looks like {1, 1, 2, 4, ...}. We can sum arrays to combine multisets.It's not difficult to sum arrays for O(n) complete binary subtrees fast.
 » 5 years ago, # | ← Rev. 2 →   0 Could somebody help me find out my mistake in my solution?It fails on the 14th test casehttps://codeforces.com/contest/1084/submission/46908137So basically i have three 1-D arrays up,down and cross. Initially i root the graph at node "1" to form a treeup[i] holds the value for the maximum gasoline possible ending for an upwards path ending at idown[i] holds the value for the maximum possible gasoline on a downwards path starting at icross[i] holds the value for maximum possible gasoline on a path with i as the turning vertexInitially up[i]=down[i]=cross[i]=gasoline[i] for all i up[i] is nothing but maximum over up[i] and up[j]-(distance between i and j)+gasoline[i] for some child j of isimilarly down[i] is maximum over down[i] and gasoline[i]-(distance between i and j)+down[j] for some child j of icross[i] is (for all pairs of children j and k): maximum over 1) cross[i] 2) (going from j->k) up[j]-(distance between i and j)+gasoline[i]-(distance between i and k)+ down[k]3) similarly go from k->jWhat is wrong with my logic? :( I have read a solution below but i would also like to know what is wrong with my solution.I'll be very grateful if you could help me out cause i've been struggling for a while. Thanks in advance
 » 5 years ago, # |   +8 Is the runtime for problem C missing a Qlog2N term? On each query swapping two values, each of the logN updates to the segment tree's nodes may involve an LCA lookup in logN.
•  » » 5 years ago, # ^ |   +8 You can do it in O(1) using Sparse-Table.
•  » » » 5 years ago, # ^ |   +8 Didn't know about that, thanks!
•  » » 5 years ago, # ^ |   0 Actually if you store middle node for non-vertical path too, you'll need to compute lca not more than once per query, because it happens only when you merge to vertical paths and in result you get a non-vertical one. So you can still compute lca with binary jumps.
•  » » » 5 years ago, # ^ |   +8 Interesting. How can you perform the case where two vertical paths add to a vertical path in O(1) if you are doing LCA with binary jumps?
•  » » » » 5 years ago, # ^ | ← Rev. 2 →   0 If two paths together form a vertical path too, you can say, that it’s endpoints are nodes with the maximum and the minimum depth. After that you just check that all other nodes lie on a path between them.
•  » » » » » 5 years ago, # ^ |   +8 How can you check that the other nodes lie between them without doing LCA lookups?
•  » » » » » » 5 years ago, # ^ |   +8 It is enough to have isParent(u, v) function, which uses only in and out times.
 » 5 years ago, # | ← Rev. 2 →   -8 Div 2 C: Can anyone explain what's wrong with my code? Maybe I divide on ‘a’ — bloks incorrectly? 46914444
•  » » 5 years ago, # ^ |   0 Your mod constant is wrong. It's 1e9 + 7, not 10e9 + 7.
•  » » » 5 years ago, # ^ |   -16 No, it is correct.
•  » » » » 5 years ago, # ^ |   0 It's wrong mate. In C++, 1e9 === 1,000,000,000. You have used 10e9 == 10,000,000,000.
•  » » » » 5 years ago, # ^ |   +8 Whatever you say, boss. :PBesides, you access beyond the vector's range — the condition 'ptr != n' in the two inner while loops should be the first.
 » 5 years ago, # |   0 Guys, what is needed to solve Div2 Problem D? I know DFS but I am not able to understand the approach to be used for this problem. For me, the editorial above is providing no clue/ help. Can someone help me with some explanation on this problem?Thank you.
•  » » 5 years ago, # ^ | ← Rev. 2 →   0 For a node i, dp[i] denote the answer when you start at node i and move down. you calculate dp[i] as d[leaf] = w[leaf] and dp[i] = max{dp[j] + w[i] - c(i, j) : j is direct child of i }. Consider a path which has maximum value, this path has a node N which is at minimum depth, either the path starts at this node and goes straight down or passes through N and divides into two paths both of which moves down. So to calculate the answer you take maximum of dp[i] — path moves straight down or,dp[j1] + dp[j2] + w[i] - c(i, j1) - c(i, j2) — path passes through node i and moves down to j1 and j2 with j1, j2 as direct child of i.and update the answer. here is the code https://codeforces.com/contest/1084/submission/46876865
 » 5 years ago, # |   +10 If I understood correctly the explanation for div-1 B, it will be better to change "Let's add the first 1 at the end." to "Let's add the first 1 in the end.". So the idea is like this, let's name our array a which has length m, we have to add [1, 1, 2, 4, 8, ...] (length is H) to our arrays last H elements. To do this we forget about first 1 and add second 1 to am - H + 1. We do this for each group. Then start from element a0 and add 2 * ai to ai + 1. And finally add every 1, which we intentionally forgot, for each group, at the needed index.
•  » » 5 years ago, # ^ |   0 Thanks, you are right. I'll fix it.
 » 5 years ago, # |   0 Does this Div. 1 F require many constant optimalizations? I came up with the intended solution but got TLE on test 10 or 11 plenty of times.
•  » » 5 years ago, # ^ |   +15 Try submitting your solution with GNU C++ compiler without diagnostics.
•  » » » 5 years ago, # ^ |   0 Got Accepted. Thanks!
 » 5 years ago, # |   0 In the solution of div2 B you wrote that if sum of all a[i] is <=s the answer is -1, but actually if sum==s the answer is 0.
 » 5 years ago, # |   0 Can somebody explain what is going on in problem : https://codeforces.com/problemset/problem/1084/B ?What does least mean in kvass level in the least keg is as much as possible.?
•  » » 5 years ago, # ^ |   0 we can get 1 liter of kavss from any not empty kegs, and each time we pour 1 liter from a keg, the level of it will decrease by 1. After we get s liters, the levels in some kegs will be decreased. So our task is to maximize the lowest level.
 » 5 years ago, # |   0 How to do the Div1 C question, the editorial is unclear to me. I am not able to understand how the queries to swap the values of the two nodes will work with the segment tree. Please help
 » 5 years ago, # |   -10 |p−x|+|x−1|+|p−1|=2(max(p,x)-1) CAn somebody explain this ??.. I am a newbie
•  » » 5 years ago, # ^ |   -10 If we expand modules we will get this:So we know from problem situation that it is always true.
•  » » 5 years ago, # ^ | ← Rev. 2 →   -10 When you get the formula |p−x|+|x−1|+|p−1|, you know that x >= 1 && p >= 1, so then you will convert it to |p-x| + x + p - 2. Then just expand the module and you will get the final formula.
»
5 years ago, # |
-13

I am is a beginner and i was solve to proble A, but i have a problem with this cide:

# include <bits/stdc++.h>

using namespace std; int main() { int n; cin >> n; vector a(n); for (int i = 0; i < n; i++) cin >> a.at(i); vector sorted(n); copy(a.begin(), a.end(), sorted.begin()); sort(sorted.begin(), sorted.end()); int x; for (int i = 0; i <= n; i++) if (sorted.back() == a.at(i)) { x = i; break; } int onX, nX; for (int i = 0; i < n; i++) { if (a.at(i) == a.at(x)) onX += x * 4 * a.at(i); nX = (i — x) * 2; if (nX < 0) i *= -1; nX += i * 2; nX += x * 2; nX *= a.at(i); } int sum = onX + nX; cout << sum << endl; return 0; }

When i was compiling that code using my compiler that was has result 16 (correct). But when i submitting this code on pretest 1 that not has result 16 (that result is 28, and sometimes resulting random number) What's wrong??

•  » » 5 years ago, # ^ |   0 You are using values of uninitialized variables
 » 5 years ago, # | ← Rev. 3 →   0 nvm
 » 4 years ago, # |   0 DIV 2 -> Problem D — The Fair Nut and the Best PathCan anyone please explain the meaning of line in italics in this statement?Nut wants to rent a car in the city u and go by a simple path to city v. He hasn't determined the path, so it's time to do it. Note that the chosen path consists of only one vertex.
•  » » 4 years ago, # ^ |   +3 Please read the original statement again. It says "the chosen path CAN consist of only one vertex". This means the answer can be the amount of gasoline that can be filled at a particular station.
•  » » » 4 years ago, # ^ | ← Rev. 4 →   0 Very kind of you to answer.But the problem was solved.Thanks anyways.And an upvote too...Happy Coding.