szdytom's blog

By szdytom, 5 weeks ago, In English

2248A - You Delete, I Delete

Hint1
Hint2
Hint3
Solution

2248B - Merge to Match

Hint1
Hint2
Hint3
Solution

2248C - Maximize the Score

Hint1
Hint2
Hint3
Solution

2248D - Good Pair Queries

Hint1
Hint2
Hint3
Solution
Bonus Question

2248E - Excuse for Breaks

Hint1
Hint2
Hint3
Solution

2248F - Matrix Elimination

Hint1
Hint2
Hint3
Solution

2248G - No Balance Left

Hint1
Hint2
Hint3
Hint4
Solution

Also check out:

  • Vote: I like it
  • +146
  • Vote: I do not like it

»
5 weeks ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

Auto comment: topic has been updated by szdytom (previous revision, new revision, compare).

»
5 weeks ago, hide # |
 
Vote: I like it +56 Vote: I do not like it

is it just me or B felt harder than C and D?

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone explain why taking the current maximum length is not optimal in C

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    1 2 3 1 4 2 3 4

    if you choose "3" first, you can get 5^2 + 1^2 + 1^2 + 1^2 = 28.

    But if you choose "1" and "4", you can get 4^2 + 4^2 = 32.

  • »
    »
    5 weeks ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    There could be 2 pairs with the same length that overlap eachother

    For example : 5 4 3 3 4 6 5 2 2 1 1 6

    You can see that both pair 5 and pair 6 have the same length which is 7, and you can only pick one pair only, because they overlap eachother , and in this case, picking pair 5 is better than picking pair 6, but you cannot determine which pair is better with greedy, therefore you have to use dp

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    your algorithm fail when the current maximum appear at least 2 times. Like this testcase: 5 1 2 2 3 1 4 5 3 4 5 At the first operation, you don't know which value you should choose (1 or 3)

»
5 weeks ago, hide # |
 
Vote: I like it +17 Vote: I do not like it

D < C

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    It would be interesting to know how long it took the testers to solve problems C and D. Did they really solve D longer/worse than C?

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    its just dp it aint that hard

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    in my opinion C was the easiest problem, the easiest dp ever

    • »
      »
      »
      5 weeks ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I'm trying (and failing) greedy since yesterday.

      How did you immediately understand it was DP along with the type of DP?

      • »
        »
        »
        »
        4 weeks ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        I also implemented the greedy idea and got WA t2, but after failing I thought about a counter example, I didn't find one, but I got the idea of what one would look like. If it's not greedy then you default to DP. Here's a counter example: https://codeforces.me/blog/entry/155640?#comment-1382284. For experienced coders, they pretty much didn't get fooled by the samples and went straight to the correct answer.

      • »
        »
        »
        »
        4 weeks ago, hide # ^ |
        Rev. 2  
        Vote: I like it 0 Vote: I do not like it

        its pretty trivial, ill explain it to you.

        Either, if the number you are handling/processing right now has already occured before that literally means you have the option to take it, but if you do take it you lose all of the numbers in between the pairs. So what matters? The thing that matters is the best value possible to get before the first occurence of that number, so either you dont take the pair and add 1 to it, or you take the pair and add the best value of the previous index where the first occurence happened.

        With a simple example

        0 1 2 3 4 5 6 7 (indexes)
        1 2 3 1 4 2 3 4

        here at position 6, either we can add 1 to it extending from position 5, or we can take the best one from index 1 right before the first occurence of "3" in the array, and the maximum value at that index will be the best value at that index. Note, either you can take a pair or you cant, but if you do you lose everything in between the pairs. So, We basically have 2 options every time, but instead of a n^2 solution iterating through all possible 2 options we can maintain a dp to store the best maximum value at each position i so that we can use it later. That was my observation.

        i hope that helps :D

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hey can anyone help me out why a greedy strategy does not really work for problem C. Here's my submission: https://codeforces.me/contest/2248/submission/385189701

»
5 weeks ago, hide # |
Rev. 2  
Vote: I like it -18 Vote: I do not like it

szdytom I have a counter example of problem B where editorial solution fails --

Testcase:
1 10 20 30 40 100
3 15 35
Expected Answer - YES
But Editorial Code Answer - NO

Edit- solution is correct, I gave testcase in wrong format

»
5 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

[Deleted]

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For D's bonus question,

Spoiler
»
5 weeks ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

I overcomplicated D by doing binary search: 385178994

Suppose the whole array a and b are not the same mode (if they are the same then it is always good by selecting the whole array). I binary searched to find if the minimum number of elements I can delete so that both arrays have the same mode (if anyone is interested I can explain further in detail)

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

My contest discussion stream here for ABCDE

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem C has a tag "greedy", could anyone explain greedy approach or drop the code in a comment

