Editorial of Pinely Round 3 (Div. 1 + Div. 2)
source link: https://codeforces.com/blog/entry/123584
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
The official implementations of all the problems are here.
Author: TheScrasse
Preparation: TheScrasse
1909B - Make Almost Equal With Mod
Author: TheScrasse
Preparation: TheScrasse
Author: TheScrasse
Preparation: TheScrasse
Author: TheScrasse
Preparation: TheScrasse
Author: TheScrasse
Preparation: TheScrasse
1909F1 - Small Permutation Problem (Easy Version), 1909F2 - Small Permutation Problem (Hard Version)
Author: TheScrasse
Preparation: TheScrasse
Author: TheScrasse
Preparation: TheScrasse
Author: TheScrasse
Full solution: Endagorion, errorgorn
Preparation: TheScrasse, franv
1909I - Short Permutation Problem
Author: TheScrasse
Full solution: errorgorn
Preparation: TheScrasse
3 days ago, # | Thanks for the fast editorial! |
-
Wrong.
-
gcd * 2
?-
Wrong (I tried it)
-
Sort the array and take the GCD of the difference of consecutive elements, then multiply it by 2.
AC Submission : 238522780
-
-
-
3 days ago, # | Thanks for the quick editorial. I will probably become expert in this round. Thanks. |
3 days ago, # | Can someone explain why this solution receives TLE? I assumed that the time complexity would simply be O(nlogn): 238557809 |
-
That's because you've used
upper_bound(r.begin(), r.end(), l[i]);
instead of
r.upper_bound(r[i]);
The first one works in and the second one in where is the size of the set. This is due to the fact that the first function is a generic function that works with any container and is guaranteed to work in only if the container supports random access (std::set doesn't support random access), while the second one is a specific function designed only for sets and is guaranteed to have good complexity. You can refer to the documentation in case you're confused:
-
I have basically the same code and I did use
r.upper_bound
, but I still get TLE: 238528720. Did I perhaps get constant factored?-
I think I did get constant factored :( Instead of storing the segment in a
vector<pair<int, int>>
, I removed this intermediate step and stored the length of the interval in avector<int>
directly and it passed 238595993. I don't understand why this would impact the time factor; I thought this would only cost a bit more memory.
-
3 days ago, # | I Feel so stupid for not getting B in an hour |
3 days ago, # | MathForce. All problems have math tags. |
3 days ago, # | Great round. Thanks for fast editorial |
3 days ago, # | thanks for the fast editorial |
3 days ago, # | I have a different solution for H so I'll leave it here. Let's assume is even. Consider operations . These operations reverse the whole permutation. For odd , one can do similar operations. During this reversing process, every pair of numbers become adjacent at some moment. Therefore we can "insert" adjacent-swap operations to achieve the arbitrary-swap operation in the final array. The pitfall is that when we try to do multiple arbitrary swaps, they can interfere with each other during the reversing process. However, if the target permutation has a matching structure, there are no worries about interference. Then, remember that we can obtain any permutation by composing two appropriate matching permutations. Therefore by repeating the reverse-and-swap process twice one can sort the input permutation. The time complexity is and it's better than the intended solution. However, the number of moves is and it's worse than . I'm so glad that the author didn't ask me to optimize this constant. (By the way, I'm wondering if it's possible to achieve a better constant factor with my approach.) |
3 days ago, # |
|
-
can you explain ,thank you
-
first, every number % gcd would be same.
second, if every number % (gcd * 2) would be same, then gcd will not be gcd, it should be at least 2 * gcd.
-
can u pls explain in detail
-
for example,
7 10 16
the gcd of the difference is
3
So we know that after mod
3
, they will be same. i.e.1 1 1
If we look at
gcd * 2
, i.e.6
. we can see the result would be1 4 4
. There could be only two elements in it. Also, if everything are still the same, then we must be have a wronggcd
in the first step.-
nice explanation I want to add that this rule will help in understanding this approach
if a % k == b % k then (a-b)%k==0
-
This property is nicely applied in 1826B - Lunatic Never Content question if anyone is interesting in using this property to practice a task
-
-
-
its not correct
-
it works when you do the GCD of the Difference Arrays only as mentioned in the main comment
-
Oh I thought I made it clear. The gcd in my comment means the gcd mentioned in mostafaabdelaal_03's comment, which is the gcd of the difference.
-
-
-
Hello, Sorry if my doubt sounds dumb but I agree that everynumber%(gcd*2) won't be the same but how can you claim that those "not same" %(gcd*2)s will be "same" as there have to be exactly 2 numbers one would be those who are still %(gcd*2)==0 and the other ones who are not zero but how can you claim that the other ones who are not zero will be equal
-
let's say every number mod
gcd
isx
, i.e.a[i] = x + num * gcd
for some integernum
then everything mod
2 * gcd
would be eitherx
, orx + gcd
.This is because when
num
is even,a[i] % (2 * gcd)
would be stillx
,when
num
is odd,a[i] % (2 * gcd)
would bex + gcd
-
-
-
3 days ago, # | In problem D, when we are doing operations on a[i]and want to make it to "tar". You are just doing partitioning. Won't there be cases where we partition a[i] into y, a[i]+k-y, and then recursively do operations on both of them? |
3 days ago, # | The amount of effort that went into this round is insane, the timeline doesn't really do it justice. Kudos to TheScrasse for never settling on anything less than perfect, and errorgorn for coordinating this extremely demanding set. This turned out to be one of the most quality rounds in my recent memory! |
-
Thanks a lot for the solution of problem H! It's my favorite problem in this contest, but I don't deserve the merit because you've solved it :)
3 days ago, # | Thank you very much for fast Tutorial! I dream of seeing an analysis of the problem D. Thank you for this good round! |
3 days ago, # | How to solve F1 with combinatorics? |
-
A way to think of this is to maintain the invariant that the difference of p between consecutive elements in front of the ith element is correct and also to satisfy the current consecutive difference. Denote unallocated elements smaller than i as k.
If we want to make a consecutive difference of 2, we must assign the current largest element in previous k-1 vacant place, and place any of other k-1 element in current place which is (k-1)*(k-1) ways. Note that placing the current largest element in front of ith element will not break the difference invariant.
If we want to make a consecutive difference of 1, we can either put the current largest in the previous k-1 vacancy or place any of current k element in current place. This results in 2k-1 ways. As mentioned before, placing the current largest element in front of ith element will not break the invariant.
while (true) { int div = 0, ndiv = 0; for (int i = 0; i < n; i++) { // st.insert(a[i] % d); if (a[i] % d == 0) div++; else ndiv++; } if (div > 0 && ndiv > 0) { cout << d << '\n'; return; } // if (st.size() == 2) // { // cout << d << '\n'; // return; // } d *= 2; } why is not working when I keep the track of two distinct remainders using two variables instead of a set. the loop is running infinitely for some test case when I use the two variables. |
-
I had similar approach, got any idea why using set/map to check the size won't work?? i am still stuck with that part.
-
got it
-
can you explain?
-
-
3 days ago, # | |
3 days ago, # | can someone please explain first three lines of solution of problem D ? |
-
let's forget array and assume that we have to deal with single element.
if this element is greater than k say (k+x),now in one operation we have to find 2 number a,b such that a+b=(k)+(k+x), to reduce complexity of question we are doing this trick
a+b=(k)+(k+x)
=>(a-k)+(b-k)=(0)+(x)
we don't care about final number we care about how many operation so we reduce all number by k so our operation changed in select number and break it such in two number such that sum is our x((k+x)-(k)).
in short after reducing by k we don't have to deal with k;)
3 days ago, # | All the problems were great and overall the contest was very enjoyable to solve. Thank you for such a good round! |
3 days ago, # | Worst contest for me so far. Didnt solve any. For who wants to see pure pain: |
3 days ago, # | Story timeline of the round was something new and quite interesting, I enjoyed it. |
3 days ago, # | In question C I have a test case : 5 1 2 3 6 7 3 4 5 7 8 1 2 3 4 5 in which many got answer 14 and 18. according to me answer is 18 as 14 is not possible please comment on it. |
3 days ago, # | why my solution gives WA 238595753 in B. Got the logic but got stuck with WA. can anyone explain why this is happening?? |
3 days ago, # | Last Dance, gud taste m8. |
This is my soln to the problem B:
this passed the pretests but in the system test's testcase-4 line 130 it gives ans as 128 but my output is 2. Idk how this is possible with this code cause every time for every power of two a new map is made and values are added... also, it's almost the same as the ans present in the editorial... can someone explain why my soln would give the wrong ans...? The link to the submission is:- 238532052 |
-
Your output on line 130 is 10 not 2.
As a counter-example consider, a = [10, 20]
3 days ago, # | In the editorial of problem D(solution section), Shouldn't it be "replace y with x+y" instead of "replace x with y+z" |
3 days ago, # | Amazing contest! Problems were fantastic. Thanks a lot for the round TheScrasse and all testers. |
3 days ago, # | Can anyone explain the concept behind problem D.I didnt understand the editorial |
-
Assume we create a new board , on which for each number written on our original board (call it ), we write the number on . Then, let's try seeing what our operation does. So, since (for the original board), and since we've written on our modified board instead of , this gives the observation that on , our operation just corresponds to replacing by two numbers such that . Then, solving this reduced problem is pretty easy (for implementation purposes, just subtract from each element of , and do the calculations on that itself).
3 days ago, # | spoilers make the whole text unreadable |
3 days ago, # | writing complexity at the end of an editorial is cringe |
3 days ago, # | problem B was really something... |
-
it was easy
-
can u explain editorial?
-
well it is kind of obvious intuition you get once u see 2 distinct values modulo some k you think of 2 since only two values are possible however then you remmeber all values might be pair hence there is only one value and then this amazing idea gets to u about the next power of 2 which is 4 well since the numbers were allwith the same parity they have only 2 values possible (pair numbers possible values are 2 or 4 and impairs values are 1 and 3) and then again there is possiblity that they all have same value time to check 8 again only 2 values are possible, you can conlcude that the sol requires checking all powers of 2,genius,see this much easier than what the editorial makes it look like.By the way i just guided you trough how the logical thinking went in my brain
-
-
3 days ago, # | TheScrasse How does the grader for H work? |
-
-
(Nvm, this is wrong if you run it on a decreasing array.)
By the way, isn't the number of moves for the editorial solution actually bounded above by ? Since the number of moves during the second phase is at most the number of Bs after the first phase, which is at most due to there being no two consecutive Bs and the last letter being an A.
-
Also curious about these:
Were you aware of a solution to H that made operations before the contest? Or did the limits just happen to be large enough to allow that to pass?
What was the original solution to H with operations?
-
H with operations sounds like a considerably more boring problem, as I believe you can simply simulate merge sort: suppose the right part starts at , then you can use operations on , etc to create a train of moving elements from right to left, and you remove elements from the train when they reach their final positions.
Other than the very annoying implementation, H with operations is a work of art :)
-
Hm, I don't fully understand.
By merge sort, do you mean that given two sorted arrays on consecutive ranges, you can merge them into one sorted array?
If you do operations on then this will move some elements right to left over and others left to right over . But how are you removing elements when they reach their final positions? What if there's some element in the middle of the train that you don't want to keep moving?
-
Here's how I did it: imagine inserting just the rightmost element of the left half with operations. can trail behind two positions away for free simply by extending the segments to the left (skipping the first one). We stop the trail early if doesn't need to go that far, and proceed to . Final case is if needs land next to , in which case we add one more swap at the end to achieve that.
-
I agree about and , but what if and need to keep moving to the right but you don't want to keep moving to the right?
It's true that must move at least as far right as , but once you take into account the fact that must trail two spaces behind to move them rightward at the same speed, this is no longer true.
-
-
-
-
- I was not aware of any linear solution other than the official one. I didn't put as the operation limit to avoid spoilers.
- The original solution was "put the smallest elements in the left half of the array in operations, then recurse". Some testers found the "merge sort" solution explained by ffao.
-
3 days ago, # | hints are very helpful,thanks for editorial |
3 days ago, # | Can somebody please explain why my solution of problem C — Heavy Intervals does not work |
3 days ago, # | Writing timeline of the round is very cool. |
3 days ago, # | too much math and number theory |
3 days ago, # | Very well written editorial! Excellent hints that actually do help a lot if we don't want to see the full solution. Thank you TheScrasse for the good contest and the great editorial. |
3 days ago, # | problem D is very nice! |
2 days ago, # | The subtasks of F is helpful for some but also misleading many participants including me :( anyways, good problems and good contest |
2 days ago, # | In E, I only understand this phrase in the statement after reading editorial:
It's my fault to not read it properly, and couldn't think that it is possible to press all buttons |
2 days ago, # | Thanks for the editorial :) |
Can someone please prove this If ai mod k=x , one of the following holds: ai mod 2k=x ; ai mod 2k=x+k ; problem B |
-
This is not a proof but this might help in understanding more intuitively. consider this array A=[42,10,26]. let's write these numbers in the bit representation.(i will use term 'bit position k' which means kth bit from right).
42 = 101010
10 = 001010
26 = 011010let's take k=4
can you tell what will be A[i]%k for all i ,(where k=4) just by looking at bit representation?using k=4, we got only one distinct element i.e. 2
let's do same thing for k=2*k. follow the same steps that we did for k=4
for k=8, we will get only one distinct element i.e. 2
for k=16, we will get only one distinct element i.e. 10now when k=32, the set bit position is 6. The bits for each number in A which lies on right side position of set bit are reminders.
42%32 is 01010
10%32 is 01010
26%32 is 11010for k=16 we got
42%16 = 1010
10%16 = 1010
26%16 = 1010see the transition in results from k=16 to k=32. Upto bit position 4 the reminder is same(which is 10) but when we did the mod operation for k=2*16 we got 5 bits. The new bit that we got will be either 0 or 1. If it is 1 we will add previous k(16 in our case) to the previous reminder, and if it is 0 we will get the previous reminder as it is. So we finally got 2 distinct values which are (previous reminder+previous k) and previous reminder as it is.
2 days ago, # | B was such a good question. Was stuck on the gcd approach for a while before realizing the pattern in the bits. |
2 days ago, # | Lot of mathematics, I feel they should also try not giving just mathematics. B was too hard for me,i never would have thought it that way. |
2 days ago, # | In Problem D why the answer is -1 when we have both negative and positive in our array after applying the operation (a[i] = a[i] — k) ? |
2 days ago, # | Alternative solution for E: Let's call a subset of pressed buttons good if at the end at most lamps turned on. For , the amount of such subsets is . We can find them for each in , and then check only them for each testcase in . |
-
Actually, you can prove that the number of such subset for is exactly since we can think of each switch associated with a vector and all the vectors are independent, so , , is the basis, and we only care when has less than or equal to bits on (excluding the case it is equal to zero), so we get the above result.
In problem D could we have solved for the smallest x in x*k+ Sum of Ai which is divisible by x+n |
Can anyone help me with a simple test case for which my submission is wrong for C? It's the same logic as the above solution |
2 days ago, # | I love long and clear explanations in math and number theory problems. thanks to errorgorn TheScrasse franv Endagorion and everyone who contributed. |
In problem C, making small intervals is not the same as sorting the sequences of left and right endpoints and pairing the corresponding indices. Let, si obtained after sorting and is obtained after sorting . Then the new intervals will be . |
-
what are you trying to say ? can you explain a little more ?
-
What is the difference between the approach explained in the editorial and my approach?
-
Your approach fails for , , . You claim that it's optimal to keep everything as it is, while the editorial makes , , ( is matched with the closest , which is ). The picture "explains" why the second solution is better than the first.
-
Hi, I didn't understand the picture can you explain it once? what is the 3 and 5 and what the different colors represent
-
The red subinterval is the only one whose cost changes (from to ). In general, you want intervals with big cost to be as small as possible, so you let intervals with small cost "steal" subintervals from intervals with larger cost.
-
-
-
-
2 days ago, # | Hello, would be great if someone explained why the following idea for B does not work https://codeforces.com/contest/1909/submission/238648704 I was thinking of the following test cases It should seem to always be possible for k to be a single digit value unless all numbers are multiples of 10. If there are 2 distinct numbers for each last digit, k=10 is always a solution. If there are more than 2 distinct numbers for each last digit, we can iterate k=2-9 to find a value for which it works |
2 days ago, # | learned a property of divion and remainder in binary form.Thank you |
2 days ago, # | In problem E, What the following line does inside the
|
47 hours ago, # | Thanks for interesting tasks. Little comments for authors. problems are really cool, because they can be solved without any algorithms with only a brilliant idea. Problem . Maybe it was better to make more samples, because there were a lot of WA's and a lot of greedy solutions can pass example tests(but of course incorrect). |
47 hours ago, # | Problem E is kind of funny.I tried to use complicated graph algorithms but failed.And the official solution is cool!I love it! |
46 hours ago, # | Thanks for the fast editorial! |
46 hours ago, # | I have another solution for problem G. It is easier than the intended one in my opinion, at least idea-wise. Solution |
44 hours ago, # | In D, I think it should be y+z = x+k instead of x+y = z+k. |
35 hours ago, # | I can understand the solution of G now,but I've got no idea about how the vital observation " is valid then is valid if and only if " was found.What inspired you think about that?Can someone help me? |
31 hour(s) ago, # | I see lots of people have doubts about why '2' in B,and I have difficulty in it too.Actually it's difficult to find 2 when in competiton for man who didn't cotact number theory. |
-
I can tell you how it worked for me. It is easy to get that if the array has even one even and odd number k can be taken as 2. Now the arrays left will have either all even or all odd numbers.
For arrays with all even numbers, upon taking k as '2' the only value we get will be zero, but these might not all be divisible by '4' which forces us to think to take k as 4, which can yield two distinct values as zero and two, and again if this doesn't work we can keep on taking k as 8, 16, 32...
For arrays with all odd numbers if we take k as 2, the only value we'll get is one, or the other way round if we remove 1 from all numbers again they all are divisible by 2, which again forces us to think that these all numbers(after one being removed from them) might not be divisible by 4, hence taking k as four can yield values as one if the number is divisible by 4 or yield as three if it's not divisible by 4, and again we can take 8, 16, 32...similarly until we get two distinct values.
29 hours ago, # | can someone please explain with a proof how does subtracting k from every a[i] in problem D, effectively convert it into a shifted problem with k = 0? for all problems with k = 0, our job is to divide the sum of the numbers in the array into pieces which are all equal right? so i assume, we subtract k from the numbers in the original problem in order to reduce the sum. but since every time we do an operation we increase the sum of the numbers in the array by k, how does it guarantee that subtracting k from all the numbers lets us forgo this requirement, i.e. we subtract n*k from the original sum but it is not true that we have to do exactly n operations every time and hence subtract n*k from the original sum. i'm sure there is some misconception in my assumptions, if someone would be nice enough to point it out to me, it would be very cool. thanks for reading. |
in problem c I'm trying to follow the editorial in the proof section he is saying at the third line If you swap ri and rj, the cost does not increase. I did that on the example n=2 provided and the cost increased from 6 to 7 first step he said match them any other order so: [2,4] [1,3] then he said You have also assigned some cost ci to [li,ri] and cj to [lj,rj] . [2,4] ci=2 [1,3] ci=1 now cost is 6 then ** If you swap ri and rj, the cost does not increase. ** swapping here will make the cost 7 so I don't get it can you please explain what is wrong here |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK