### bthero's blog

By bthero, history, 7 months ago, translation, Several unexpected Kuhn solutions passed for D1F. Could you please discuss your solutions in the comments and prove its correctness or provide any counter-examples. Author's solution uses flows with Dinic.

Editorial is not completed yet. Problem D1F will be added later. Hope you enjoyed the problemset!

Editorial was/will be written by bthero and BledDest.

Our tester namanbansal013 has made amazing video-tutorials on YouTube for problems D2D/D1B and D2E/D1C. Make sure to check them out and show him some love!

Finally added the editorial for D1E. Currently it is very complicated and error-prone.

Div2A by bthero

Editorial
Code in C++ (BThero)

Div2B by gege

Editorial
Code in C++ (hugopm)

Div2C/Div1A by gege

Editorial
Code in C++ (BThero)

Div2D/Div1B by gege

Editorial
Code in C++ (BThero)

Div2E/Div1C by DimmyT

Editorial
Code in C++ (RedDreamer)

Div2F/Div1D by DimmyT

Editorial
Code in C++ (BThero)

Div1E by bthero

Editorial
Code in C++ (BThero)
Alternative solution code in C++ (hugopm)

Div1F by bthero

Editorial
Code in C++ (BThero) Tutorial of Codeforces Round #673 (Div. 1) Tutorial of Codeforces Round #673 (Div. 2) Comments (234)
 » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).
•  » » A really great tutorial. Also one of the fastest tutorials.
•  » » » Video Editorials: Problem D: Make Them Equal Problem E: XOR Inverse
•  » » » » Thanks man. Please keep doing this job. Your videos are so helpful.
•  » » » » @namanbansal013 Thanks bro.Keep it up.
•  » » IN div 2 B solution:: There is a mistake ::-- f(X)=f(Y)=0 I think this should be f(X)=f(Z)=0.
•  » » » Fixed, thanks.
•  » » » » welcome
 » thanks for instant editorial
•  » » it was posted 38 hours ago !
•  » » » These dudes are time travelers bro, just in case you were not aware of this already
 » D was really a good one... Thank you for such an amazing round :)
•  » » Yep agreed 100%, I was thinking in terms of a linear combination of factors and stuff, later realized how amazingly they exploited the fact that we have 3 * n operations!
 » Thanks for the super fast editorial!
 » Can anyone explain C i still don't get it.
•  » » 7 months ago, # ^ | ← Rev. 2 →   Okay, here are some observations that might help you. Consider element x, if they are at indexes 'i' and 'j'(consecutive occurrence of element x), then for a block of length [j — i — 1] or lower we cannot have that element as answer. Also, if we have element x as answer for length 'L', then that element can be considered as answer for block of length >= 'L'. I think these two observations were enough for me to reach at solution.You can try too or I can explain if you are not able to reach to algorithm.
•  » » » Please elaborate more. It will be very helpful.
•  » » » » Okay so here is what we can conclude from above observations. As we are to check for consecutive occurrence of element x, It makes sense to make an array of positions for each element x from 1 to N. So, now our question breaks down to finding maximum gap(or block of length that is invalid) between two positions for element x. Do this for each element from 1 to N, and update their answer in array 'maxgap' which stores the minimum element x, that is valid answer for blocks of length > maxgap. Now just traverse from 1 to N, print the minimum element you have stored till now for 1 to i'th in 'maxgap', to get answer and update the minimum element till now. Here is my code — Submission.I hope it helped.
•  » » » » » yes it is a very nice explanation.
•  » » » » » Can you explain how we are evaluating maxgap by taking three max's in the inner forloop?
•  » » » » » it helped a lot...
•  » » » » » Thanks, pal. 'Find maxgap' is a great hint for this problem.
•  » » » It did help. Thanks
•  » » if x has to be the minimum number in all segments of length k then in input array between every occurrence of x and its previous occurrence there should not be another segment of length >=k. For instance in {1,2,2,1,1} for k=2 and x=1 we have a segment {2,2} of length two and hence not all segment s of length 2 have '1' in common. Note: We have to consider index -1 and n+1 also as value equal to chosen x.(segment from beginning and end);We can calculate this by maintaining a previous index for all the values we see traversing through array and updating the max distance between two same values. Then we can sort and find the min for every k.
•  » » » Thanks i got it. Such a great q it was ^^
 » Thanks for the quick editorial!For div2b explanation, is the part "It is clear that f(X) = f(Y) = 0" a typo (since f(Z)=0, not f(Y))?
 » I couldn't understand Div2 C. Could somebody help me out with an example?
 » Loved Div2C. Purely algorithmic and appropriate difficulty.
 » 7 months ago, # | ← Rev. 2 →   I knew you would post the editorial fast. That's why I helped you with your n piles of candy :D.
 » 7 months ago, # | ← Rev. 3 →   .
•  » » Okay, so let's make an observation if, for each distinct element, I know it's indices (could be stored by using a map to a set/vector), so let's say my current element is 'a' and it's distributed in the following manner in my arrayx x x a x x x x a x x a x a x x x(where x is an arbitrary element (x!=a) and a is our desired element) so now let's compute the maximum difference between consecutive indices and the distance of first 'a' from the start and the distance of last 'a' from the end.The consecutive differences are [4 (dist. from start), 5, 3, 2, 4 (dist. from end)]now the max diff from the above array is 5, so now for every sliding window of length 5 & more, I can ensure that at least one 'a' is present in each of the sliding window. (Can easily be visualized)Thus, now as in an ordered_map, it's sorted according to its keys, so for each element, when I calculate the max diff for the indices of this element (let's say it's y), I can mark the answer for all length from y to n, as my current element. In the above loop (used for marking my answer), I would iterate only till a point where my answer isn't marked (as whenever I encounter a marked index, all the indices from that index to n would already be marked & as it's a map, it would definitely be marked with a smaller element!), and thus after that, I break the loop.For all the indices that aren't marked, the answer would be -1.
•  » » » 7 months ago, # ^ | ← Rev. 2 →   .
 » 7 months ago, # | ← Rev. 3 →   Isn't the solution for 1B missing something ? The statement says that after each operation, all elements of the array should be non-negative. Edit: I now see that $a_1 \ge i-1$ at the $i$-th iteration.
•  » » I think that this solution takes that into account. All array elements start greater or equal to 1. If we start accumulating the sum at $a$, then we are guaranteed to at least add 2 to $a$, meaning $a \geq 2$. Then even if $a\mod 3 \equiv 1$, it is guaranteed that we are able to add 2 to bring it to a multiple of 3, and turn $a$ into 0. In this way the entire sum can be moved into $a$.
•  » » » very awesome explanation, i was wondering how the sum is being moved for sure.
 » Dimmmy must've used his time travel capabilities again to bring such a fast editorial!
•  » » Hehe... You're pretty smart
 » 7 months ago, # | ← Rev. 3 →   Div2D/Div1B"Otherwise, we have to make it divisible by transferring i−(ai mod i) from a1 to ai. Note that this operation does not break a condition on non-negativity because all ai are initially positive."Why does this not break the condition? How are we guaranteed that a1 >= i-(ai mod i)?EDIT: just figured it out after posting this comment. Since we iterate in increasing order of i, each element we've seen will contribute at least 1, so by the time we are at index i, a1 will have at least (i-1).
•  » » 7 months ago, # ^ | ← Rev. 2 →   Imagine you are at position pos and have successfully accumulated all 2..pos-1 piles into the pile at position 1. Since v[i] for all i is at least 1 you'll have at least pos-1 value in the first position. Notice that v[pos]%pos can be at max at pos-1. (although pos-(v[i]%pos) can be equal to pos but that case is handled separately). Hence if we iterate from left to right it'll always be possible to do this.
 » So good. How fast!!
 » 7 months ago, # | ← Rev. 2 →   i think that In Div2E/Div1C editorial let's construct a trie on our array integers, trie is typo and tree is correct meaning.
•  » »
•  » » » Trie is amazing!
•  » » Save this for the "what data structure should I study to reach blue in CF" questions :)
•  » » » Good
•  » » » Please can you tell me good source to study trie data structure ?
•  » » Well, a trie is also a tree lol.
 » That was too fast. Thanks!
 » Solved C quickly but failed on B. I think it a rare condition in this round.
 » The elegancy of the solutions made me happy.
 » Div2C is a very elegant solution
 » In Div2D/Div1B, is it possible to use a ternary search to find the value we want all the elements in the list to assume?
•  » » That value is already known, say it's x. After every move, array sum will remain constant so, in the end sum = nx, x = sum/n. That's why sum%n == 0 is a necessary condition.
•  » » » Indeed. Now I see it, thanks!
 » "Otherwise, there always exists a solution which uses no more than 3n queries." What is the solution of [0,1,1,1,2], please? sum=5,divisible by n=5.
•  » » Read statements carefully: "You are given an array a consisting of n positive integers"
•  » » » Got it, thanks!
 » 7 months ago, # | ← Rev. 2 →   Div2DWhy can't we just take $\frac{a_i}{i} * i$ from each $2 \leq i \leq n$, making values equal to $a_i := a_i\mod i$ and transfer it to $a_1$, using exactly $n - 1$ operations, then transfer needed values back from $a_1$ to all elements, using another $n-1$ operations?I've desperately tried to find some counter-exapmle during last 1.5 hours, but I couldn't.UPD: Got it, thank you all
•  » » It's wrong if there is i and a[i] % i > sum / n
•  » » » Can you try to find such example?
•  » » » » 7 months ago, # ^ | ← Rev. 2 →   n = 4 a = [3 1 1 3] x in every step needs to bo more than 0
•  » » » » » 7 months ago, # ^ | ← Rev. 2 →   Got it, thank you =)
•  » » » » Have a huge number of ones so that the average is low and one approriate value high1 1 1 1 9 1 1 1
•  » » $a_i$ can become more than the target value in this case but still less $i$
•  » » 7 months ago, # ^ | ← Rev. 2 →   1 1 2 2 3 3 First phase wont change any values, so you dont have enough in a for second phase
 » Can anyone explain div2C problem in simpler way with some example
•  » » 7 months ago, # ^ | ← Rev. 3 →   Consider the distance between 2 positions (j > i), as d = (j — i — 1).For each number x that may be present in the array, calculate the following distances:1- Between the first element of the array and the position of the first occurrence of x.2- Between pairs of positions (j > i), such that a [i] = x and j is the position of the element equal to x closest to i on right.3- Between the last element of the array and the position of the last occurrence of x.Now take the largest of these values for each x value, consider this value equal to k.The number x appears in all subsegments of size >= k.then we have the answer :)
•  » » » Thank you Ayalla I got it.
 » Nice div1C
 » 7 months ago, # | ← Rev. 3 →   Also, thanks for strong pretests <3 UPD: I really don't understand this contests with 3 pretests.UPD2: I actually understand why we don't have full tests. But when we have so weak pretests, contests are just BlindForces. Why don't remove all pretests at all in this case? At least participants will get same experience, IMO
•  » » RIP.
•  » » WA on test 4 for Div2 B sucks
•  » » They do that when the problem allows many tests per test file to make the queue faster.
•  » » » It is very cool, but they can at least make them stronger. Why in problems with several testcases they make pretests with only 1 testcase per pretest? BTW, my two problems failed because of typos, so stupid typos, that I am not sure how this passed event 3 pretests. That is the point in doing many-testcase input, if pretests are actually 1 test-case? I thought they do test-case input to speed up testing process.
 » In Div2B, f(Y) is not equal to zero, f(Z) should be.
 » 7 months ago, # | ← Rev. 2 →   Tutorial for Div2C is hard to understand. I solved Div2C, maybe the way the tutorial explains it, I am not sure.So I think if somebody did not solve it, it is completly not understandable. What is meant by "we can update the suffix [some formular]..."?