»
5 weeks ago, hide # |
 
Vote: I like it +5 Vote: I do not like it

link C is insanely similar to this problem. I somehow solved that problem just a few hours before the contest. What a coincidence

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

$$$D$$$ really should have been the harder version (Bonus Problem ) . It's far easier for a $$$Div-2$$$ $$$D$$$

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The contest was kinda weird

Hard B, Easy D, dp C (compared to usual div2 B/D)

Managed to get E tho

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi guys, I'm getting WA on test 2 with this submission on problem B:

385171329

I believe it is logically equivalent to the editorial.

My checks are:

  • n >= 2 * m
  • a[i] < b[i] for all i
  • a[n — 1 — i] > b[m — 1 — i] for all i

The last condition is just the editorial's b[i] < a[n — m + i] written in reverse order.

I've compared the logic several times, and I can't see the difference between my implementation and the editorial. If anyone can point out the mistake or provide a counterexample, I'd really appreciate it.

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I think you may print "NO" more than once per test case in some cases

    • »
      »
      »
      5 weeks ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Thanks! That was exactly the problem. I spent too much time looking for a logical mistake, I completely overlooked it. Thank you for spotting it!

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Yes I think the problem is that you forgot to return from function in n < 2 * m check.

    • »
      »
      »
      5 weeks ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      Thanks for checking! The issue wasnt the logic at all, it printed "NO" more than once for a case because I didnt return it, just like you said. I appreciate the help!

»
5 weeks ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

Can someone explain their intuition for solving $$$E$$$? It's such a bizarre (but interesting!) question, but even more bizarre that so many people solved it in the contest.

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I completely agree. Besides the intuition, I'd also love to know if there are any classic problems with a similar idea. It feels like this problem is based on a well-known technique that I've somehow missed.

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    maybe it's only because that there are so many cheaters(:

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I made a comment below which may help. I didn't add a solution but if people want to see it I can.

  • »
    »
    5 weeks ago, hide # ^ |
    Rev. 3  
    Vote: I like it -19 Vote: I do not like it

    2248E - Excuse for Breaks

    Reasoning

    $$$O(m^2)$$$ (two pointers): 385222304
    $$$O(m^2 \log m)$$$ (binary search): 385196979

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

1000 contest rating first.And l solved AB uncommonly.For B,l think that we can find two numbers named n1 and n2(n1 >= bi,n2 <= bi)(0 <= i <= m-1) and delete both of them.If we can't find numbers from a to match all numbers from b, print "NO" or "YES".385158820

  • »
    »
    4 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I did the same! Turns out that the other way was actually more trivial and easier. But as long as it is accepted, I won't complain at all, lol

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

It's my first time to enter contest....and I didn't even solve B....I feel even doubt myself....Is anyone feel the same with me

»
5 weeks ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

Here is a (hopefully more intuitive) reformulation of E:

You and your friend go rock climbing. The rock wall (with height $$$n$$$) is very slippery, so if you ever take a break you will fall back down to the bottom. You will compete to see who can get the highest score.

Scoring works as follows:

  • You recieve $$$d$$$ points for every step up you take.
  • You recieve $$$r[i]$$$ points for reaching the checkpoint at height $$$p[i]$$$ (assume each step advances you one unit of height).

Your friend is very hyper, and he will climb up to the top repeatedly (returning to the ground each time he reaches the top).

Determine if there exists a strategy so you have more points than your friend at ANY point in time.

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it +6 Vote: I do not like it

    The reformulation is very close to how the problem was initially proposed! And that's why it's called "excuse for breaks". However we finally decided to rewrite the statement in pseudo code for clarity.

»
5 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

The proofs of A and B are amazing.

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem E has a tag binary_search, but the solution doesn't use it.

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it +11 Vote: I do not like it

    Most people uses binary search actually. The tag is meant to cover all solutions.

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Great round! Problem G was definitely the highlight for me. The idea of splitting the state space and conquering the problem in two parts (DP + GCD) is extremely neat. Really enjoyed the problem design!

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem B can be solved by normal simulation and i think its much easier to realize than the mathematical proof in tutorial.

I stored both a and b elements in one array say c (with their type, 1 means from array a and 0 means from array b)

sort array c

so by problem statement we simply understand that for each element b there has to be atleast 1 element of a less than b (to its left in c) and atleast 1 element of a greater than b (to its right in c)

This can be checked with two simple iteration

Forward: keep a dynamic counter for a elements, when i encounter a b element, i decrement the counter (this means i immediately resolve with an available a which is less than bi) if the counter is 0 and we encounter b element means there is no element a < bi hence instant NO

Backward: exact same counter logic backwards for an available a > bi

code: 385141172

