Editorial of Codeforces Round 889 (Div. 1 + Div. 2)
source link: https://codeforces.com/blog/entry/118540
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.
Author: Kaey
Preparation: akifpatel
Official implementation: will be posted after the system tests.
1855B - Longest Divisors Interval
Author: TheScrasse
Preparation: TheScrasse
Official implementation: will be posted after the system tests.
Author: TheScrasse
Preparation: akifpatel
Author: TheScrasse
Preparation: akifpatel, dario2994
The hints and the solution continue from the easy version.
Official implementation: will be posted after the system tests.
Author: TheScrasse
Preparation: akifpatel
Official implementation: will be posted after the system tests.
Author: TheScrasse
Preparation: akifpatel
Official implementation: will be posted after the system tests.
Author: TheScrasse
Preparation: akifpatel
Official implementation: will be posted after the system tests.
Author: dario2994
Preparation: akifpatel, dario2994
Official implementation: will be posted after the system tests.
Author: dario2994
Preparation: akifpatel, dario2994
6 hours ago, # | really nice problems and fast editorial, thanks for the contest |
6 hours ago, # | thanku for the editorial . |
6 hours ago, # | bitset in the author's solution, are you retarded? |
-
Has to be one of the most bullshit problems lately. could have been up to 10^4 and the problem would be just as hard but 10 times better.
-
it's way easier to think of n^2 dp without bitsets
-
Then something could have been changed so you that the trivial DP solution didn't pass. Bitset is just a constant optimization and not a complexity optimization.
-
So is problem A. Constant optimizations are fine if they cause big difference in runtime, cp is not purely theoretical...
-
That's different. A pragma can make a code run 2x faster. That doesn't mean there should be pragma problems on contests. Plus, 10^5 is usually intended to be solved in at most , not quadratic time.
-
Just because bound is in range does not mean solution should be certain subset of complexities. All that matters is if it runs fast or not, and preferably intended runs significantly faster than worse approaches (32x is significant, 2x is not).
-
-
-
If you assume the values of the problem fit into an integer, then implicitly you assume that the length of the integer data type you are using is . That means that if you apply bitsets, you are guaranteed to at least divide the runtime by a factor. So actually the solution is or better. Another justification for this lowerbound of on the word size is here: https://en.wikipedia.org/wiki/Word_RAM.
This means that it is better than quadratic.
-
You cannot assume a bound for n when calculating time complexity. Following that logic, since the code will do at most 10^10 operations, then theoretically it's O(1) with an extremely high constant. Sure, on practice you can optimize quadratic in 32 or 64 times, but it's still quadratic.
-
It's not that you bound the maximum , the actual logic, is, assuming you are going to do testcases with huge values of , you will also upgrade your computer to have a bigger word size, otherwise you can't even store the input inside integers.
-
Complexity depends on what you count as a single operation. The word ram model is probably the most common model where you consider an integer operation as one operation, which is why you are able to divide by factor of lgn in complexity.
If you instead want to say every bit flip counts as single operation, you have to multiply lgn complexity of every addition/multiplication every time you do big O complexity, which is almost always not very insightful and annoying.
-
-
-
-
-
I disagree. If passes, then people could formulate a dp without noticing the connection to knapsack.
-
the bitset is still O(n^2)...
-
its atleast O(n^2/logn) as pointed out by jeroenodb
-
no, the time complexity remains as O(n^2), it's simply been optimized by a constant factor of 32-64, which doesn't affect the actual complexity? if my understanding is correct
-
that 32/64 is coming from log of Integer size (log 10^9 or log 10^18), so its a log factor.
-
-
-
-
-
-
Does this mean it cannot be solved in any language other than C++
-
Sometimes it's difficult to make a problem suitable for other languagues while not nerfing it for C++ enjoyers. When there are problems with solution, there are people who do magic and get AC for solution just because AVX and because stupid solutions have incredible constants while smart solutions have a big constant
-
You can technically write your own bitset in most languages (like this witchcraft: https://codeforces.com/contest/1854/submission/216290667)
6 hours ago, # | Excellent, insanely fast editorials! |
6 hours ago, # | Can someone explain the bitset part for Earn or Unlock? What is ? How to update it? |
-
is similar to knapsack . says if it's possible to unlock exactly cards after first moves (in knapsack it says if the sum of weights can be if we look on first elements). In knapsack you can see that to count all values of you only need values of , and it can be further optimised to one-layer dp. So, in other words, knapsack can be written as , where we iterate from to . Consider this as a giant bitmask. Then, when we update dp with , this is equivalent to updating like this: . In this problem is calculated in a similar (not the same) way
It was so curiously but cool enough, thank you :))) But why there were n,m <= 500 in C task if solution works in ? |
can anyone help me debug this. why is this failing for D2D on a pretest 4 https://codeforces.com/contest/1855/submission/216341178 |
-
Take a look at Ticket 17016 from CF Stress for a counter example.
5 hours ago, # | nice problems and rly quick editorial with hints(best kind of editorial). thanks for the contest |
5 hours ago, # | I submitted a solution to B https://codeforces.com/contest/1854/submission/216340662 in the end of the contest which I knew wouldn’t get AC (I had nothing to lose at this point) can’t believe the author’s accepted solution is the same but using bitset :\ . I can already see a day the author’s solution is gonna require some #pragma optimiza_some_bs. Really didn’t like this question at all. |
For problem B longest divisor interval.. Longest interval always be under 44? As a newbie, I wanted to find out what is the longest interval I can get ..i used lcm of range from 1 to some x till lc exceed 10e18 , it turns out to be x<=44; |
5 hours ago, # | I really liked div2C2, it is a well-rounded problem, and discovering why k=31 is the worst case was really fun! |
-
I was stuck at 34 moves, how did you come to the k=31 soln
-
The solution when every value is either non-negative or non-positive takes at most 19 moves.
For C1 my idea was to convert negatives to positives when there is fewer of them, and vice versa. The converting is done by adding the biggest positive to every negative value. However if the value of the maximum positive is smaller than absolute value of smallest negative, you must waste at most 5 moves by adding it to itself, that is when
maxPositive = 1
andminNegative = -20
. Worst case scenario is when there is equal number of negative and positive values. So for C1 my solution takes at most19+5+10=34
moves. I guess you discovered that already.Now, for C2, notice that if the maximum absolute value is positive, you need at most
19+countOfNegatives
moves. This strategy only takes10+19
when the number of negative and positive values is equal. And, it is better than our first strategy as long as the difference between number of negative and positive values is smaller than 5. This means that the turning point is when the number of negatives is 12 and the number of positives is 8. The second strategy then takes19+12=31
moves! And when the number of negatives is 13, positives 7, the first strategy is better, taking19+5+7=31
moves!.
-
5 hours ago, # | This was an amazing round, thank you for the nice problems :D |
5 hours ago, # | Feedback on the Div. 1 round: tl;dr ACD are great problems; BE are mid. The problem statements were great, but there were numerous FSTs that probably could have been avoided. Overall, the round was decent. A: Great problem, although I think the n <= 50 subtask should have been worth a bit less since the observation that the problem is easy when all numbers are positive/negative was a lot more obvious to me than the observation that we can make all numbers positive/negative in 12 moves and then finish in 19. B: Not an awful problwem, but coming up with the DP is pretty trivial and the hardest part is just confirming that bitset will be fast enough. Not my favorite task. C: Nice little EV problem. n <= 500 baited me into thinking intended may be O(n^3), but the DP is cute and it's a nice linearity application. D: Fun interactive; I really enjoyed trying different ideas to puzzle this one out. Probably my favorite problem of the round. E: The idea to trying to start with a bunch of small numbers and then add in some big ones to make the desired answer felt pretty standard, so the main new part here is that even if any individual starting set might not work for all N, we can quickly try a bunch of different starting sets until we find one that's valid. (fwiw, my implementation is a lot simpler than what the editorial describes--I literally just brute force starting sets in a reasonable order and end up ACing in 15ms.) This felt a bit like a guessing game and didn't feel satisfying or clever to come up with. I think I (mildly--it's not a terrible problem by any means) dislike this problem for the same reason I dislike B: the solution is basically just to tweak something stupid rather than to make a clever observation. F: Didn't read. (fwiw, I think this and the last round are maybe a reason to have Div. 1.5 rounds rated for, say, 1600-3000 or something, and/or to have five-problem Div. 1 rounds--problems like this one are relevant to only a handful of people in the contest pool and it might allow for more contests if not every round needs to have a really hard problem at the end.) Feedback on preparation: I liked that pretests = systests on D/E (I think, anyway), and the problem statements were all clear, short, and easy to parse. However, I think A should have had a higher test limit (I don't see why 1-2k tests per input would be infeasible?) and B and C should have been multitested. (I don't think multitesting makes the complexity calculation weird for either of those problems or allows unintended solutions to pass.) Thanks to the authors for the round! |
-
Thanks for the feedback! :)
Problem B does not have multitests because there is no easy way to declare variable-length bitsets.
-
Wouldn't the complexity stay the same if you declare one bitset of size 2e5 and use it for every test case (setting elements to zero after each TC), assuming the sum of was bounded by ?
-
can someone explain what is the factor "w" in the complexity of 1854B ? |
-
is the system word length (basically, the number of bits the processor stores in one unit; generally either 32 or 64).
Problem 1855B could also be solved with Binary Search, I have no idea how it passed the pre-tests, but here is some code if anyone is interested. |
-
fwiw, mine is also not randomized. I iterate over the possible "base sets" in an order that's basically just counting in base 60. For a given base set, I check if an answer exists by computing the number of ways to get each sum up to 60, then adding additional values. In particular, I iterate over from to , where is the number we can get in the most distinct ways. Then, while , I add one copy of to the solution. If this process generates a valid solution, we can output it.
Checking a given base set takes time. The expected number of base sets we must check is small (intuitively there is no reason any given number will fail for all base sets), and my solution passes in 15ms.
5 hours ago, # | What could be the difficulty ratings for all the problems ? |
5 hours ago, # | My solution for E: Use times of and times of , where . And for every from to , we'll put an in if the total count does not exceed . Because , we can't include latter type numbers into one set, so the total count will be trivial to calculate. The time complexity is . I don't know why this is correct but it actually got Accepted. Submission: 216320776. |
5 hours ago, # | Can you help me with d2D? I have a bit different approach with dp: dp[i].first = minimal cost of cards to unlock ith card, dp[i].second = max j that jth card can be unlocked using same set of cards as ith card can. I recalc dp with segtree. Why this idea can lead to WA? |
-
Take a look at Ticket 17022 from CF Stress for a counter example.
4 hours ago, # | Can anyone please explain Problem-C , I was getting MLE in 216330218 |
-
My approach ->
Case 1 -> If all the elements are +ve, take prefix sum. We can do it in n-1 steps.
Case 2 -> If all the elements are -ve, take suffix sum. We can do it in n-1 steps.
Case 3 -> If there are some +ve and some -ve elements, We first try to convert all the -ve elements to +ve. We can do this by adding the max element to the -ve elements. If the maxEle < abs(minEle), then we increase the maxEle by doing the operation {maxIdx,maxIdx} while maxEle < abs(minEle).
Then, we add maxEle to all the -ve elements. Now, as all the elements have become +ve, we take prefix sum.
Dont know why this worked, so please can someone explain me how i got this lucky to pass all system test in this solution 216290096 |
-
Your solution basically checks all ranges between and finds the longest one.
We can prove that this will always find the correct answer.
Fact 1: We can show that the range will be optimal for some . In other words, if (for given ) the longest good range has length , the range must also be good. For proof of this fact, check my comment here.
Fact 2: We can show that the answer never exceeds (for ).
Proof: Suppose the answer for some is . What do we know about this ? We know that is a multiple of each of , according to Fact 1 (and this is a sufficient condition for such an to exist with answer ). This means that is a multiple of . But notice that , so such doesn't exist for within the constraints of the problem. This means that the maximum answer will be , and your solution will find it.
My approach for C2 was a bit different. Let's suppose the maximum absolution value is mx. If mx is from positive, we'll first modify the array like this: v3-v1 >= mx, v5-v3 >= mx, v7-v5 >= mx ... For this, we'll kind of use 2 moves for each position, so we'll need n/2*2 = 20 moves. After that, for each of v2, v4, v6... we can find a valid value (v[i-1] <= x <= v[i+1]) using only one move, except for if the last unmodified value is the nth index, for that we'll use 2 moves. So total = 20 + 9*1 + 2 = 31 We'll do the same if mx is from a negative value, but the other way around. |
3 hours ago, # | How is a formal proof to the model solution of a problem in a competitive programming contest NOT required? |
Can anyone please explain the solution for problem Div2-D ? I read the editorial but didn’t quite understand it. |
3 hours ago, # | I performed poorly due to a silly mistake, but I loved the round thank you. |
3 hours ago, # | Damnn I can't believe question B took all my time, and all I needed was to check the cintinuity from l=1. sigh |
3 hours ago, # | why did I rethrow the second task for fun an hour later ... I didn’t know that only the last attempt that passed all the pretests would be considered ... |
3 hours ago, # | Can someone Please explain div2 B , I saw few codes in which brute forcing <= 100 works but I am unable to understand what is going on and how this thing works ? kindly explain please |
2 hours ago, # | Thank You for the amusing problems and the great song :) ! |
112 minutes ago, # | Does anyone know how to apply the solution for the A?, I'm trying with cpp but it doesn't work. |
86 minutes ago, # | any one to explain this more please?: (div2 hard C) Therefore, you need additional moves. Since , , as we wanted. Now you can simulate the process in both cases (positive and negative), and choose one that requires moves in total. |
34 minutes ago, # | My solution to Div. 1 A2 / Div. 2 C2 differs from the editorial and uses at most 30 moves: |
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK