### nitesh_gupta's blog

By nitesh_gupta, history, 16 months ago, , Author's code
Author's code
Author's code
Author's code
Author's code  div2, Comments (85)
 » Can you use Latex or whatever it is called for D and E also
 » You should have made a div1+div2 round with these div2 D/E as div1 D/E
 » 16 months ago, # | ← Rev. 2 →   a = div3a , b = div2 c , c = div2 d , d = div1 d , e = div1e
 » 16 months ago, # | ← Rev. 3 →   Hi can someone explain why this dp soln for C is wrong. Link 49433152 I have taken the dp state as the length of the segment and the no. of avengers in the segment. The code is obviously redundant but I am still confused that why is it not working.
•  » » 16 months ago, # ^ | ← Rev. 2 →   Hi, some avengers are possiblely in the same position so this code upper_bound(pos.begin(), pos.end(), b) will return a wrong result. I think this is the preblem of your solution.
•  » » 16 months ago, # ^ | ← Rev. 2 →   Oh, I am very sorry but what I have just said is WRONG and there are no problem with that code, please forget it >_<
•  » » 16 months ago, # ^ | ← Rev. 4 →   Please excuse my poor English. I think the real problem is that your state don't have all information in a segment. Just think a situation, A = 1 and B = 1000. There are two segments [1, 2] and [3, 4], and for each one there are two avengers inside. These 4 avengers are in postions 1 1 3 4. It's obviously that the answer of first segment is 2001 and the answer of the second one is 2000. But your state will think they are same. So you have a wrong state.
•  » » » Thank you. In the actual solution the code will be run for exactly 2k+1 times right because of k position of avengers and k+1 empty segments. So in a way k puts a limit over 2^n . Also in my dp solution I am not considering the distribution in a state which obviously matters.(same thing I did with problem A and got hacked )
 » 16 months ago, # | ← Rev. 2 →   After the testing i got wa on test 109 on B wtf =((
•  » » Lol, same.
•  » » i got one on 110
 » I just love fast editorials <3. Thanks a lots <3
 » looking from the editorials of d and e it looks like u too were not interested in contest
 » Video editorial: https://youtu.be/082mInmBC8Q?t=327 for Problem A Superhero Transformations using C++17's transform_reduce:1111A - Superhero Transformation
 » For c you can just inplement a lazy created rangetreeThe time complexity is more sensitively O(kn)
 » What made so many people lose B?
•  » » Weak pretests + previous expectations.You usually don't have to make observations to solve div2B, but for many people the problem was unintuitive without making an observation: you will never increase a superhero that you delete. Seems obvious, but many people quickly went off on incorrect greedies because they did not stop to think. Even worse, these incorrect greedies passed pretests, which deceived people into thinking they got it.The easy way to solve it is just realize that since you don't increase and delete the same superhero, you can separate them into "deleted" superheroes and "upgraded" superheros. After this the bruteforce is quite obvious.
•  » » Because there will be a problem if you use printf("%Lf") and choose G++11
 » I don't think it's proper of a Div.2 Test to have so hard a problem D and such weak pretests of problem B. In my room only two participants passed the final tests of problem B, but over ten participants passed the pretest. Some solutions are wrong obviously, but they can pass the pretest, too.
•  » » You can say that again.
 » 16 months ago, # | ← Rev. 2 →   In D's editorial removal of character, shouldn't the loop has to go from x to n.
•  » » Yes, you are right.Updated now.
 » In problem D ,can jarvis make any arrangement?
•  » » Yes, any permutation is a product of transpositions
•  » » » " all villains of a certain type either live in the first half of the colony or in the second half of the colony"-by certain type does it mean the type(s) at position x and y?
 » Why this solution get WA 4? I don't know why is not correct
 » In problem DIV2/C. Can someone please explain me how are we calculating number of avengers between l to r,I mean how exactly the upper_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l) tells the exact number of avengers from l to r? Thanks in advance..!
•  » » In a sorted vector v, upper_bound(v.begin(),v.end(),y) gives the number of elements less than or equal to y in v. lower_bound(v.begin(),v.end(),x) gives the number of elements less than x in v. So, number of elements in v that are less than or equal to y and greater than or equal to x, i.e. in range [x,y] = upper_bound(v.begin(),v.end(),y)-lower_bound(v.begin(),v.end(),x)
•  » » say we're looking for number of elements within range [r,l] and vector v is sorted. upper_bound(v.begin(),v.end(),r)-v.begin() would return the position j of the least element not within the range lower_bound(v.begin(),v.end(),l)-v.begin() would return the position i of the least element within the range if it does exist (or the least element not withing range if there's no element withing the range which means i=j in this case) now in v[i],v[i+1],...,v[j-1],v[j] all elements v[i],..,v[j-1] would be within range no the total of elements within [a,b] would be (j-1)-i+1=j-i if there's no element within [a,b] as I stated before i=j so j-i=0 which is what we want I hope this helps :)
 » Can Prob.B be solved with a greedy algorithm?
•  » » It is already solved with greedy algorithm: we are trying to remove only minimal elements.
•  » » » How to prove that the algorithm is correct
•  » » » » Suppose that we got answer, where we removed x instead y, where x > y. If we remove y instead x we will increase sum of element on x - y and increase average value, and increase result. So, we need to remove y instead x.
•  » » » » » Tks !
•  » » » » » » https://codeforces.com/contest/1111/submission/49695185my solution is getting WA on 15 , anybody help please.
•  » » » » » » »
•  » » » » » » » » Thanks , i got it
 » 16 months ago, # | ← Rev. 3 →   i was trying to upsolve C problem using editorial approach https://codeforces.com/contest/1111/submission/49458002 (code is simple/easy to read) , I am getting TLE if anyone who solved C can suggest probable erroneous lines or any guess of eror , will be thankful
•  » » 16 months ago, # ^ | ← Rev. 3 →   found 2 errors : a.) if( count in [l, r] == 0 ) return cost without recursive calls (maybe timeout reason) b.) in base case (l==r) don't do binary search, you need to consider duplicates in cost
•  » » In your code you are making calls to your f() fn even if there are no avengers in [l,r].This is making your time complexity O(2^30) since you are visiting each node.You should instead return A if there are no avengers in [l,r] since breaking it down to 2 or more intervals will only increase answer (A+A..>A).
•  » » » Hi @tech_mint0013 thanks for looking into my code and clearly pointing out the error, really appreciate !!!
•  » » » But in the worst case(one avenger in each node) time complexity is still O(2^30).
•  » » » » Maximum number of avengers given are 10^5 .So in the worst case only 10^5 nodes will be visited not 2^30
 » For Div2 B, I have approximately the same code as given in the tutorial, but in Java. The code fails for test case 37. Here it is. So, can anyone tell what exactly I did wrong?
•  » » test case 37 was submitted by me (for hacking)u need long long when u do multiplication of k*notherwise it will exceed intUNLUCKY
•  » » » Not unlucky, fair and square an error on my side. Thanks for pointing that out!
 » May I know the reason why this round is unrated? It never stops to happen on codeforces, I experience this for the fourth time.
 » My WA solution for D: 1. I calculated the number of permutations of the selected letter types in one half. 2.Then I multiplied it by the permutation of the remaining letters. 3.I multiplied the whole result by 2 as the permutation of the selected types could happen in either of the halves. Can anyone point out where I am wrong?
•  » » 16 months ago, # ^ | ← Rev. 2 →   "2.Then I multiplied it by the permutation of the remaining letters." This way you count also permutations in which non selected letters may violate the condition that all occurrences of a particular letter belong to the same half.
•  » » » 16 months ago, # ^ | ← Rev. 2 →   Try e.g. the test: AABBCCDDEEFF 1 1 3 
•  » » » Thats where I was confused.I thought only the selected letters have to follow the rule.
 » Could anyone solve D in O(nk)?
 » Can anyone explain me problem D ? after we find result with O(n*k*k*k) for number of good string ?
•  » » In a normal knapsack when adding an item, we have the following dpdp[i][j] = number of ways to get sum as j considering the first i items, and the transition is governed by dp[i][j] = dp[i - 1][j] + dp[i - 1][j - va[i]], val[i]= value of the ith item. Essentially we mean , To get sum as j, you can either exclude the ith item and use only i - 1 items, which is stored in dp[i - 1][j] or you can select the ith item, hence from the remaining i - 1 items you take all ways to generate j - val[i] as your sum, so that on adding val[i] it becomes j.Now, what we are given is a table using i items, and we want to compute ways to get sum j as using only i-1 items. Basically removal of an item from knapsack. The transition now is,dp[i - 1][j] = dp[i][j] - dp[i - 1][j - val[i]] ( I have just rearranged the above equation)This says,(ways of getting sum as j using only i - 1 items) = (ways of getting sum as j using i items) — (ways to get sum as j in which ith item was necessarily included)Here we know dp[i][j], but we do not know, dp[i - 1][j - val[i]]. But if we know dp[i - 1][j - val[i]], we know dp[i - 1][j]. Similarly if we know dp[i - 1][j - 2 * val[i]] , we know dp[i - 1][j - val[i]] and so on. And obviously dp[i - 1] = 1 since there is always one way of not selecting any element to get sum as 0.So what we do is we loop j from val[i] to n, and perform the operation dp[i - 1][j] = dp[i][j] - dp[i - 1][j - val[i]]Now from the ways to generate combinations adding upto j, we get ways to generate combinations without frequency of any particular alphabet. Thus for two alphabets (x,y) we have the ways to get sum as n / 2 without frequencies of x and y. We multiply a 2 to this because the group containing x and y can be placed in any of the halves.PS: I have removed one dimension from my dp hence it is 1D instead of 2D in the code.
•  » » » 16 months ago, # ^ | ← Rev. 3 →   D is a really cool problem IMO. But I couldn't quite grasp one part of the editorial, particularly the way you get to O(n * k2). Won't your "removal" algorithm that you use to obtain O(n * k2) result in the removal of any other character that appears as many times as the i-th one? If the state dp[k - buc[i]] is the number of ways to get k - buc[i], what happens if there is some character that one can use to get to k from k - buc[i]? EDIT: I got it, it is only subtracted once, so it only accounts for the removal of one character.
•  » » » » Damn, this took me a few days to solve.Here's an alternative solution to problem D. The idea is quite general in fact. Assume that we have some kind of a DP problem which involves processing a collection of O(k) elements, but we can process them in any order (like in knapsack), and each pass takes, say O(n), but, unlike in this problem, I won't assume that this forward-operation is reversible. Let's assume that the problem "gives" you k elements, then asks you to compute DP values for all k(k - 1) / 2 subsets of these k elements of size k - 2. This can be done as follows: Assume for simplicity that k = 2m for some m. Let s(x, y) be some sequence of numbers 1, ..., k such that x and y are missing from it. Note that we are free to permute this sequence as we see fit. If we could take all these sequences and store them in a trie and this trie ends up having no more than O(k2) nodes, we can compute DP values (in the leaves of the trie) by recursively solving for every node. Unfortunately the obvious way to form the sequence s(x, y) = [1, 2, ..., x - 1, x + 1, x + 2, ..., y - 2, y - 1, y + 1, y + 2, ..., k] does not work because the trie will have O(k3) nodes. Fortunately there is a better way. Form the sequence s by repeatedly finding the largest value w and then the smallest value i such that w is a power of two and i - 1 is divisible by w, such that all numbers in [i, i + w) are unused and different from x, y, then add these numbers to s(x, y). For example, if k = 8, x = 1, y = 3, s(x, y) = [5, 6, 7, 8, 2, 4]. When we insert all these sequences into the trie it will only use around 3k2 / 2 nodes, so the algorithm works in O(nk2).
•  » »
•  » » » oh thank you
 » 16 months ago, # | ← Rev. 2 →   deleted
 » I didn't fully understand the explanation for the complexity of the solution for C and I would be very grateful if someone could explain it to me.how is the complexity for C not (roughly) o((2^n*log(k))??Let's imagine a tree similar to an interval tree. Node 1 is the interval (1,2^n). It has two children, 2 and 3, the intervals (1,mij),(mij+1,2^n). The tree obviously has 2^n*2-1 and the recursive function you described would go trough all of them, RIGHT? Then every node or interval considered has a binary search within it of o(log(k)), so the end result should be o((2^(n-1)-1)*log(k)) I have absolutely no clue where o(n*k) comes from, it would me amazing if someone could tell me.
