### nitesh_gupta's blog

By nitesh_gupta, history, 12 days ago, ,
Author's code
Author's code
Author's code
Author's code
Author's code

•
• +91
•

 » 12 days ago, # |   +125 Can you use Latex or whatever it is called for D and E also
 » 12 days ago, # |   +2 You should have made a div1+div2 round with these div2 D/E as div1 D/E
 » 12 days ago, # | ← Rev. 2 →   -33 a = div3a , b = div2 c , c = div2 d , d = div1 d , e = div1e
 » 12 days 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.
•  » » 12 days 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.
•  » » 12 days 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 >_<
•  » » 12 days 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.
•  » » » 12 days 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 )
 » 12 days ago, # | ← Rev. 2 →   0 After the testing i got wa on test 109 on B wtf =((
•  » » 12 days ago, # ^ |   +1 Lol, same.
•  » » 12 days ago, # ^ |   0 i got one on 110
 » 12 days ago, # |   +21 I just love fast editorials <3. Thanks a lots <3
 » 12 days ago, # |   -27 looking from the editorials of d and e it looks like u too were not interested in contest
 » 12 days ago, # |   +1 Video editorial: https://youtu.be/082mInmBC8Q?t=327 for Problem A Superhero Transformations using C++17's transform_reduce:1111A - Superhero Transformation
 » 12 days ago, # |   0 For c you can just inplement a lazy created rangetreeThe time complexity is more sensitively O(kn)
 » 12 days ago, # |   0 What made so many people lose B?
•  » » 12 days 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.
•  » » 9 days ago, # ^ |   0 Because there will be a problem if you use printf("%Lf") and choose G++11
 » 12 days 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.
 » 12 days ago, # | ← Rev. 2 →   0 In D's editorial removal of character, shouldn't the loop has to go from x to n.
•  » » 12 days ago, # ^ |   +5 Yes, you are right.Updated now.
 » 12 days ago, # |   0 In problem D ,can jarvis make any arrangement?
•  » » 11 days ago, # ^ |   0 Yes, any permutation is a product of transpositions
•  » » » 11 days 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?
 » 12 days ago, # |   +2 Why this solution get WA 4? I don't know why is not correct
 » 11 days 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..!
•  » » 11 days 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)
•  » » 11 days 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 :)
 » 11 days ago, # |   0 Can Prob.B be solved with a greedy algorithm?
•  » » 11 days ago, # ^ |   0 It is already solved with greedy algorithm: we are trying to remove only minimal elements.
•  » » » 11 days ago, # ^ |   0 How to prove that the algorithm is correct
•  » » » » 11 days 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.
•  » » » » » 11 days ago, # ^ |   0 Tks !
•  » » » » » » 6 hours ago, # ^ |   0 https://codeforces.com/contest/1111/submission/49695185my solution is getting WA on 15 , anybody help please.
•  » » » » » » » 4 hours ago, # ^ |   +5
•  » » » » » » » » 4 hours ago, # ^ |   0 Thanks , i got it
•  » » » 11 days ago, # ^ |   0 Thank you for your answering
 » 11 days ago, # |   0 Deceived badly in B!
 » 11 days 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
•  » » 11 days 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
•  » » 11 days 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).
•  » » » 11 days ago, # ^ |   0 Hi @tech_mint0013 thanks for looking into my code and clearly pointing out the error, really appreciate !!!
•  » » » 11 days ago, # ^ |   0 But in the worst case(one avenger in each node) time complexity is still O(2^30).
•  » » » » 11 days 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
 » 11 days 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?
•  » » 11 days 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
•  » » » 11 days ago, # ^ |   +7 Not unlucky, fair and square an error on my side. Thanks for pointing that out!
 » 11 days 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.
 » 11 days 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?
•  » » 11 days 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.
•  » » » 11 days ago, # ^ | ← Rev. 2 →   +3 Try e.g. the test: AABBCCDDEEFF 1 1 3 
•  » » » 11 days ago, # ^ |   +19 Thats where I was confused.I thought only the selected letters have to follow the rule.
 » 11 days ago, # |   0 Could anyone solve D in O(nk)?
 » 10 days ago, # |   0 Can anyone explain me problem D ? after we find result with O(n*k*k*k) for number of good string ?
•  » » 10 days 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.
•  » » » 10 days 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.
•  » » » » 6 days 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).
 » 10 days ago, # |   0 can you give me some links about Euler tree (E)?
•  » » 10 days ago, # ^ |   +1
•  » » » 10 days ago, # ^ |   0 oh thank you
 » 10 days ago, # |   0 someone plz explain problem D.
 » 10 days 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.
•  » » 9 days 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.
 » 9 days ago, # |   +8 Why this round problems do not have difficulty tag?
 » 6 days 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.
•  » » 4 days 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).
•  » » » 4 days ago, # ^ |   0 Thank you!
 » 6 days 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.
 » 4 days ago, # |   0 Is it possible to do div2 C without recursion ?
 » 4 days ago, # | ← Rev. 4 →   0 .
 » 3 days 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.