Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

### nitesh_gupta's blog

By nitesh_gupta, history, 3 years ago,
Author's code
Author's code
Author's code
Author's code
Author's code

• +91

 » 3 years ago, # |   +125 Can you use Latex or whatever it is called for D and E also
 » 3 years ago, # |   +2 You should have made a div1+div2 round with these div2 D/E as div1 D/E
 » 3 years ago, # | ← Rev. 2 →   -33 a = div3a , b = div2 c , c = div2 d , d = div1 d , e = div1e
 » 3 years ago, # | ← Rev. 3 →   0 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.
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 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.
•  » » 3 years ago, # ^ | ← Rev. 2 →   0 Oh, I am very sorry but what I have just said is WRONG and there are no problem with that code, please forget it >_<
•  » » 3 years ago, # ^ | ← Rev. 4 →   0 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.
•  » » » 3 years ago, # ^ |   0 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 )
 » 3 years ago, # | ← Rev. 2 →   0 After the testing i got wa on test 109 on B wtf =((
•  » » 3 years ago, # ^ |   +1 Lol, same.
•  » » 3 years ago, # ^ |   0 i got one on 110
 » 3 years ago, # |   +21 I just love fast editorials <3. Thanks a lots <3
 » 3 years ago, # |   -27 looking from the editorials of d and e it looks like u too were not interested in contest
 » 3 years ago, # |   +1 Video editorial: https://youtu.be/082mInmBC8Q?t=327 for Problem A Superhero Transformations using C++17's transform_reduce:1111A - Superhero Transformation
 » 3 years ago, # |   0 For c you can just inplement a lazy created rangetreeThe time complexity is more sensitively O(kn)
 » 3 years ago, # |   0 What made so many people lose B?
•  » » 3 years ago, # ^ |   +30 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.
•  » » 3 years ago, # ^ |   0 Because there will be a problem if you use printf("%Lf") and choose G++11
 » 3 years ago, # |   0 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.
•  » » 3 years ago, # ^ |   0 You can say that again.
 » 3 years ago, # | ← Rev. 2 →   0 In D's editorial removal of character, shouldn't the loop has to go from x to n.
•  » » 3 years ago, # ^ |   +5 Yes, you are right.Updated now.
 » 3 years ago, # |   0 In problem D ,can jarvis make any arrangement?
•  » » 3 years ago, # ^ |   0 Yes, any permutation is a product of transpositions
•  » » » 3 years ago, # ^ |   0 " 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?
 » 3 years ago, # |   +2 Why this solution get WA 4? I don't know why is not correct
 » 3 years ago, # |   0 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..!
•  » » 3 years ago, # ^ |   0 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)
•  » » 3 years ago, # ^ |   0 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 :)
 » 3 years ago, # |   0 Can Prob.B be solved with a greedy algorithm?
•  » » 3 years ago, # ^ |   0 It is already solved with greedy algorithm: we are trying to remove only minimal elements.
•  » » » 3 years ago, # ^ |   0 How to prove that the algorithm is correct
•  » » » » 3 years ago, # ^ |   0 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.
•  » » » » » 3 years ago, # ^ |   0 Tks !
•  » » » » » » 3 years ago, # ^ |   0 https://codeforces.com/contest/1111/submission/49695185my solution is getting WA on 15 , anybody help please.
•  » » » » » » » 3 years ago, # ^ |   +5
•  » » » » » » » » 3 years ago, # ^ |   0 Thanks , i got it
•  » » » 3 years ago, # ^ |   0 Thank you for your answering
 » 3 years ago, # |   0 Deceived badly in B!
 » 3 years ago, # | ← Rev. 3 →   0 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
•  » » 3 years ago, # ^ | ← Rev. 3 →   0 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
•  » » 3 years ago, # ^ |   0 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).
•  » » » 3 years ago, # ^ |   0 Hi @tech_mint0013 thanks for looking into my code and clearly pointing out the error, really appreciate !!!
•  » » » 3 years ago, # ^ |   0 But in the worst case(one avenger in each node) time complexity is still O(2^30).
•  » » » » 3 years ago, # ^ |   +3 Maximum number of avengers given are 10^5 .So in the worst case only 10^5 nodes will be visited not 2^30
 » 3 years ago, # |   0 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?
•  » » 3 years ago, # ^ |   0 test case 37 was submitted by me (for hacking)u need long long when u do multiplication of k*notherwise it will exceed intUNLUCKY
•  » » » 3 years ago, # ^ |   +7 Not unlucky, fair and square an error on my side. Thanks for pointing that out!
 » 3 years ago, # |   0 May I know the reason why this round is unrated? It never stops to happen on codeforces, I experience this for the fourth time.
 » 3 years ago, # |   +6 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?
•  » » 3 years ago, # ^ | ← Rev. 2 →   +3 "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.
•  » » » 3 years ago, # ^ | ← Rev. 2 →   +3 Try e.g. the test: AABBCCDDEEFF 1 1 3 
•  » » » 3 years ago, # ^ |   +19 Thats where I was confused.I thought only the selected letters have to follow the rule.
 » 3 years ago, # |   0 Could anyone solve D in O(nk)?
 » 3 years ago, # |   0 Can anyone explain me problem D ? after we find result with O(n*k*k*k) for number of good string ?
•  » » 3 years ago, # ^ |   +10 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][0] = 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.
•  » » » 3 years ago, # ^ | ← Rev. 3 →   +6 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.
•  » » » » 3 years ago, # ^ |   +16 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).
 » 3 years ago, # |   0 can you give me some links about Euler tree (E)?
•  » » 3 years ago, # ^ |   +1
•  » » » 3 years ago, # ^ |   0 oh thank you
 » 3 years ago, # | ← Rev. 2 →   0 deleted
 » 3 years ago, # |   0 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.
•  » » 3 years ago, # ^ |   +5 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.
 » 3 years ago, # |   +8 Why this round problems do not have difficulty tag?
 » 3 years ago, # |   0 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.
•  » » 3 years ago, # ^ |   0 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).
•  » » » 3 years ago, # ^ |   0 Thank you!
 » 3 years ago, # |   0 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.
 » 3 years ago, # |   0 Is it possible to do div2 C without recursion ?
 » 3 years ago, # | ← Rev. 4 →   0 .
 » 3 years ago, # |   +5 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.
 » 3 years ago, # |   0 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.
•  » » 3 years ago, # ^ |   0 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
 » 3 years ago, # |   0 Problem C Div2 My solution not getting submitted : https://codeforces.com/contest/1111/submission/50086650
 » 3 years ago, # |   0 I have posted a doubt related to the problem 1111 E here. No one is responding Kindly help !!
 » 3 years ago, # |   0 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.
•  » » 17 months ago, # ^ |   0 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.
 » 2 years ago, # | ← Rev. 3 →   0 1
 » 19 months ago, # | ← Rev. 2 →   +3 Good C, D, E problems. some of those were damn instructive .
 » 16 months ago, # |   0 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 ?
•  » » 16 months ago, # ^ |   0 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.
•  » » » 16 months ago, # ^ |   0 but won't the time complexity in worst case be same even if we recur ?
•  » » » » 16 months ago, # ^ |   +1 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.
•  » » » » 16 months ago, # ^ | ← Rev. 2 →   0 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.
 » 11 months ago, # |   0 For problem C I think we can achieve $O(k)$, by keeping the $[l, r]$ index range in each recursion, and only perform binary search there in $O(1+\log (r-l))$ time. Then we can write the recursive formula as $T(k) = T(x) + T(k-x) + \log k+1$, for some $x$. Then we use concavity of the log function to get $T(k) \le 2T(k/2) + \log k+1$. This solves to $T(k) = O(k)$ by the master theorem.