•  » » No you will not be going over all nodes in the segment tree. This is because once we reach a node with no avenger in it, we do not again recurse into it. You kind of do the same thing in a range query. Here on top of not recursing, you take a cost A, with you. 2^n ~ 10^9 >> 10^5. This implies there are lots of points and subsequently ranges which you never need to visit.Consider an example for more intuition : All avengers are in the first half. So since there is no avenger in the second half you will never go into that part and will get just a cost. Thus here itself, you reduced about 10^9/2 possible nodes to visit. If you want to think about how many intervals are we not recursing into, the idea is that maximum number of disjoint intervals without any avenger will be k+1. If you imagine these k+1 intervals as a range query, each will yield (O(log(2^n))=n), intervals in the segtree. And once you reach these you do not go any further. Thus the contributing factor are those intervals in which you have avengers, but the number of avengers is much less ~ 10^5, hence such intervals also become limited.
 » Why this round problems do not have difficulty tag?
 » Can someone help me with B, https://codeforces.com/contest/1111/submission/49674082 i am trying to solve it by removing i elements at a time.
•  » » You are allowed a maximum of m operations. So, the final loop should run till min(m, n-1) instead of n. (n-1 because you can not remove all elements).
•  » » » Thank you!
 » Hey guys, I am relatively new to the community. I attempted problem C. I implemented a modified binary search which seems to cover all the edge cases as well. I don't why I get a time limit exceeded on Testcase 5. It would be great if someone could look at my code and help me out. My code is 49673257 . Any kind of help would be deeply appreciated.
 » Is it possible to do div2 C without recursion ?
 » 16 months ago, # | ← Rev. 4 →   .
 » For E, I'm afraid you forget the dp part when analyzing the time complexity. For each query, it takes O(mk) time to do dp, and that's why the upper bound of m is necessary for the problem.
 » My solution for problem B:sort the array in decreasing order and start removing elements from the end. Remove until the first element of the array is not equal to the last element or until just 1 element is left in the array. Then we increment each element of this array from starting by k and go till the end or until the no. of operations end. But, this fails for the Test case:15 Here is the code : https://codeforces.com/contest/1111/submission/49983479 What am I missing.
•  » » Your forgot to use m!=0 inside the while loop. BTW, that's not the right logic. Test Case 109 will get you wrong.5 2 1 4 4 4 4 5
 » Problem C Div2 My solution not getting submitted : https://codeforces.com/contest/1111/submission/50086650
 » I have posted a doubt related to the problem 1111 E here. No one is responding Kindly help !!
 » What is the problem with my submission? I am not able to figure it out.https://codeforces.com/contest/1111/submission/50346012Getting WA on testcase 37.
•  » » You sort the array increasing order, but then work on it like it wourld be sorted decreasing. One of both iterations must be in reverse order.
 » 14 months ago, # | ← Rev. 3 →   1
 » 3 months ago, # | ← Rev. 2 →   Good C, D, E problems. some of those were damn instructive .
 » Can someone please help me with 1111C - Creative Snap ? // if l is equal to r or if there is no avenger in the subarray, then do not go into recursion further if(l==r || x==0) if I remove x == 0 part, i.e. I recur even if there is no avenger, it gives TLE on testcase 5 (My TLE Solution ). I don't understand why so ? How does it change worst case complexity of the code ? and how is it guarenteed that the solution that results from going into recursion will be more than that by not going into recursion ?
•  » » Suppose in the range (l,r) the number of avengers are 0. So the power consumed is A but if we recur for (l,mid) and (mid,r), the number of avengers in (l,mid) and (mid,r) are still 0. So,they return >=2A. Hence it is always better to take A instead of recurring.
•  » » » but won't the time complexity in worst case be same even if we recur ?
•  » » » » K can be at max 10^5 and n can be 2^30 so you can see that there will be too many gaps, even if we try to put the avengers at distinct places the maximum places we can fill are 10^5. Calling recursion on the remaining empty gaps can be costly.
•  » » » » 10 days ago, # ^ | ← Rev. 2 →   In worst case, your time complexity would be 2^30, since you are visiting every index. This is written somewhere above so you can refer that.