TC: O((n+m)log(n+m)).

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

szdytom was there any reason in problem B that the elements are supposed to be distinct ?

The solution does not really change right if there are multiple occurrence of n , in A and B . Its always optimal to ignore these in pairs, and just use the remaining in either A or B for the greedy approach .

Was this done for easier implementation or am i mistaken here ?

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You are right, just for easier implementation since it's already a bit hard for B in my opinion.

»
5 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

Here is a (almost definitely) wrong solution to E that get's AC. Feel free to hack my solution.

Hint for where to look when hacking
»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi great Editorial

»
5 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Problem F editorial: 1) It seems there is a typo:

max(0, ⌈c1L−2⌉, ⌈c2L−1⌉, ⌈c1+c22(L−2)⌉)

Should be not L-1, but L-2.

2) How do you come from (L−2)T−y≥c1 to T = max(..., ⌈c1L−2⌉), why "y" has just disappeared?

  • »
    »
    5 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I solved F by ternary search on 2 intervals. Can't realize the straight formula solution from editorial...

  • »
    »
    4 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    You are right. I'm too sleepy yesterday and parts of the original reasoning is just pure bullshit. I've rewrote that part now, hopefully it is now understandable.

»
4 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In problem D: u0+x0≥⌈k/2⌉ and u0+y0≥⌈k/2⌉, which imply |x0−y0|≤u0+v0

How does that imply?

  • »
    »
    4 weeks ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    I skipped some steps. I've expanded it to a very detailed reasoning (with the help of LLM) including the answer to your question. Hopefully the can help :)

    • »
      »
      »
      4 weeks ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      Yeah, thanks, it helped. I wonder if someone bothered to prove necessity during contest. I personally only did sufficiency and hoped it would work

»
4 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A very simple way to solve B:

Since all the n+m numbers are distinct. We must have n — m > m or n > 2m.

For each number in B to get generated from two numbers in A, we must have one smaller as well as larger element present in A.

Thus, sort A and B.

Check A[i] < B[i] for all i. --> One smaller present which will not be exhausted from previous element.

Check A[i-th from last] > B[i-th from last] --> One larger present which will not be exhausted from next element.

Code: https://codeforces.me/contest/2248/submission/385352600

»
4 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

How to do the bonus problem D better than O(n^2)?

  • »
    »
    4 weeks ago, hide # ^ |
    Rev. 2  
    Vote: I like it 0 Vote: I do not like it

    For D's bonus question,

    Segment Tree Approach

    TC : O(nlogn)

»
4 weeks ago, hide # |
Rev. 2  
Vote: I like it 0 Vote: I do not like it

In F as per editorial, we fixed z and we are trying to get a possible interval for y, if its not possible for the z we choosed, why can't we try for z+1 ? Please let me know if I'm missing something

Upd: Understood, after checking the case where y might not get an interval, I can observe that our intuition of z >= (s-2*vk)/(L-2) is not possible. So if y does not get an interval then we go for full.

»
4 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I was able to figure out D easily but couldn't even begin B & C. Really need to work on my DP

»
3 weeks ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I think a part of the presented solution to E is incorrect. In particular, the part after "Induct on the number of runs". How does $$$S_x+S_{|t|}+\max(M,0) \leq S_{|s|} + \max(M, 0)$$$ follow from $$$S_x+S_{|t|}−S_{x+|t|+1} \leq M$$$? It only follows if $$$M \leq 0$$$.

The conclusion, and of course, the solution is correct. But induction only works if $$$M \leq 0$$$. I think the author wanted to prove that $$$F_{|s|} \leq S_{|s|}$$$ when $$$M \leq 0$$$. In that case, a modified (and simplified) induction proof works. Otherwise if $$$M \gt 0$$$, we don't need to prove anything because we already have a working solution.

  • »
    »
    13 days ago, hide # ^ |
    Rev. 3  
    Vote: I like it 0 Vote: I do not like it

    My reasoning is that in our strategy, we should reset (do a 0 and go back to the bottom) exactly once.

    Proof: let's say we reset at point A and point B. If resetting at point A doesn't immediately win the game already, then at point B, our value will be <= the opponent's value (or else we have already won). So if we just don't reset at A, then we will be at our opponent's value at point B, and thus resetting at only point B gets us the same or better result.

    Then for implementation basically we brute force on our second half (after resetting): take 1, 1/2, 1/2/3, 1/2/3/.../m, and use two pointers to look for any section where we can beat them. I chose to say that taking each index actually gives 0 and resetting subtracts d. So then I just have to find a window in the original array where the sum of r[i] is less than the sum of our second half.

»
9 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

C is beautiful nice contest

»
31 hour(s) ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Bonus questions for d can be solved via sliding window , right?