•  » » 7 months ago, # ^ | ← Rev. 3 →   For every array element x, they compute the minimum window size k which is guaranteed to contain x. If the minimum k contains x, then all window sizes greater than k also contain x. Then, they sort the array elements in increasing order. For every array element, they have computed a minimum k which it covers. So we try to update the suffix [k..n] with this array element. If we find some k' in the suffix [k..n] which is already updated, then we don't need to update any further because every element in the suffix [k', n] has already been updated by a smaller array element because we're iterating through the elements in increasing order.
•  » » » How to update "the suffix", whatever it is?
•  » » » » 7 months ago, # ^ | ← Rev. 3 →   We make an array which maps the window size k to the smallest number which occurs in all windows of size k. For example if the array size is 3, then we will have 3 window sizes. So, let the array(1-indexed) "K" be [-1, -1, -1]. Then, we sort the array given in the problem in non-decreasing order. Let's say the array given in the problem is [1, 2, 3](already sorted). If it wasn't, then sort it.Let's also assume that we have computed the smallest window size k for each of these elements. For 1, this window size is 3. For 2 this window size is 2. For 3 this window size is 3.Now, we will update the suffixes of the "K" array while iterating through the array we sorted.When iterating through the array we sorted, we will run into 1, 2, 3 in that order. For 1, the smallest window which works is 3. So, we will update each k >= 3 in the K array with 1. Now the K array is [-1, -1, 1].Then we'll run into 2 while iterating. The best minimum window size which works is 2. So, we'll update the "suffix" with indices [2..3] in the K array with the value 2. But, when we try to update position 3, we see that it has already been updated. So, we can stop and we don't need to go any further. Now the "K" array is [-1, 2, 1].Then we run into 3 while iterating. The smallest window which works is 3. So, we can update the suffix [3..3], but it has already been updated by a smaller value 1. So, we can stop.
•  » » » » » Thanks!
 » I'm Speed!
 » 7 months ago, # | ← Rev. 2 →   Can someone tell me how I passed pretest and managed to get runtime error on test "1" during system testing?here is my submission: 93984302
•  » » Usually this is undefined behaviour, some index out of bounds. But I cannot see in which line.
•  » » » But I at least passed pretest(which had 4 tests). Very confused, hope I don't get ruined by some mysterious problem.
•  » » » » I compiled and run it on my computer, it is a out of bounds. Error messagegcc -Wall -Wextra -pedantic -std=c++17 -O2 -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual -Wcast-align -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2 c.cc -lstdc++ -o c.exe c.cc: In Funktion »void solve()«: c.cc:47:20: Warnung: Vergleich von Gleitkomma mit == oder != ist unsicher [-Wfloat-equal] 47 | if(ans[i]==1e9) | ^~~ /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/include/c++/debug/vector:427: In function: std::__debug::vector<_Tp, _Allocator>::reference std::__debug::vector<_Tp, _Allocator>::operator[](std::__debug::vector<_Tp, _Allocator>::size_type) [with _Tp = int; _Allocator = std::allocator; std::__debug::vector<_Tp, _Allocator>::reference = int&; std::__debug::vector<_Tp, _Allocator>::size_type = long unsigned int] Error: attempt to subscript container with out-of-bounds index 6, but container only holds 6 elements. Objects involved in the operation: sequence "this" @ 0x0xffffcb10 { type = std::__debug::vector >; } 0 [main] c 2390 cygwin_exception::open_stackdumpfile: Dumping stack trace to c.exe.stackdump You could try to use compiler options which make your code fail in such cases.
•  » » » » » nah I re-submitted and got AC, idk
•  » » » » » » That is the "undefined" in undefined behaviour. Sometimes it works, sometime it does not.
•  » » » » » » » Hmm, very interesting. I guess I will prompt to static arrays more to avoid this kind of problem(maybe will help? idk).Anyway, thank you for trying my code.
•  » » » » » » » » 7 months ago, # ^ | ← Rev. 2 →   Why would using static arrays help? It may sound rude and it is rudeBut people who don't understand what UB is should not code in c++. It should literally be repeated on every c++ lesson
•  » » » » » » » » » I will try to study what UB is. It is correct that I didn't take C++ lesson though.
•  » » » » » » » » » Don't take my comment personally It just happens again and again. People create posts or comments and always ping Mike or contest authors regarding "It passes locally but fails with the same example in tests" or something like that. Though it is completely normal for their language Really painful. Also it is not the only ub you can get. There are plenty of other options to run into it.
•  » » » » » » » » » What is ub?
•  » » » » » » » » » Undefined Behavior
•  » » » » » » 7 months ago, # ^ | ← Rev. 2 →   Yes you did get AC from undefined behaviour, but you may get RE during systests. To avoid that, compile with -D_GLIBCXX_DEBUG. This will catch all out of bounds accesses(in stl containers), and you will therefore not get any UB for that reason at least.
•  » » DimmyT asking for help
•  » » » xD I don't know what to do... Maybe MikeMirzayanov can help? In every problem, pretests are prefix of all tests.
•  » » » » yes but I surely passed pretest so I don't know why...
•  » » Yeah I submitted the exact same code again and got accepted, MikeMirzayanov sorry to bother but please help me TAT
 » This contest made me purple for the first time :) Thanks for the good problems
 » On this contest I had big troubles with problem E. I used the idea with the mergesort + checking every bit of answer from last to first. It uses O(30 * n * logn) time, and with n = 3e5 it is approximately 1e8, is it really that slow for time limit = 2 seconds? If anyone can get AC with my code, please share my mistake. (94018657)
•  » » My solution was with almost the same idea as yours and also got TLE on test case 12, when I optimized my code it runned locally on 3 seconds for the worst test case.However I think you should use long long for computing the amount of inversions.
 » a great contest but very weak pre test for B (div 2)
 » Div2-B my code What's wrong in this I'm unable to figure it out. If anyone could help me. Initially it is assigning all the elements of array as '0' and putting current index(j) in map if it's not making 'T' with another previous element i
•  » » What about the case when a[i] = T/2???
•  » » » got it.
 » 7 months ago, # | ← Rev. 2 →   Testcases were very weak for Div2 C. I saw a lot of submissions where the test case 1 1 3 would fail, but somehow got accepted. I submitted my code at 1h 6 mins(all pretests passed). Went on ahead to try D, but at 1h 52 mins, I realised, that I've missed the case for n = 1, So I resubmitted it. Turns out they didn't have a basic case like n = 1. My ranking fell from expected 1200 to 1765. Please do something about the resubmission rule.
 » When you submit solution in the last min and net fucks you up and then you submit that after contest and it ACs. FML
 » Weak Test cases for problem (div.2)A. A soluion with 10^7 operations(in worst case) in java, it just has 3 test cases which are also randomly made i guess. look at this solution. I know its just problem A but still it decreases the quality of contest.
 » Can someone help explain why I keep getting Time limit exceeded? Submission: https://codeforces.com/contest/1417/submission/94021351 From what I see I solved it the same as the solution, is it just because I use python or can I optimize further?
 » "Several unexpected Kuhn solutions passed for D1F. Could you please discuss your solutions in the comments and prove its correctness or provide any counter-examples. Author's solution uses flows with Dinic."Don't worry, it is almost impossible to hack Kuhn (especially on those, specific graphs). Even if it is possible, it is VERY hard. Of course, I don't have any proof of my words, but I know it from my experience.
•  » » This is a strange question, but: how to solve this problem with Kuhn? I don't doubt its time efficiency, but I don't know how to find the maximum matching which saturates some given set of vertices without using circulations (which is the model solution).
•  » » » Alright, so you need to find the matching that covers some set of vertices. Using Kuhn in proper order, find any maximum matching that covers all vertices of the left part from the set, let's say that the set of covered vertices of the left part is $L$ (some vertices from the set + something else). Using Kuhn in proper order, find any maximum matching that covers all vertices of the right part from the set. Similarly, define $R$. The claim is: there is a maximum matching on the set of vertices $L \cup R$. You can prove it using alternating paths.And then you can just leave only those vertices and find the maximum matching.Note: this algorithm also can be used for solving the maximum vertex-weighted matching where you have weights on both parts. I will leave it to you as an exercise.
•  » » » » That's really cool (though I need some time to make sure I understand why this is correct). Thank you!
•  » » » » which Kuhn's algorithm is this and what does it do? He has too many...
•  » » » » » Oh, excuse me! In Russia, we call the 'Kuhn's algorithm' a DFS-like approach for maximum matching in the bipartite graph, which goes like that: for $v \in L$ in some order, run $dfs(v)$ (i.e. simplified and faster version of Ford-Fulkerson, which appears to be quite powerful).Here is the Russian (unfortunately) page with code and description of this algorithm (you can use a translator if you really want): e-maxx..
•  » » » » » » Thank you!
•  » » » » So u use that claim just to cut down the number of vertices and edges? Or am i missing something?
•  » » » » Can you please explain how to find maximum weighted max matching? I know about fast Kuhn, but I always thought that it can’t be used for finding maximum weighted matching.
•  » » » Another way (I didn't participate today, though, so I'm not 100% sure). We can model our subproblem (given a bipartite graph, find a matching covering a given subset of vertices) as a maximum-cost flow: connect a source with all vertices of one side of the bipartite graph with oriented edges with capacity 1 and cost 0 or 1 (1 if the vertex must be covered by the matching). We analogously connect all vertices of the other part of the graph with the sink. Now, if we need to cover $R$ vertices, we can easily see that we are looking for any flow with cost $\geq R$.But running a maximum-cost flow algorithm naively is probably too slow. In order to cope with that, we need to understand how the simplest maximum-cost flow algorithm works on our network: In the first phase, the algorithm iteratively finds augmenting paths with a total cost of $2$. We can convince ourselves that this is actually equivalent to finding the maximum-size matching on the subgraph induced by all required vertices (and no other vertices). We can do just that using Kuhn's algorithm. In the second phase, the algorithm iteratively finds augmenting paths with a total cost of $1$ in the residual network created after the first step; hence, we are looking for alternating paths connecting an unmatched required vertex with an unmatched non-required vertex. This is equivalent to running the Kuhn's algorithm on the graph above (with the matching found in the first phase), only that we only search for alternating paths originating from a required vertex (remember that the origin can belong to either part of the graph). No further augmenting paths in the max-cost flow algorithm will increase the cost of the flow, so we're done — we found a matching covering as many vertices as possible.
 » Hi, Can anyone please tell me the time complexity of my code for D2B, according to me, it is approximately O(N log N). But it gave TLE on the system tests, which shows that I have approximated the complexity incorrectly. Here is the link to my submission:https://codeforces.com/contest/1417/submission/93976053Thanks in advance!
•  » » If input looks like $a = [x,x,x,...,y,y,y,...]$ and $x + y = T$, every time your program sees a $x$ it will go through every positions of $y$ and set the answer, then the run time becomes $O(n^2logn)$
 » For problem D how can it be told for sure that a wont be negative after the operation. lets say if a[i]=8 and i=7 then it would take 7-(8%7)=6 at least 6 but if a is less that that how what will happen then? what am i missing?
•  » » Even I have the same doubt
•  » » Because he did the operation from i=2 to i=nyou can see that the when we are doing from i=k, a[i] is at least k-1 since a~a[k-1] is all greater than zero, thus the method will work.
 » Aaaaaaaah! That feeling when a solution passes after contest by changing just ONE CHARACTER!!!! Last minute of contest:94019973 First minute after contest: 94021490 Nice contest!
 » Hi, I failed to understand Div2 Problem B,. When is f(c) or f(d) considered 1? And what does it mean by f(c) + f(d) equal 0 or 1?
•  » » 7 months ago, # ^ | ← Rev. 2 →   When the sum of a pair of elements of an array b is equal to T, then the f(b) is incremented by 1.
 » 7 months ago, # | ← Rev. 2 →   please someone help what's wrong in my code in div.2(B)? It failed in system testing(test#4). https://codeforces.com/contest/1417/submission/93983911why I am downvoted why it's not right of the "pupil" to ask doubts
 » 7 months ago, # | ← Rev. 3 →   for div2C , i tried to do a for loop through each length , using a sliding window and then check the intersection of all windows to find the common elements. is there a way to improve the time complexity of this method or is it too slow?
 » My solution for Div1 D using small-to-large merging passed in 1.4s. 94011971The idea is that we simulate small-to-large merging for edges in reverse order and save which values were in a smaller set, so we could simulate the process in reverse order by removing the smaller set from the larger set.
•  » » 7 months ago, # ^ | ← Rev. 2 →   You don't even need to use a set of pairs to answer queries.With the same idea you can maintain the vectors during the small-to-large merging and sort them afterwards, then you can simply rollback the edges which you are supposed to erase. To answer queries you go from the back of the vector and pop elements which are already deleted (equal to 0) or aren't in the same disjoint set anymore. So for all queries you get O(n) amortized (After sorting for NlogN of course).This made my code run in about 650ms.Submission
•  » » » Wouldn't there be $O(NlogN)$ elements in total which we need to sort? Therefore the complexity would still be $O(Nlog^2N)$, but with a better constant.
•  » » » » Yeah, i didn't notice that, you're correct.
•  » » » » You can get the complexity down to $O(N \log N)$ if you sort all vectors using counting sort (consider all vectors that contain 1, then all vectors that contain 2..., and proceed like this to reconstruct all vectors in the correct order). See 94166796
•  » » » » » Cool. I spent day to optimize this solution with vectors to $O(n log(n))$ and didn't come up.
 » can anyone tell me what's wrong with my code for div 2B. Code link-link . I first put all elements <=T/2 in x , remaining in y.Then checked if k is even to allocate all elements=k/2equally in x and y.
•  » » You have sorted the original array and in the answer you have to print 1s and 0s for the elements at the original position of the array.
 » In DIV2B , why can't we just put all pairs whose sum add up to the target into different multi sets?  for(int i = 0 ; i < n ; ++i){ int curr = arr[i]; int val = tar - curr; if(freq.count(curr) <= 0) continue; --freq[curr]; if(freq[curr] <= 0) freq.erase(curr); if(freq.count(val) == 0){ ms1.insert(curr); continue; } if(ms1.count(val) > ms2.count(val)){ ms1.insert(val); ms2.insert(curr); --freq[val]; } else{ ms1.insert(curr); ms2.insert(val); --freq[val]; } if(freq[val] <= 0) freq.erase(val); } 
 » Was O(nlog(n)log(max(A[i]a)) not intended for div2E or does my solution just have poor constant/ bad implementation on that time complexity?
•  » » My O(n log A) with hashmap failed on pretests so I assume that anything slower than O(n log A) wasn't intended but I might be wrong
•  » » 7 months ago, # ^ | ← Rev. 2 →   The time limits were tight here. My solution failed with map too. Unordered_map barely passes (but I needed a custom hash function — they may have some anti-hash test cases).EDIT: Actually your complexity can work (for example see tourist's solution), But constants matter a lot here.
 » Otherwise, we have to make it divisible by transferring i−(aimodi) from a1 to ai. Note that this operation does not break a condition on non-negativity because all ai are initially positive. I don't know why this sentence is true. If a1 is small, isn't it a negative number? Or am I too stupid?
•  » » Since we are considering the indices $i$ in ascending order, and all $a_i > 0$, when we start dealing with $a_i$, $a_1 \ge i - 1 \ge a_i \bmod i$.
•  » » » I understand, thank you very much!
 » 7 months ago, # | ← Rev. 2 →   Can anyone tell what is wrong with my code for DIV2B?. I have first put all elements <= T/2 in X and the remaining in Y. Then checked if k is even to allocate all elements =k/2 equally in X and Y. Edit: My code fails on testcase 4.
 » Rating changes working correctly? 2000 rank made me a specialist again
•  » » Dropped to specialist before, seems like 2000 rank is not enough for a steady blue title.
 » I just solved the 1st one and it was correct. But my rating was reduced by 66. Anyone knows why?
 » 7 months ago, # | ← Rev. 5 →   Can any one give a formal proof for Div 2 A?
•  » » 7 months ago, # ^ | ← Rev. 8 →   The first obvious observation is that the order of the piles does not matter. Second, if there are two sets of piles of equal lengths, {$a_i$} and {$b_i$}, such that for all valid $i$ $b_i \leq a_i$ then the answer for {$b_i$} is not less than for {$a_i$} — just do all the operations on the same set of indices in the same order on {$b_i$} as on {$a_i$}.Now, for any two piles $a_i$ and $a_j$ such that $a_i \leq a_j$ copying $a_i$ to $a_j$ is more optimal (or, at least, as optimal) than copying $a_j$ to $a_i$ — the resulting piles will be of the same sizes except for one — the pile which was copied from.Similarly, if the are three piles $a_i$, $a_j$, $a_k$, and $a_i \leq a_j$, then it is more optimal to copy $a_i$ to $a_k$ than $a_j$ to $a_k$.The observations above immediately imply that at each step it is most optimal to copy from the pile of the minimal size.A rather simple proof for problem A! :P
 » Can someone please explain div2b, how we can separate array a in white and black balls based on given unlucky integers T?
•  » » here two array element say x , y will make bad pair when x+y = T ,,, there are two case : case 1: (x!=y ) in this case you can paint all array elements with value = x ,,, white and all elements with value = T-x ,,, black(if such numbers exist ) case 2: ( x==y ) say there are Cnt such elements , its optimal to paint Cnt/2 elements white and rest elements black .
•  » » » That's exactly what i did but it shows WA, code: https://codeforces.com/contest/1417/submission/93987002
•  » » » » Changed your code a bit its ac now ... https://codeforces.com/contest/1417/submission/94035994
•  » » » » » Thanks, got it now
•  » » » 7 months ago, # ^ | ← Rev. 2 →   Thanks for the explanation reversebladesamurai
 » Some video solutions (for 2A-F), in case you like those
 » I might be wrong here, but isn't your solution for D wrong with an input of: 1 8 0 0 2 0 0 0 0 6 The second operation from your code 1 3 1 would reduce the first element by 1 and increment the third element by 1, but the first element is still 0 at that stage. 16 2 1 0 1 3 1 ... // snipped here, got the output from a custom invocation call 
•  » » A[i] >= 1
•  » » » Of course, thanks for pointing it out.
 » For Div1C / Div2E I have another solution without using a trie.Here is my submission: 94030812Explanation: SpoilerLet f(v) be a function that calculates the number of inversions of the array v. We can calculate that while doing a MergeSort in O(n log n).We keep track of the minimum number of inversions in ans, and the number x that creates the answer when doing xor to the vector.Then we update bit by bit x, from the highest bit to the lowest bit. If adding that bit to x, the answer decrease, we keep that bit set, else we continue. Pseudocode: Spoilerx = 0; // v is the input vector and x the number that xors the vector. for(i = 31; i >= 0; i--) if f(v^(x|(1<
•  » » Cheers, I tried a mergesort solution after the contest but it would always be too slow, your submission + explanation was a nice reference :)
•  » » I did think of the solution on these lines but couldn't come up with a concrete proof of why it would work... good to know it works
•  » » Can someone prove why this solution works?
•  » » Your solution's time complexity is O(30*n*log(n)) right ? I have made similar submissions but they all got TLE, did you use any optimization ?
•  » » » 6 months ago, # ^ | ← Rev. 2 →   you can do the same using counting sort instead of merge sort to achieve O(30*n).the idea is: you sort using only the uppermost k bits and use that to sort for k+1. in order to sort for k+1, you divide your current sequence into parts in which all numbers have the same k upper bits. now you sort each of these parts with counting sort and count the inversions. you end up doing something similar to a radix sort but going from upper to lower bits. my submission:96530820
 » can someone tell me what is wrong with my solution....what i did was take input as pairs(to remember the index after sorting by value) and loop until i find two index that gives the sum "k" and the remember the indices of the two value. Now just make half of the subarray 1 and others zero then sort by indices to finally print the answer. submission : 94032531
 » For div2 B it shows WA, My solution was to divide them like this: paint X white and T-X black if it exists and distribute all x=T/2 equally to black and white. What is wrong with this approach.
•  » » maybe trying not declaring big arrays in main? that sometimes causes WA
•  » » » Changed it but still shows WA
 » Can someone please find out mistake in my code for Div2B? I and my friends have been at it for hours and can't figure out what's wrong in my submission.Submission link: https://codeforces.com/contest/1417/submission/93993690
 » Div1 C TL not friendly :( My solution with hash table didn't pass.
 » Is div2D solution wrong? consider 1 1 1 1 3 3 4 the sum is divisible by 7, but it seems to be unsolvable...... pls help me with this case :)
•  » » 7 months ago, # ^ | ← Rev. 2 →   1 1 1 1 3 3 4 >>> 0 2 1 1 3 3 4 >>> 2 0 1 1 3 3 4 >>> 0 0 3 1 3 3 4 >>> 3 0 0 1 3 3 4 >>> 0 0 0 4 3 3 4 >>> 4 0 0 0 3 3 4 >>> 2 0 0 0 5 3 4 >>> 7 0 0 0 3 4 >>> 4 0 0 0 6 4 >>> 10 0 0 0 0 4 >>> 7 0 0 0 0 7 >>> 14 0 0 0 0 0 >>> 12 2 0 0 0 0 0 >>> 10 2 2 0 0 0 0 >>> 8 2 2 2 0 0 0 >>> 6 2 2 2 2 0 0 >>> 4 2 2 2 2 2 >>> 2 2 2 2 2 2 2
•  » » » Oops i gave the wrong data......0 1 1 1 3 3 5should this be unsolvable or not ?thx
•  » » » » 0 1 1 1 3 3 5 is not a valid testcaseA[i] should be larger than 0
•  » » » » » Oh thx for the fastest reply i ever seen......I should make sure that i am able to read problem statements carefully for every contest :( it's so hard since i'm in gmt+8 and codefroces contests are often held late at nightanyway thanks @..vince
 » In Div2B if we consider this test case 5 9 1 3 3 3 8According to solution provided it would be 0 0 0 0 1But optimal solution would be 0 0 0 1 1Am I correct gege ?
•  » » I think so. And I found a WA in the test case from the Div2B.The positive solution would be 1 0 0 1 1 0 1 0 0But the solution of RedDreamer would be 0 0 0 1 1 1 0 1 0
•  » » What are you asking for?Both coloring you gave have f(c)+f(d)=0
•  » » » Yes He's(theHermit) wrong ,but in my test case I consider the optimal solution to be right whereas gege's version gives another answer.
•  » » » » Nope, in your case both are optimal.
•  » » » » » Got it thanks!
•  » » » » Yes you are right. I get it now.Thanks for your at~
 » why my submission fails
 » Can someone please explain why [i==n-1] is at the end of same line without semicolon in problem Div2B? Thank you!  cout << r << " \n"[i==n-1];
•  » » " \n"[i==n-1]is array of char. When Condition True "\n" (1st index) printed, when false " " (0th index)printed.
•  » » » Wow! Thank you, I had no idea.. Also can you please share any resource for reference, I am unable to find this kind of if else condition on Google. Maybe I am not using the right keywords. Thanks again
 » Can someone explain to me the C problem,i still dont get it
 » In Div1 B , the condition:"$a_i\geq 1$" is really important.At first,I ignore it,so I am stuck for a very long time.Compared to B,Div1 C is much classical.
•  » » I made an unnecessary resubmission for missing that. I found out about that constraint when I wanted to hack someone.
 » With this contest I regain my lost confidence. Thank you Codeforces for helping me growing better.
 » Problem B. Can anyone tell me why my solution is giving WA. My Solution Code
 » According to problem Div2D: Number 1 is the most powerful number :))
•  » » The trick of an array of adhoc problems
 » Construct a Trie to solve Problem E. I can never think such a great idea! Here's my solution: 94045226 without using trie. Code may be a little long, but it got accepted!
 » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).
 » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).
 » 7 months ago, # | ← Rev. 2 →   Great contest!
 » 7 months ago, # | ← Rev. 2 →   Div 2B: Even after reading editorials I don't see any error. Can someone plz tell me what's wrong with my code? Solution B. My solution was to divide them like this: paint X white and T-X black if it exists and distribute all x=T/2 equally to black and white.
 » Is the rating change unusual this time for everyone or only me?
 » A can be done like this for _ in range(int(input())): n,k=map(int,input().split()) l=list(map(int,input().split())) l.sort() r=0 for i in range(1,n): r=r+int((k-l[i])/l) print(r) 
 » 7 months ago, # | ← Rev. 2 →   We (me, gyh20, Time_tears) have a totally different (and much easier, we think) solution to D1D.First add the edges from the last operation to the first, and at the same time use disjoint-set-unions (with merging the small one into the big one) to add the numbers a connected component contain into a vector and keep the total size under $n\log n$.After getting done with the vectors, sort each of them from small to big. Let $bel[i]$ be the connected component the point with value $i$ is now in. For all $i$ from $1$ to $n$, let $bel[a_i]\leftarrow getfather(i)$. (getfather is the dsu function)Then we answer the queries: If this operation is a deleting operation, let's assume that we delete (x,y). If deleting the edge doesn't affect connecting components, ignore it. Else let $getfather(x)\to x, getfather(y)\to y$. Assume that $size[x]  » 7 months ago, # | ← Rev. 2 → can anyone explain what does this line in editorial for DIV2 F mean, If the current query is of first type, remember the "boss" of the corresponding vertex. How is the question transformed to subtree-maximum query on DSU tree ?  » why my O(n*30) solution in E give TLE94058521 •  » » https://codeforces.com/contest/1417/submission/94186120 Use reserve as the hash table require a lot of rehashing and resizing.  » 7 months ago, # | ← Rev. 2 → Can Someone please tell me what's wrong in my solution for div2-Bhttps://codeforces.com/contest/1417/submission/94138194I have simply changes the color (k — number) to opposite value of the (number). This also ensures that all the n/2 numbers are equally distributed but still getting wa on test 4 •  » » 7 months ago, # ^ | ← Rev. 2 → Got my mistake. For any one doing same mistake as me consider the test case4 88 0 4 4 •  » » » 7 months ago, # ^ | ← Rev. 2 → Thanks, bro! It was really messing up with my head, didn't realize that I am just plain dumb :)  » can someone help with the last graph question. I couldn't find the explanation for it. Thank you  » The editorial of Div1E is missing. Could anyone share the idea of its solution?  » 7 months ago, # | ← Rev. 3 → Since the editorials of problem E and F are missing, I will share my approach to solve them:E: Observation 1We are actually minimizing the number of neighbor pairs in$b$that are different Observation 2Add numbers one by one. For each number$a_i$, we will decide if we want$b_{2i - 2}$and$b_{2i - 1}$to be different, and also$b_{2i - 1}$and$b_{2i}$. (We can special handle the first element, of course).If we want$b_{2i - 1}$and$b_{2i}$to be same,$a_i$must be even and$b_{2i}=\frac{a_{i}}{2}$.If we want$b_{2i - 2}$and$b_{2i - 1}$to be same,$1\leqb_{2i - 2}<a_{i}$. and$b_{2i}=a_i-b_{2i-2}$. Observation 3After first element is added, possible values of$b_2$are either a range ($1\leqb_2<a_1$) (If$b_1\neqb_2$) or a single possible value$\frac{a_1}{2}$.It follows that after adding each number, the possible values of$b_{2i}$is a range of values added by a set of individual values$S$.If$b_{2i - 1}=b_{2i}$, possible values become a single value.Otherwise if$b_{2i - 2}=b_{2i - 1}$, you will essentially flip the range and every value inside$S$.Otherwise, possible values are$1\leqb_{2i}<a_{i}$. Observation 4You can now do a DP or greedy here:For greedy, you can observe that when you add each element, you will want to always make$b_{2i - 2}=b_{2i - 1}$or$b_{2i - 1}=b_{2i}$, or both.If you can do both, it is always optimal to do both.If you can do either, it is always optimal to do either. The result possible values will be a range of values added by the set of individual values$S$, and we will add at most one more individual value to$S$.It is annoying but possible to maintain the range and the set$S$with$O(n lg n)$total time complexity.A lot of details are omitted but that should be enough observations for a complete solution.F: Observation 1If we construct the resultant graph based on directions, it's a cycle with some branches moving towards the cycle.The sum value of all elements within cycle must be the same.The sum value of other elements must be strictly larger than its next element. Observation 2All cycles can be decomposed to cycles of length 2. Observation 3Label squares with no neighbor strictly smaller than itself.If we can match each of labelled squares with a neighbor element (not necessary labelled) with same value, such that no element is matched twice, we can form a solution.The reverse is also true: If no such matching exists, we don't have a solution. Observation 4Since the board is bipartite, form a bipartite graph with odd parity elements on left, and even parity elements on right. (Parity based on value of$i+j$for element$a_{i,j}$).Create a lower bound max flow graph by connecting source to each element on left, and each element on right to sink with capacity 1 each.If an element is labeled, make the lowerbound of the particular edge from source to element or from element to sink to be 1.If$x$is on left side, element$x$and$y$are neighbor in original graph, and their values are equal, make a edge from$x$to$y$.By running lowerbound maxflow, we will get a possible matching that fulfills the requirement, or notice that no such matching exists.Lastly, maxflow on unit capacity graph have some complexity like$O(E \sqrt{V})$. •  » » Thank you. Was waiting for this.  » In the editorial of Div2E/Div1C，there's something I can't understand. In the tutorial,$a, b$are the children of$v$, and$v$has a depth of$k$. Then$a, b$have a depth of$k-1$, and the highest bit which differs in both should be the$k-1$-th highest bit. So I think only the$k-1$-th highest bit of X is toggled, lists$S(a)$and$S(b)$will change their relative order, not$k$-th. Can anyone explain it?  » 7 months ago, # | ← Rev. 2 → Edit: has been fixed.The maxn (4e6 + 100) in the code of Div2E/Div1C is small and can be hacked by this generator: #include using namespace std; const int mx = 1e9; const int n = 3e5; int main() { cout << n << endl; int cnt = 0; for (int i = 0; (i << 12) <= mx; i++) { if (i > 0) { cout << " "; } cout << (i << 12); cnt++; } for (int i = 0; cnt < n; i++) { cout << " " << ((i << 1 | 1) << 11); cnt++; } cout << endl; return 0; }   » Nice contest. I'm new to the technique of adding fake node to build dsu tree where used in Div2F/Div1D problem. Is there any relevant problem can be solved by this technique?  » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).  » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).  » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).  » Can anyone point out where I am going wrong for Div2 B? https://codeforces.com/contest/1417/submission/94288900 I am getting WA on test case 2. Any help will be appreciated.  » 7 months ago, # | ← Rev. 4 → Div2 A. The statement "any operation which increases the value of$min(a_1,a_2,…,a_n)$is unoptimal" (the proper English word is non-optimal) is false. Besides, it is meaningless since not every operation that preserves the minimal value is optimal. 1 2 3 1 2 You can copy once regardless of whether you copy from$a_1$to$a_2$or from$a_2$to$a_1$. •  » » 7 months ago, # ^ | ← Rev. 5 → Why has it been downvoted? Is anything stated there incorrect? It referred to the original text of the editorial which has since been edited.  » 7 months ago, # | ← Rev. 4 → Thanks for Editorial  » 7 months ago, # | ← Rev. 2 → I am not able to find the error in my code why it is giving in WA in test case 2 in problem B https://codeforces.com/contest/1417/submission/94340854  » BThero — In Div2A instead of having a while loop to add min element with the array element, you can just use the AP formula by having k as the last term of the AP series and you can just traverse through the array, so time complexity will be just O(n)  » seeing space complexity for the first time in editorial!! one of the best round.  » Auto comment: topic has been updated by bthero (previous revision, new revision, compare).  » I am thrilled after understanding div2 F's solution  » In the problem Div2 D, the editorial said that transfering i-(a[i]%i) from a to a[i] does break the non-negative rule. How about the case that a < i(a[i]%i)? Will the a become negative?  » In Div2E/Div1C we can convert all numbers to binary. Then we start with the whole array of n elements and look at the most significant bit. Iterate over the array, counting inversions and antiinversions along the way. (Inversions are 1-0 pairs and antiinversions are 0-1 pairs, 0-0 and 1-1 pairs are neither.) Split the array into two subsequences, placing the elements with a 0 bit in this position to one of them and elements with 1 bit to the other, while preserving order within each subsequence. Repeat the same procedure recursively on each of the subsequences, with the next most significant bit. Inversions (as well as antiinversions) at the same recursion depth should be added up. Recursion stops, after processing the least significant bit. At the end we have the total number of inversions and total number of antiinversions per recursion level i.e. per bit position. Now we can construct the required x, setting its bits to 1 at positions where the number of inversions is strictly larger than the number of antiinversions and to 0 in the others. Complexity is O(n logn).  » 7 months ago, # | ← Rev. 2 → DimmyT Thanks for the nice solution for DIV2F. But I am not clear on last update after doing type 1 query: updPos(1, 1, n, tmp.se, mp(0, 0)).Can you explain why is it required?  » 6 months ago, # | ← Rev. 9 → Unofficial Div1F editorial (Dinic's maxflow solution):Note that a satisfactory solution can be produced as follows: for every cell, exactly one of the following must be true: It points to a neighbor of lower value than it that doesn't point back, and its cost will be the difference between the values. It points to a neighbor of equal value that points back to it. The sum of their costs will be equal to their shared value (division can be arbitrary, e.g.$(1, x-1)$or$(\lfloor x/2 \rfloor, \lceil x/2 \rceil)\$) Note that the second condition resembles the bipartite matching problem that arises from domino-tiling an arbitrarily-shaped chessboard (e.g. this problem from AtCoder). In fact, it can be framed as a bipartite matching, but with only a subset of nodes being mandatory participants for the matching. A node is mandatory iff its cell has no neighbors of lower value, otherwise it's optional.Recall that to use Dinic's maximum flow to solve a bipartite matching, there are source-edges to one of the parts (the "white" cells on a chessboard), sink-edges from the other of the parts (the "black" cells), and directed edges from the first part to the second part based on potential matchings (i.e. between cells with equal value). To model mandatory nodes, we add a "demand" to its source or sink edge, modeling demands by the transformation detailed in this tutorial.The answer is yes iff all demands are met by the flow. The solution can be reconstructed by matching cells by the second condition if there is flow between them. If a cell isn't matched this way, the first condition can be fulfilled instead; this is always possible as the node would have been marked mandatory otherwise.Beware: Dinic's algorithm implementations that pass many other flow problems fine may TLE here due to a subtle bug. In particular, ensure your adjacency-list pointers (ptr in the author's solution, or next in my solutions [recursive | iterative]) only increment if its edge is exhausted, i.e. the search in that node hasn't fulfilled its flow limit yet.
 » In problem 1D, the given solution code runs about 1.3s (GNU C++17(64)) and the TL is 1.5s?
 » All spoilers are broken. Please fix this.