2

Editorial of Codeforces Round 889 (Div. 1 + Div. 2)

 1 year ago
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.
neoserver,ios ssh client
By TheScrasse, 7 days ago, In English

1855A - Dalton the Teacher

Author: Kaey
Preparation: akifpatel

Hint 1
Hint 2
Solution

Official implementation: will be posted after the system tests.

1855B - Longest Divisors Interval

Author: TheScrasse
Preparation: TheScrasse

Hint 1
Hint 2
Hint 3
Hint 4
Solution

Official implementation: will be posted after the system tests.

1854A1 - Dual (Easy Version)

Author: TheScrasse
Preparation: akifpatel

Hint 1
Hint 2
Hint 3
Hint 4
Solution

1854A2 - Dual (Hard Version)

Author: TheScrasse
Preparation: akifpatel, dario2994

The hints and the solution continue from the easy version.

Hint 5
Hint 6
Hint 7
Hint 8
Hint 9
Solution

Official implementation: will be posted after the system tests.

1854B - Earn or Unlock

Author: TheScrasse
Preparation: akifpatel

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Solution

Official implementation: will be posted after the system tests.

1854C - Expected Destruction

Author: TheScrasse
Preparation: akifpatel

Hint 1
Hint 2
Hint 3
Hint 4
Hint 5
Hint 6
Solution

Official implementation: will be posted after the system tests.

1854D - Michael and Hotel

Author: TheScrasse
Preparation: akifpatel

Hint 1
Hint 2
Hint 3
Hint 4
Solution

Official implementation: will be posted after the system tests.

1854E - Game Bundles

Author: dario2994
Preparation: akifpatel, dario2994

Hint 1
Hint 2
Hint 3
Solution

Official implementation: will be posted after the system tests.

1854F - Mark and Spaceship

Author: dario2994
Preparation: akifpatel, dario2994

Hint 1
Hint 2
Hint 3
Solution

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?

  • 6 hours ago, # ^ |

    it's something new

  • 5 hours ago, # ^ |

    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.

    • 5 hours ago, # ^ |

      it's way easier to think of n^2 dp without bitsets

      • 5 hours ago, # ^ |

        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.

        • 5 hours ago, # ^ |

          So is problem A. Constant optimizations are fine if they cause big difference in runtime, cp is not purely theoretical...

          • 4 hours ago, # ^ |

            Rev. 3  

            -30

            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.

            • 4 hours ago, # ^ |

              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).

              • 4 hours ago, # ^ |

                but i am bitset-phobic

            • 3 hours ago, # ^ |

              Bitsets have already been used in problems. There was a problem with solution that runs in , where was up to

        • 4 hours ago, # ^ |

          Rev. 2  

          +68

          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.

          • 4 hours ago, # ^ |

            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.

            • 4 hours ago, # ^ |

              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.

              • 4 hours ago, # ^ |

                That's fair, but the optimization is still limited to your computer's architecture. Your compiler may fit 128 or 256 bits into an integer in a 32 bit architecture, meaning you're still getting x32 optimization.

            • 4 hours ago, # ^ |

              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.

      • 4 hours ago, # ^ |

        How is it possible to do as n was given 10^5

    • 5 hours ago, # ^ |

      I disagree. If passes, then people could formulate a dp without noticing the connection to knapsack.

      • 4 hours ago, # ^ |

        the bitset is still O(n^2)...

        • 4 hours ago, # ^ |

          its atleast O(n^2/logn) as pointed out by jeroenodb

          • 4 hours ago, # ^ |

            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

            • 3 hours ago, # ^ |

              that 32/64 is coming from log of Integer size (log 10^9 or log 10^18), so its a log factor.

              • 3 hours ago, # ^ |

                yea its a log of the int size, which is a constant, so its still a constant factor. comparatively, it wouldn't be accurate to say that it runs in O(n^2/cbrt(n)) just because of the fractional constant factor.

  • 5 hours ago, # ^ |

    Does this mean it cannot be solved in any language other than C++

    • 4 hours ago, # ^ |

      Welcome to codeforces

    • 3 hours ago, # ^ |

      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

    • 2 hours ago, # ^ |

      No; see this Python solution by misorin. (Congrats on the first Python AC!)

    • 112 minutes ago, # ^ |

      You can technically write your own bitset in most languages (like this witchcraft: https://codeforces.com/contest/1854/submission/216290667)

  • 113 minutes ago, # ^ |

    cope, it's just skill issue

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?

  • 2 hours ago, # ^ |

    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

6 hours ago, # |

Rev. 4  

0

It was so curiously but cool enough, thank you :)))

But why there were n,m <= 500 in C task if solution works in ?

  • 5 hours ago, # ^ |

    there exists some cubic solutions (like mine) which they wanted to pass as well ig

5 hours ago, # |

Rev. 2  

0

can anyone help me debug this. why is this failing for D2D on a pretest 4 https://codeforces.com/contest/1855/submission/216341178

  • 5 hours ago, # ^ |

    Take a look at Ticket 17016 from CF Stress for a counter example.

5 hours ago, # |

  • 5 hours ago, # ^ |

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.

  • 5 hours ago, # ^ |

    same happened to me , didnt know of that kinda optimization though

  • 3 hours ago, # ^ |

    unlike pragma, bitsets are not constant time optimizations in some sense

  • 2 hours ago, # ^ |

    When using pragmas in different situations you can get different results, sometimes RE or the time even increases. However with bitset you know, that everything with it works times faster

5 hours ago, # |

Rev. 3  

0

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, # ^ |

    It ACs after 42, the answer to life, the universe, and everything :)

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!

  • 3 hours ago, # ^ |

    I was stuck at 34 moves, how did you come to the k=31 soln

    • 2 hours ago, # ^ |

      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 and minNegative = -20. Worst case scenario is when there is equal number of negative and positive values. So for C1 my solution takes at most 19+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 takes 10+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 takes 19+12=31 moves! And when the number of negatives is 13, positives 7, the first strategy is better, taking 19+5+7=31 moves!.

      • 2 hours ago, # ^ |

        your pfp offends me

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!

  • 5 hours ago, # ^ |

    Thanks for the feedback! :)

    Problem B does not have multitests because there is no easy way to declare variable-length bitsets.

    • 5 hours ago, # ^ |

      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 ?

      • 5 hours ago, # ^ |

        That's true, I missed it. So it would have made sense to make multiple testcases, sorry.

        • 5 hours ago, # ^ |

          No worries, thanks for the response and for preparing the contest!

5 hours ago, # |

Rev. 2  

+10

can someone explain what is the factor "w" in the complexity of 1854B ?

  • 5 hours ago, # ^ |

    is the system word length (basically, the number of bits the processor stores in one unit; generally either 32 or 64).

    • 5 hours ago, # ^ |

      Thank you, but how does that complexity could pass the n<=1e5 constraint?

      • 5 hours ago, # ^ |

        With , we have , suggesting that we're fine given the 3s time limit. In practice, bitsets have a pretty good constant factor, and my code passes in about 1s.

5 hours ago, # |

Rev. 3  

0

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.

https://codeforces.com/contest/1855/submission/216294987

5 hours ago, # |

Rev. 2  

0

tourist's solution for E seems not randomised although I don't really understand what it does and why there is some magic constant defined in it (216302036)

  • 5 hours ago, # ^ |

    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, # ^ |

      Thanks! It makes sense. tourist's solution seems similar.

5 hours ago, # |

What could be the difficulty ratings for all the problems ?

  • 5 hours ago, # ^ |

    CLIST usually gives quite accurate problem ratings

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?

https://codeforces.com/contest/1855/submission/216348756

  • 5 hours ago, # ^ |

    Take a look at Ticket 17022 from CF Stress for a counter example.

4 hours ago, # |

Rev. 4  

0

[deleted]

4 hours ago, # |

Can anyone please explain Problem-C , I was getting MLE in 216330218

  • 4 hours ago, # ^ |

    Rev. 2  

    0

    Keep the vector global and reset it before each test.

    • 4 hours ago, # ^ |

      bro, can you send the code making this modification. I am not getting AC

  • 4 hours ago, # ^ |

    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.

4 hours ago, # |

Rev. 2  

0

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

  • 3 hours ago, # ^ |

    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.

4 hours ago, # |

Rev. 3  

0

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.

4 hours ago, # |

How to solve div2D in python?

  • 2 hours ago, # ^ |

    There is likely no way

4 hours ago, # |

Can you help me with 216358050. Why it is failing on pretests

3 hours ago, # |

How is a formal proof to the model solution of a problem in a competitive programming contest NOT required?

  • 3 hours ago, # ^ |

    This is the next codeforces phase.

    From Mathforces to Guessforces to Hopeforces.

    • 118 minutes ago, # ^ |

      you forgot about animeforces

3 hours ago, # |

Rev. 2  

+4

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.

  • 73 minutes ago, # ^ |

    Idk what you're trying to do in your solution but answer is just number of i such that pi = i divided by two and rounded up.

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.

  • 61 minute(s) ago, # ^ |

    x1 + y1 <= 5, x2 + y2 <= 20, so x1 + x2 + y1 + y2 <= 25. Let X be x1 + x2, Y be y1 + y2, X + Y <= 25. If a + b = c, then min(a, b) <= c/2, because if min(a, b) > c/2, then с — min(a, b) < c/2, we got a contradiction. min(X, Y) <= 25/2

34 minutes ago, # |

My solution to Div. 1 A2 / Div. 2 C2 differs from the editorial and uses at most 30 moves:

https://codeforces.com/contest/1855/submission/216373889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK