cannor147's blog

By cannor147, 4 months ago, , Authors: MikeMirzayanov and geranazavr555

Authors' solution

Author: MikeMirzayanov

Authors' solution

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution

Author: MikeMirzayanov

Authors' solution

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution

Authors: MikeMirzayanov, cannor147 and geranazavr555

Authors' solution Tutorial of Codeforces Round #568 (Div. 2) 568, Comments (57)
 » very good contest!
 » 4 months ago, # | ← Rev. 2 →   How to solve C2 if 't' constraint would have been t<=10^5 (without segment tree and treap) ?
•  » » Use two Priority Queues, in one keep those that pass and in the other those that fail. Before printing the result, you have to fail as students as needed to sum less than M. Then, while the greatest from the pq that passes is greater than the smallest of the pq that fails, swap them. Finally, while you can pass the smallest from the pq that fail for free, do it.My submission during contest: 55775417
•  » » » So you're pushing/popping same element multiple times. How are you sure this will work in the given time constraint? Can you please explain a bit more?
•  » » » I have used a similar method but couldn't get the answer ...What I am is doing deleting removing the greatest from the pq till the sum is less than M and then adding the smallest from the pq that fails until the sum is than M ...Can someone help me know why won't this method work?
•  » » » » 4 14 5 5 10 3
•  » » » » » Thanks...
•  » » » I solved this with a similar approach here: 55809687, but I believe this solution also depends on the low t constraint. The amount of elements that can be popped from the priority queue at the end depends on the possible difference in answers between two subsequent indices. With the current constraints, the possible difference between t values of two indices is 99, and since each t value must be at least 1, the difference in answer can be at most 99. If you were to have t go up to m, you could construct a case where you alternate t values between 1 and m. For the cases where t is 1, you only need to remove the t values that are equal to m, and thus the answer is current index / 2. For the cases where t is equal to m, you need to remove all previous values and thus the answer is the current index. Here, the amount of elements popped and pushed is O(n^2) and will not pass.
•  » » » In your code you have written saco.top(); inside the while loop. What is the value of saco.top() when i=0 and you haven't put anything inside it. Can you help explain the code ?
•  » » » » when i=0 then u r correct that there will be nothing in pq but u will never go inside that while as first element will always be less than M. also this is mentioned in problem statement ** It's guaranteed that all values of ti are not greater than M. **
•  » » I used two BIT arrays to find the answer, 55812830$su$ stores the prefix sum,$seen$ stores the sorted idx,
•  » » https://codeforces.com/contest/1185/submission/55759936Top programmers usually have the best solutions. He basically puts the students in a multiset (multiset automatically sorts things in O(logN)) and if the sum is bigger than M, he subtracts the biggest students in the multiset and prints the answer. Just doing this would give TLE so he also deletes students from his multiset if the sum is above M. For example, take this case: 5 100 80 40 40 40 60 first sum is 80, so no need to fail any students, output is 0.second sum is 120, so we need to fail one student, output is 1. Since the sum is now bigger than M, and it will always be bigger than M from now forward, its in our best interest to elements from the multiset until the sum is smaller than M.This solution would I think work regardless of the constraint of $t$
•  » » » 4 months ago, # ^ | ← Rev. 2 →   No, I don't think this solution works for large $t$.e.g. N = 20000, t = 10000, M = 10000and the array are1...(repeat 10000 times) 10000... (repeat 10000 times)
•  » » » » Maybe maintain an array that stores the sum after each $i$,and then when sum > M, we use binary search to find the first sum in the array that would get the current sum below M, maybe do $int index = lower__bound(sum_array.begin(),sum_array.end(),current_student)-sum_array.begin()$ ? Because sum-current_student is less than M. And then whatever index we get, we add it to every other result. This seems like a TLE as well tbh, but I'm too lazy to figure out a better solution.
•  » » » » » Can you please explain why this approach gives TLE isn't the complexity n(lgn+ n)
•  » » » After understanding your explanation, check for this 4 14 5 5 10 3
•  » » » » 0 0 2 1
•  » » I have used two Fenwick Trees + Binary Search. It will work for t <= 2e7. Code: 55863688
•  » » » Could u explain ur code a little bit for what purpose are u using Fenwick tree
 » Good Contest...Thanks for Fast Editorials
 » nice contest!!
 » Hey can someone help me in F Two Pizzas:In the editorial it has been written: "Then, we should go through all possible masks, that could be reached by two different pizzas.For each such mask we should keep two pizzas with the smallest total cost, which gives us that mask (i. e. their bitwise-or is equal to our current mask)."My question is how to choose two pizzas of min-cost that generate a particular mask without going back to brute-force which would, of course, be a TLE.
•  » » this solution may help you.
•  » » » Thanks mate
 » Though G1 was a bit easier than F, it's still a really good contest for div 2. The score distribution is a bit weird since i saw someone who solved 8 problems have a higher rank than someone who solved all.
 » 4 months ago, # | ← Rev. 2 →   The author solution is n^2 for problem d how did it work. Also is there any other approach to do it??? My approach is this-> https://codeforces.com/contest/1185/submission/55853531
 » I solved G2 by using a $O(n^3T^2)$ solution. I think it's incorrect but I can't hack it.Can someone help me hack it?
 » can somebody tell me why my code is incorrect? https://codeforces.com/contest/1185/submission/55763542 it is easy to read
 » 4 months ago, # | ← Rev. 2 →   For problem D cant we maintain a difference array after sorting the initial array of pairs . now we traverse the difference array and maintain a count of number of different elements in the difference array . if there is only one element say 1,1,1,1... then we can output first or last index since it is in ap already , if the difference array is something like 2 2 1 1 then we output the first occurrence of the changed difference that is index 3 in the above array if the difference array is something like 1 2 3 .... then -1.is this approach correct ?
•  » » Your second case will fail on test 1 2 4 6 8 Difference array will be 1 2 2 2 and your output will be 2 instead of 1.
•  » » » yes that was dumb of me , thanks.
•  » » » Could u also explain how u solved Nick an array(CF round 569 Problem B)
•  » » » » yes that was pretty simple one if you convert all elements to non negative integer that is either zero or positives. Now you just need to sort the array and then make elements negative pairwise so that you can get maximum product at the end. If your array length is odd then you don't need to add any condition you just need to leave the last element as it will handle itself the maximum product. For example if your array is -2 -4 -1 0 0 1 2 3 then after converting them to non negatives it becomes 1 3 0 0 0 1 2 3 now after sorting and making the elements pairwise negative you will get 0 0 0 1 1 2 3 3 --> -1 -1 -1 -2 -2 -3 -4 -4 now you can restore your array order by sorting it with index for that you can store it in pair.
•  » » » » » Thank u so much for sharing this approach. My approach was count number of positive and negative elements in array and if both positive and negative elements are even or odd make all elements positive elements negative. Else make all positive elements negative except the last one but it is giving me a wrong answer. Can u explain y my logic is wrong?
•  » » » » » » Consider the following example array[]={-2 -1 2 1} according to your approach neg_count==2 and pos_count==2 here both are even hence you will perform the operation on all elements and your new array will be [3 0 -3 -2] which leads to zero product which is incorrect.
•  » » » » » » » Thank you so much kingarp1914
•  » » Check my submission history :|
 » in solution of problem D, i don't really understand why we need to find the maximum difference between two elements in sequence maxd = max(maxd, a[i + 1].first — a[i].first) and how we to to know if we do that, it will correct ?
•  » » Please, try one else. One of authors added another solution, which wasn't coincidental with editorial. I hope, it will help you.
 » Could someone elaborate more on the $3^9$ bruteforce to get the number of satisfied people for each mask? Or as in this submission: 55810335, Why is making the outer loop as the loop over the bits, and the inner loop as the loop over the masks stops double counting. I don't seem to get what it is trying to achieve with order. Here's the for loop, $B$ is a const int which is equal to $9$ and $cntHappy$ has the number of satisfied people for each mask and at first contains the count of the input masks. for (int j = 0; j < B; ++j) { for (int msk = 0; msk < (1<
•  » » Can Anyone Please Explain how to solve C2 problem in given time constraints. I am not able to understand the solution.
•  » » » Use greedy approach,let's say u have available A marks out of M (u need to pass a student that have index i so A=M-marks[i] ).u want to fill Max amount of students in A so u check count of student having marks 1 to 100 (which, u are maintaining count). Now greedily fill A, so u will get maximum number of passing students.
 » Can anyone please explain this particular line of code of problem c2 posted by author and please also explain the reason behind this line.k += (d + j — 1) / j;
•  » » I will try to explain it. So, according to the editorial, this line will be run if we have situation, when $s_i + t_i - count_k \cdot k \le M$. And in this situation just $a_i + count_k$ may be not mimimal answer (but we want to output the minimal answer).So we need to free some $s_i$ minutes. We know that $k$ students' durations equal $count_k$. And for the minimal answer we need to add only $\lceil \frac{s_i}{k} \rceil$ to $a_i$. Let me rewrite this line in editorial's naming:$a_i += (s_i + k - 1) / k$Note that in programming (A + B — 1) / B' is equivalent to $\lceil \frac{A}{B} \rceil$
 » 4 months ago, # | ← Rev. 7 →   In problem E, Does taking input as :char a; for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ cin>>a[i][j]; } }takes more time than taking input like this:string a; for(int i=1;i<=n;++i){ cin>>a[i]; }plus later processing like: for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ //doing processing on each character of the matrix } }as given in the tutorial.Why is my solution giving TLE?The approach is that I go through the a->z and fill a temporary integer matrix of the same size that of the above matrix with that (character-'a'). ie — 0 for a, 1 for b and so on only on those positions where that particular character is present. Then while filling the matrix for a given character, I check to see if the position on which I'm writing the corresponding number already has a number lesser than the number to be filled.If so, the answer is NO because that will mean that the earlier occurring letter snake is overwriting the current letter snake. If not then I keep filling the temporary integer matrix.At the end, the answer is YES and I print the starting and ending position of a snake in the matrix.My answer is giving TLE. So, that's why I'm asking if there is any difference in taking the input.
 » 4 months ago, # | ← Rev. 2 →   Is 12 4a..a....  a valid input for Problem E?
•  » » Yes. It is.
 » Problem EGetting error on test case 5 : wrong answer actual picture doesn't equal to expected [test case 35]Can anyone help me with that?
•  » » 1 2 caYou are getting wrong answer here.
 » 4 months ago, # | ← Rev. 2 →   In problem F, can 2 people with 3 as favorite ingredient ever get pleased by a pizza with 3 4 5as the ingredient.Here, 3 is occurring only once in a given pizza while there are 2 people who need it.
 » 4 months ago, # | ← Rev. 2 →   In problem F, test case 31 59 9 8 7 6 5 4 3 2 13 4 1 2 3 41 4 5 6 7 84 4 1 3 5 71 4 2 4 6 85 4 1 9 2 8the output is 2 4With 2nd and 4th pizza being chosen, none of them suffice to the liking of the customer. Can anyone explain how we get the solution for above case? I am finding this problem a bit difficult to understand although it seems easy to read.
•  » » With all the combinations of pizzas, the maximum number of friends which can be satisfied is 0. So we simply choose 2 pizzas with minimum cost.
 » Can anyone please help me in PROBLEM-DHere is my codeI am failing on Test Case 42Thanks in advance
 » 4 months ago, # | ← Rev. 2 →   56396995why is this solution not working?
 » Problem E: Getting WA in test case 5Checker log: wrong answer expected r1 == r2 || c1 == c2, but r1=1, c1=1, r2=2, c2=2 [test case 136]This is my submission : 56490336. Can anyone help?
 » Can anyone explain the two BIT approach in C2?
 » I can't understand G2 editorial ... any help?