Boboge's blog

By Boboge, 2 months ago, In English

Thank you for participating! We hope you enjoyed the problems.

2237A - Destroying Towers

Idea: _ja Preparation: _ja & quacksaysduck

Tutorial
Implementation

2237B - Annoying the Ghost

Idea: quacksaysduck Preparation: _ja & quacksaysduck & Boboge

Tutorial
Implementation

2237C - Duck Surplus

Idea: quacksaysduck Preparation: Boboge

Tutorial
Implementation

2237D - Fullmetal Bitchemist

Idea: zeemanz Preparation: __baozii__

Tutorial
Implementation(python)

2237E - Permutation Commutation

Idea: _ja Preparation: Boboge

Tutorial
Implementation

2237F - Paint the Array

Idea: StarSilk Preparation: StarSilk & Sugar_fan

Tutorial
Implementation

2237G - Send GCDs

Idea: _ja Preparation: Boboge

Tutorial
Implementation

2237H - Slime and Queries

Idea: StarSilk Preparation: StarSilk & Sugar_fan

Tutorial
Implementation

2237I1 - DBFS Order (Easy Version)

Idea: StarSilk Preparation: StarSilk & Sugar_fan & Boboge

Tutorial

2237I2 - DBFS Order (Hard Version)

Idea: StarSilk Preparation: StarSilk & Sugar_fan & Boboge

Tutorial
Implementation
  • Vote: I like it
  • +88
  • Vote: I do not like it

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

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

»
2 months ago, hide # |
 
Vote: I like it +6 Vote: I do not like it

D absolutely fried me man... Im quite happy with my +16 tho!

»
2 months ago, hide # |
Rev. 2  
Vote: I like it +29 Vote: I do not like it
Alternate Solution to F
»
2 months ago, hide # |
 
Vote: I like it -50 Vote: I do not like it

.

»
2 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

I went to sleep early =(

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

in D, because our final value is either +1 or -1 we can deduce that the total sum is 3x+1 or 3x-1.

After this, noting that alternating strings don't work or that there has to be at least one pair of adjacent equal characters suffices. My implementation

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

    can you explain the part of ur code where you count the correct substrings ~~~~~ int r = n, ans = n; for(int i = n — 1; i > 0; i--) { if(s[i — 1] == s[i]) { while(r > i) { f[pref[r--]]++; } } int here = f[(pref[i — 1] + 1) % 3] + f[(((pref[i — 1] — 1) % 3) + 3) % 3]; ans += here; } ~~~~~

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

      I maintain a pointer r which stays at position i such that $$$ s_i = s_{i-1}$$$ and if I get to such i, I decrease my pointer to that point while updating the map. Finally, as I said above the valid sum is $$$ 3x \pm 1 $$$, I am just calculating those values in the variable here. $$$ 3x \pm 1 \equiv \pm 1 \mod 3 $$$

»
2 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

man i got cooked by B so bad

»
2 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

man i got cooked by D so badly

»
2 months ago, hide # |
 
Vote: I like it +14 Vote: I do not like it

D led to my -73 =(

  • »
    »
    2 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    For me it was a huge gap between C and D. A-C took <20 minutes, but was unable to solve D at all, even on next day. With so many problems and so much time for this contest, I wish there could be more solvable problems...

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Really enjoyed the contest And problem D is fire ,,, happy to solve it

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

Is problem B — 2237B - Annoying the Ghost significantly harder if the problem is to change from array $$$b$$$ to $$$a$$$, instead of $$$a$$$ to $$$b$$$?

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

    You're right! I still don't fully understand the solution yet. In the last contest, I only solved Problem A, and I still don't understand the solution for Problem B.

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

      You can check my submission it it simpler to understand i just thought greedily replacing a[i] if(a[i] > b[i]) with a[j] that <= b[i] by performing swaps should do the work because it is optimal to do so like if a1<b1 a2<b2 and a3<b3 it is always possible to make b[i] from a[i].

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

        thanks bro nice solution. i understand the problem bcz of your solution.i am sad i am stuck at this rating :(

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

B is extremely difficult... ...

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is there a way to solve D using DP, trying to store the count of beautiful substrings ending at 0 or 1, and also storing bad/alternating substrings, then adding them up for each index?

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

    Plenty of edge cases arise. I tried this approach over and over but was always able to think of a case where it fails.

  • »
    »
    2 months ago, hide # ^ |
     
    Vote: I like it +3 Vote: I do not like it

    Yes, there is a way for D using just DP and I did exactly that in the contest. But the issue is I used 7 DP arrays.

    Nevertheless, here is my solution.

    Let $$$dp_{x}[i]$$$ denote the number of substrings ending at i, such that it is non alternating and the difference of counts of $$$0$$$ and $$$1$$$ is $$$x \space mod \space 3$$$. ($$$x = 0,1,2$$$)

    Let $$$dp_{xy}[i]$$$ denote the number of substrings ending at i, such that they are alternating and they start at $$$x$$$ and end at $$$y$$$. ($$$xy = 00,01,10,11$$$)

    Then loop through the string and at each character try adding it to the previous substrings. Just keep in mind that a single character is considered an alternating substring.

    Here is my code for reference : 379415083

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

      Thank you for the solution. If you don't mind, can you please share how you arrived, or why you went in the direction of taking the difference of counts? Taking the difference of counts wasn't even remotely in my thinking space, when trying to solve the problem. I kept trying to find the count at each index by checking if adding a 0 or 1 to the previous subarrays will yield a beautiful one, and adding them with half the alternating subarrays.But I couldn't go in the direction of taking the counts, as I kept thinking count of each element won't matter since we're taking a subarray.

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

        try to set up an equation to just observe the initial and final (potential/target) states

        we have two kinds of operations (-2, +1) and (-1, +2) on the pair (n, m) representing the number of 1s and 0s. say we did x and y of each operation and reached the final state of a single number lets say '1' for now.

        n-2x + y = 1

        m-2y + x = 0

        n-m-1 = 3 * (x-y) , meaning n-m = 1 mod 3.

        similarly for reaching a final state of '0' we get n-m = 2 mod 3.

        This means we can only reach final state of a single number of we have n-m != 0 modulo 3.

        Setting up the equations and playing around made me stumble upon this

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

Sorry for the comment, I completely missed that given b is sorted.

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

it makes no sense that people with O(n^2) are passing B, those testcases are way too weak

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

not relevant to the solution of D but in D, is it true that for any binary string, if it is beautiful then it reduces to a unique value? Intuitively feels like that should be the case, but not sure how to prove it.

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

    Yes thats true(if I understood your question correctly) and here is my proof:

    Lets say $$$f^n(0)$$$ is the quantity of $$$0$$$ in some string $$$s$$$ after $$$n$$$ operations. The same with $$$f^n(1)$$$. It is easy to see that $$$f^{n-1}(1)-f^{n-1}(0) \equiv f^n(1)-f^n(0) \pmod3$$$(because -1-2 is 0 and 2-(-1) is also 0 by mod 3). So, we are assuming that string is beautiful so lets say that after $$$k$$$ operations we will end up with one symbol(WLOG lets say it is 1). So: $$$f^0(1)-f^0(0) \equiv f^1(1)-f^1(0) \equiv ... \equiv f^{k}(1)-f^{k}(0) \pmod3$$$

    But we know that since we only have one 1 and zero 0 at the end after $$$k$$$ operations, $$$f^{k}(1)-f^{k}(0)=1 \pmod3$$$

    If there was some combination where we can reach only 0 at the end after some $$$m$$$ operations, then with the same logic $$$f^0(1)-f^0(0) \equiv f^1(1)-f^1(0) \equiv ... \equiv f^{m}(1)-f^{m}(0) \equiv 0 -1 \equiv 2 \pmod3$$$

    So we have $$$1 \equiv f^0(1)-f^0(0) \equiv 2 \pmod3$$$ which is not true

    In less formal terms: Every single move you make changes the value of (count of 1s) — (count of 0s) by either $$$+3$$$ or $$$-3$$$. This means the remainder of this difference when divided by 3 never changes. Since a final 1 leaves a remainder of 1, and a final 0 leaves a remainder of 2, a string can never reduce to both. The final symbol is strictly predetermined from the very beginning.

    Feel free to ask if anything is unclear

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

Heyy

idk if i have enough knowledge regarding this but idk i noticed some of my known friends whom i know personally solving D in like 15 mins who are new to CF like me. I dont wanna accuse someone but i got suspicious and after contest tried GPT and it gave the correct passable code logic within like 1 min.

Can something be done because i was bummed out seeing 4k submissions on D during contest(though i know here div 1 ppl were there too this time) . i do agree that most would have independently solved it and me finding it tough isnt a benchmark for saying that others cant do it quickly but just in case can any measures be taken??

»
2 months ago, hide # |
 
Vote: I like it -8 Vote: I do not like it

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

»
2 months ago, hide # |
 
Vote: I like it +10 Vote: I do not like it

Oh boy, I don't remember having so many alternative solutions

Problem C
Problem F
Problem G
  • »
    »
    2 months ago, hide # ^ |
     
    Vote: I like it +36 Vote: I do not like it

    For the record, I think it's very cool and I generally liked the problems as well, so thank you for the contest! (if only it had been on a weekend though)

»
2 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

1k+ people solved E (╥﹏╥)

»
2 months ago, hide # |
 
Vote: I like it -26 Vote: I do not like it

E isn't hard but code is too long

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

How can I compute inversions in O(N log N) in problem B?

»
2 months ago, hide # |
 
Vote: I like it +21 Vote: I do not like it

When testing the round, I found some optimizations that can be done on problem G to reduce the array length to $$$\left \lceil \frac{10n}{9} \right \rceil + 55$$$ and query count to $$$55n$$$, I think it's a pretty cool challenge to think about. Here's my solution:

Spoiler
»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I only accepted problem A,so I think I don't have enough programming level to take part in this contest.

»
2 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

D is such a cool problem.

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

i dont get editorial of B i see if we map a1 to bj instead of bi < bj and say some other element am comes for bi then then am <= bi < bj which shows that am is also a valid candidate for bj so a better move is to do a1 for bi (as it reduces the cost)

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

waw , Mate, D really gave me a proper thrashing

DAMN , although C is shit about D lol

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Who agrees with me that B is harder than C and D?

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Bro why does that G solution look so cracked, like how would you even come up with that.

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

    hmm let's create runtwice with gcd.

    $$$2n$$$ solution is clear.

    this idea with subsets of primes is a bit harder but also clear.

    let's merge those two!

  • »
    »
    2 months ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    It's much easier to come up with the idea if you try to enumerate solutions according to the constraints, as 150 points to first 150 primes and 10/9 leads to 9 elements in a row using 10 numbers to encode, then you can realize that 3e5^10 > 1e6^9 and figure out the solution

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

B was cool :)

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

    Hello brother can u please help me understand why each a[i] should be increased to smallest b[j] greater than a[i] ? After a lot of try, still this couldn't make sense to me.

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

      Because assigning each a[i] to the smallest available b[j] with b[j] >= a[i] leaves the larger b values for larger a's that may not fit anywhere else, so it never reduces the set of feasible future assignments and avoids blocking a valid matching.

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

guys why didnt my score change????????

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

D and E are great, almost among the best problems I've encountered on this platform!

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

B is such a cool problem

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

    Hello brother can u please help me understand why each a[i] should be increased to smallest b[j] greater than a[i] ? After a lot of try, still this couldn't make sense to me.

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Could solve 3 in this div1+2 as well

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Did anyone solve D with DFA? It was nice building a DFA to solve an easy problem that could be solved with simpler observations. :V

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Does anyone have a simpler explanation for E? I didn't understand what the editorial means by "cycles of a".

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

    same that statement was not obvious at all heres a explaination/proof

    Every permutation can be broken into disjoint "cycles". If you think of the permutation like a functions that maps (1-indexed) $$$i$$$ to $$$a_i$$$, then if you apply the permutation to itself, elements of each cycle will trade places with eachother.

    For example the permutation 2 1 6 3 5 4 can be broken into the cycles (1, 2) (3 4 6) and (5) because 1 maps to 2 and 2 maps to 1, 5 maps to 5, etc.

    The problem says if permutations $$$A$$$ and $$$B$$$ commute, than $$$A(B(i)) = B(A(i))$$$ for all $$$i$$$. if some element $$$j$$$ is in a cycle of length $$$n$$$ in $$$A$$$ then

    $$$A^n(j) = j$$$
    $$$B(A^n(j)) = B(j)$$$
    $$$B(j) = A(B(A^{n-1}(j)) = A^2(B(A^{n-2}(j)) = \dots = A^n(B(j))$$$

    $. therefore if $$$j$$$ is in a cycle of length n, then $$$B_j$$$ is also in a cycle of length n

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

For problem B, Can anyone please explain why each a[i] should be increased to smallest b[j] greater than a[i] ? After a lot of try, still this couldn't make sense to me.

»
2 months ago, hide # |
 
Vote: I like it +50 Vote: I do not like it

Thanks for the contest! In case it's helpful to others, I thought I'd write down some thoughts on my solution to G and its motivation.

A natural first strategy to attempt is to write down the array $$$a$$$, then write down some other numbers that can be used alongside the GCD operation to decode it. One problem with any strategy of this shape, though, comes from handling inputs consisting of large primes. If the array $$$a$$$ consists of $$$n$$$ large primes, then each of these primes must appear twice in $$$b$$$ for it to be a possible output of the GCD operation, so for such an input, $$$b$$$ would require at least $$$2n$$$ elements, which is infeasible.

This indicates that rather than writing down $$$a$$$ itself, we'll need to encode its elements in some more convenient way, then decode them later. That motivates thinking about what numbers are easy to identify in $$$b$$$ using the provided GCD elements. One natural idea here is that if we fix an element of $$$b$$$ to be the largest power of some prime $$$p$$$ less than $$$10^6$$$, we can take the GCD of any other element of $$$b$$$ with this one to count the times $$$p$$$ appears in its prime factorization. So, if we use powers of the 150 smallest primes as the 'spare' elements of $$$b$$$, we can use them to compute the exact values of all other elements of $$$b$$$, provided those elements don't have any prime factors outside of these 150 smallest primes.

By writing a brute force, we can find that there are just over $$$3 \cdot 10^5$$$ numbers up to $$$10^6$$$ that have sufficiently small prime factors and can thus be identified exactly if we put them in $$$b$$$. Say the exact count of such values is $$$k$$$. By writing down a map from these $$$k$$$ values to the first $$$k$$$ nonnegative integers, we can imagine that instead of using the GCD queries described in the problem, we're allowed to write down $$$\frac{10n}{9}$$$ values up to $$$k$$$ in $$$b$$$, and the second player receives those exact numbers and needs to restore $$$a$$$.

A reasonable starting point from here is to write down $$$a_i \bmod k$$$ for all $$$i$$$. Then, for every nine elements of $$$a$$$, we can add one extra element to $$$b$$$, and we need to use this element to restore the values of $$$\left\lfloor \frac{a_i}{k} \right\rfloor.$$$ But given $$$k \approx 3 \cdot 10^5$$$, this quotient has to be between $$$0$$$ and $$$3$$$, so there are $$$4^9$$$ possible sequences of quotients for a series of nine elements of $$$a$$$. Since $$$4^9 \lt k$$$, we can encode all nine of these quotients in a single element of $$$b$$$, e.g. by writing down a nine-digit number in base four. This is enough to solve the reduced problem, so we're done!

A mildly amusing aside: I was able to overkill F using segment tree beats (the first time I've actually used segtree beats to implement a problem), which saved me from thinking harder about how to use prefix minima/maxima to optimize the DP transitions :)

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

    I have another idea:

    We will encode $$$9$$$ consecutive numbers in the array $$$a$$$ to $$$10$$$ consecutive numbers in the array $$$b$$$.

    Let $$$base = 10^6 + 1$$$, and define $$$c_i = a_i * base^0 + a_{i + 1} * base^1 + ... + a_{i + 8} * base^8$$$ and we can change every $$$c_i$$$ $$$(i = 9 * x + 1$$$, there are $$$\lceil n/9 \rceil$$$ such $$$c_i)$$$ to other form, here I chose base $$$252000$$$ (we need a base of at least $$$\sqrt[10]{base^9} \approx 251189$$$ so it fits in $$$10$$$ digits).

    I chose $$$252000$$$ representatives of the form $$$2^x * 3^y * p_1 * p_2 * ...$$$ (I tried $$$2^x * p_1 * p_2 * ...$$$ and $$$p_1 * p_2 * ...$$$ but they don't have enough numbers less than $$$10^6$$$ to represent base $$$252000$$$) with $$$p_i$$$s are the first $$$140$$$ primes greater than $$$3$$$ and they have to be pairwise distinct. I'm pretty sure $$$140$$$ is somewhat large, it can be reduced further but it's not necessary because we only used $$$k = \lceil 10n/9 \rceil + 142$$$.

    We transformed a 9-digit number in base $$$10^6 + 1$$$ to a 10-digit number in base $$$252000$$$, Quack can just get the array $$$b$$$ in $$$\lceil 10n/9 \rceil * 140$$$ queries and decode to $$$a$$$ easily.

    I almost got that during the contest, but my base conversion code was stupidly bugged :( looking forward to see more interactive/communication problems soon.

    Implementation: 379431426

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

the prove of greedy problem on codeforces is terrible

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

Nice problems

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Is there a typo in the tutorial for B? It isn’t necessarily true that a_m <= a_1, right?

»
2 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Can anyone explain 2237C plz

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi in c if we remove constraint of adjacent and swap any two position according to rule. can anyone suggest the way to the modified problem.

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Rating problem D at 1500 is insane i guess, man that problem is tough.

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

d why the number of odd substrings for aletranting string is ⌊y−1/2] ?))

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

Problem I is a really great problem! The DP part is super detail-oriented. Fully digesting it really puts your understanding and implementation skills to the test.