BledDest's blog

By BledDest, history, 41 hour(s) ago, translation, In English

2069A - Was there an Array?

Idea: BledDest

Tutorial
Solution (BledDest)

2069B - Set of Strangers

Idea: adedalic

Tutorial
Solution (adedalic)

2069C - Beautiful Sequence

Idea: BledDest

Tutorial
Solution (Neon)

2069D - Palindrome Shuffle

Idea: BledDest

Tutorial
Solution (Neon)

2069E - A, B, AB and BA

Idea: adedalic

Tutorial
Solution (adedalic)

2069F - Graph Inclusion

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +24
  • Vote: I do not like it

»
41 hour(s) ago, # |
Rev. 2   Vote: I like it +39 Vote: I do not like it
  • »
    »
    40 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    System testing is running right now.

  • »
    »
    39 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Oh

  • »
    »
    35 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    next year

  • »
    »
    34 hours ago, # ^ |
      Vote: I like it +18 Vote: I do not like it

    tbh, I would rather they spend longer doing anti-cheating measures than for rating to appear and then rolled back bc they found loads of cheaters

»
40 hours ago, # |
Rev. 2   Vote: I like it -41 Vote: I do not like it

A ~ E all have tag "greedy". I think it might be better to swap E and F so that programming skills can be improved as well.

  • »
    »
    39 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can you pls tell me how to do C using greedy? I scratched my head over C for $$$1.5$$$ hrs staright (cuz i do not know dp :/)

    • »
      »
      »
      39 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Actually you can find that a beautiful sequence in this condition shows like this:

      $$$1,2,2,2,\cdots,2,2,3$$$

      count the sequence. That's all.

      • »
        »
        »
        »
        38 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        define "count the sequence"?

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

          Count how many 1, 2, 2, 2, ..., 2 you have (or maybe just 1).

          You get an 1 : add 1;

          You get a 2 : multiply by 2;

          You get a 3 : add the number to the result.

    • »
      »
      »
      39 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      same I too got 7 TLE

    • »
      »
      »
      39 hours ago, # ^ |
        Vote: I like it +4 Vote: I do not like it

      You can see dynamic programming as splitting the problem into multiple states, each state representing the solution to a subproblem of the main problem. The splitting should be done in a way that solving one subproblem makes solving the next subproblem easy (this is called a transition).

      One of the usual way to split the problem is to cut the array you're given at some index and calculate the value for this shorter array. That way if you know the value for the subarray from index $$$1$$$ to $$$i$$$, it can be easy to calculate the value for the subarray from index $$$1$$$ to $$$i+1$$$.

      For problem C, you know the beautiful sequences look like $$$1, 2, 2, ..., 2, 2, 3$$$. One way to construct such a sequence is to start with a $$$1$$$, then add any number of $$$2$$$s you want and finally end with a $$$3$$$. This gives us three different states for our dp (called state $$$j$$$ in the editorial). Then there are three different transition for each index $$$i$$$ :

      • if $$$s_i$$$ is $$$1$$$ you have one more $$$1$$$ to start a sequence with, so $$$dp_{i+1,1} = dp_{i,1}+1$$$
      • if $$$s_i$$$ is $$$2$$$ you can add one more $$$2$$$ to one of the available sequences (from state 1 or state 2), you can also choose not to add a $$$2$$$, so $$$dp_{i+1,2} = 2*dp_{i,2}+dp_{i,1}$$$
      • if $$$s_i$$$ is $$$3$$$ then you can choose to end all sequences from state $$$2$$$, so $$$dp_{i+1,3} = dp_{i,3}+dp_{i,2}$$$.

      The number you're looking for is the count of finished sequences at index $$$n$$$ so $$$dp_{n,3}$$$.

      • »
        »
        »
        »
        35 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        can some one please help me understand the transition when si = 2, I didnt get

        • »
          »
          »
          »
          »
          34 hours ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          If $$$s_i=2$$$, then you want to find all ways of creating a sequence of the form $$$1, 2, 2, ...$$$. There are three possibilities :

          • First one is to have a sequence containing only $$$1$$$ and add $$$s_i$$$ to the sequence, this is $$$dp_{i,1}$$$ possibilities
          • Second one is to have a sequence of the form $$$1, 2, 2, ...$$$ and add $$$s_i$$$ to the sequence, this is $$$dp_{i,2}$$$ possibilities
          • Third one is to have a sequence of the form $$$1, 2, 2, ...$$$ and not add $$$s_i$$$ to the sequence, this is also $$$dp_{i,2}$$$ possibilities

          You add up all possibilities and you get $$$dp_{i+1,2}=2*dp_{i,2}+dp_{i,1}$$$

      • »
        »
        »
        »
        21 hour(s) ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I was just thinking how would we solve if all conditions were removed except the order that is 123,1123,12233,1233 ... Are allowed?

      • »
        »
        »
        »
        21 hour(s) ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Do you know some resources using which i could practice such dp on subsequences problems??

        • »
          »
          »
          »
          »
          10 hours ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          You can just filter problems with the 'dp' tag on codeforces. There are plenty of problems for every level if you want to pratice and learn dp

    • »
      »
      »
      38 hours ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Another way you can do it is like this: iterate from right to left, and store a current variable and when you

      • see a $$$2$$$, multiply your current variable by 2 because all the subsequences to the right can either contain the two or not.

      • see a $$$3$$$, add 1 to your current variable to start a new chain of subsequences ending at that $$$3$$$.

      • see a $$$1$$$, just add the current variable to your total result since everything else is accounted for.

      The only problem is that you also count the subsequences $$$1, 3$$$, so just whenever you encounter a $$$1$$$, subtract off all $$$3$$$ s that have appeared after it.

      • »
        »
        »
        »
        35 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        thanks, this was much more intuitive than the dp solution

  • »
    »
    39 hours ago, # ^ |
      Vote: I like it +27 Vote: I do not like it

    My experience on educational rounds is that the last problem is often easier than the second last problem if you have knowledge about many data structures/algorithms.

    • »
      »
      »
      39 hours ago, # ^ |
        Vote: I like it +13 Vote: I do not like it

      Thanks for your experience, I'll change my strategy in the future ECR.

»
40 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Awesome contest!!!

»
40 hours ago, # |
  Vote: I like it +11 Vote: I do not like it

F is way too standard for experienced coders(especially Chinese),shouldn't appear at this position.

  • »
    »
    39 hours ago, # ^ |
      Vote: I like it +16 Vote: I do not like it

    I would like to take this opportunity to ask,are there any standard problemsets that Chinese CP guys solve? :)

    • »
      »
      »
      30 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Go to luogu, I think quite a few Chinese competitive programmers solve problem sets on that

»
40 hours ago, # |
  Vote: I like it +25 Vote: I do not like it

Super slow editorial!

»
40 hours ago, # |
  Vote: I like it +14 Vote: I do not like it

Lighting slow editorial

But the problems were very interesting

»
39 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Why can't we use DSU with rollbacks together with paths compression?

  • »
    »
    38 hours ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    What is this method(DSU with rollbacks)? Resources please!!

    • »
      »
      »
      38 hours ago, # ^ |
      Rev. 2   Vote: I like it +16 Vote: I do not like it

      When using DSU you have two functions

      find(x) — find the leader of the set that contains $$$x$$$ (not really important for rollbacks)

      unite(x, y) — unite sets containing $$$x$$$ and $$$y$$$ respectively

      The thing with rollbacks is you want to undo the unite(x, y) operation.

      If you look at the following code for this function

      void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (size[x] < size[y]) swap(x, y);
        parent[y] = x;
        size[x] += size[y];
        components--;
      }
      

      you can see we do $$$O(1)$$$ changes (last three lines of function).

      We can save these changes in stack-like structure and when we need to rollback we can easily do it by reading the top tuple of the stack.

      Here we can just save pairs $$$(x, y)$$$ and do the following:

      void rollback(int x, int y) {
        parent[y] = y;
        size[x] -= size[y];
        components++;
      }
      

      More about this:

      https://cp-algorithms.com/data_structures/deleting_in_log_n.html

      https://usaco.guide/adv/offline-del?lang=cpp

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

        Thanks for detailed reply, but as I understood, the main thing is not in rollbacks structure. If we had paths compressions, it's anyway some changes in arrays size and par, so we can just push pairs {v, par[v]} and {v, sz[v]} on stack before operations and rollback them easily, even with path compression. I think in DCP offline case, as tfg mentioned, it`s redundant, because anyway after rollback we decompress paths, so it seems like we dont achieve alpha in asymptotics and it stays log n anyways.

  • »
    »
    38 hours ago, # ^ |
    Rev. 2   Vote: I like it +16 Vote: I do not like it

    You can but it won't change the complexity. As long as you still do union by rank or union by size.

    • »
      »
      »
      32 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      oh interesting, for some reason, I thought that path compression+by rank/size+rollbacks would be O(n) but I guess that was silly to think that

  • »
    »
    36 hours ago, # ^ |
      Vote: I like it +19 Vote: I do not like it

    Of course you can't.

    If only path compression is implemented (without union by rank), there exists a possibility of generating a chain of length O(n).

    Although amortized analysis suggests that one only needs to pay a single $$$O(n)$$$ cost for path compression, after which all subsequent queries achieve $$$O(1)$$$ time complexity,

    the existence of rollback operations fundamentally breaks this guarantee! A single rollback operation could nullify the effects of previous path compressions.

    This occurs because amortized complexity and persistence are inherently conflicting concepts—persistence invalidates the assumptions of amortized analysis.

    For similar reasons, data structures like the Splay Tree cannot be made persistent either.

»
38 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain the dp transition equation of problem C.

»
38 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

I think in question B, it's easy to misunderstand. That is, select a cell that wants to meet no connections around it

»
38 hours ago, # |
  Vote: I like it +7 Vote: I do not like it

For problem D, I have an alternate solution for the 2nd half after you cast out the prefix and suffix of the original string. When a string is palindrome, for n/2 first character, the number of each character is equal to half of the total. For example, in the string "abccccccba", there are 2/2 = 1 'a', 2/2 = 1 'b', 6/2 = 3 'c'

So I will run from 1 to n/2, and whenever the number of each character exceeds half of the total, for example position i, it is sure that I will have to reshuffle [i, n], thus reshuffle n — i + 1 character. Then, I do a similar run from n back to n/2 + 1 to find the position i such that I have to reshuffle [1, i]

However, if I can't find such position i (for example "acbddbac"), I know I have to reshuffle at most n/2 character. To optimize the answer, I found out you don't have to reshuffle the center if it is a palindrome (I mean, in the string "acbddbac", the center "bddb" is already a palindrome, and I shouldn't reshuffle it but reshuffle the prefix "ac").

So, it's my O(n) solution, you can see my code here: https://codeforces.net/contest/2069/submission/306731691. Sorry for my bad English

»
34 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Any solution of C without dp?

  • »
    »
    33 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    I realized my code is basically the same thing as all the dp code, but I wouldn't have called it dp. I guess the dp is so simple the line is a bit blurred.
                long ways = 0;
                int ones = 0;
                int twos = 0;
     
                long totalWays = 0;
     
                for(int j = 0; j<n; j++){
                    int val = arr[j];
                    if(val == 1){
                        ones++;
                    }
                    if(val == 2){
                        ways = ways*2 + ones;
                        ways = ways % 998244353;
                    }
                    if(val == 3){
                        totalWays += ways;
                        totalWays = totalWays % 998244353;
                    }
                }
     
                out.println(totalWays);
    
    • »
      »
      »
      20 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Can you please explain me the part where you are calculating the "ways"?

      • »
        »
        »
        »
        18 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Number of ways to make an array with the first element a 1 and all other elements a 2, using any elements up to the current index.

        • »
          »
          »
          »
          »
          18 hours ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          But for that I was thinking counting the no of 2's (let's say X) and whenever I encounter a 3, I will just add 2^X — 1. But I couldn't understand ur formula "ways = 2 * ways + ones". All the top participants have done the same thing. Am I missing something?

          • »
            »
            »
            »
            »
            »
            17 hours ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Your idea would work, if there was exactly one 1 at the start of the array and no 1s anywhere else.

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

            I actually tried something similiar, and it tled :( The reason is definitely obvious. 306989184

  • »
    »
    33 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yes ofcourse by precomputing powers of two and using a little maths , you can solve this without dp also , Here below is one way to do so (i don't quite know how to implement DP so i use maths as a bonus tool for myself):-

    306917242

    PLS UPVOTe...

  • »
    »
    33 hours ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    For a pair of $$$1$$$ and $$$3$$$ such that position of $$$1$$$ < position of $$$3$$$, let $$$x$$$ be the number of $$$2s$$$ between them, so the pair $$$1$$$ and $$$3$$$ contributes with $$$2^x - 1$$$ to the answer. For now, forget about the $$$-1$$$ and let the contribution be just $$$2^x$$$

    Let's iterate on the array from left to right and calculate the total contribution for each $$$3$$$ to the answer.

    Let $$$suf_i$$$ is the number of $$$2s$$$ in the suffix $$$i$$$, and $$$p(i,j)$$$ is the number of $$$2s$$$ between $$$i$$$ and $$$j$$$

    So $$$P(i,j) = suf_i - suf_j$$$

    Let the current $$$3$$$ we are calculation is at position $$$j$$$, then its contribution is $$$\sum_{i = 1, a_i = 1}^{j-1} 2^{p(i,j)} = \sum_{i = 1, a_i = 1}^{j-1} 2^{suf_i - suf_j}$$$

    Since $$$suf_j$$$ is constant for each $$$i$$$ so we can take it as common factor and the contribution will be $$$\frac{\sum_{i = 1, a_i = 1}^{j-1} 2^{suf_i}} {2^{suf_j}}$$$

    The term $$$\sum_{i = 1, a_i = 1}^{j-1} 2^{suf_i}$$$ can be updated on the fly in the loop.

    And finally, Let $$$rem$$$ be the number of pairs ($$$i$$$,$$$j$$$) such that $$$i$$$ < $$$j$$$, $$$a_i = 1$$$, $$$a_j = 3$$$, so that we can compensate all the $$$-1$$$ that we have ignored

    Check my code here: https://codeforces.net/contest/2069/submission/306743594

    • »
      »
      »
      33 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Kind of similar to what i have done, actually the same , I feel like doing maths that dp, because i do not know much of it , Any suggestions on where to learn it?? like some resources ?PG_Mazen

    • »
      »
      »
      32 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      hey i have done similar, but my code gives wrong answer and overflows , could you please help me out ?306751479

      • »
        »
        »
        »
        32 hours ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I think that the problem is in this part: tmp=(tmp%mod-cnt%mod)%mod;

        It should be: tmp=(tmp%mod-cnt%mod+mod)%mod;

»
33 hours ago, # |
  Vote: I like it +11 Vote: I do not like it

Is it this usual to not get delta updated in the rating for the educational rounds or is it this time that taking too much time to get updated? Anyone , is it me or everyone?

  • »
    »
    32 hours ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Taking too long. Something wrong with the round ig because many reds in official standings when it's supposed to be unrated for Div 1.

    • »
      »
      »
      32 hours ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It's normal that div1 participants appear in the official standings (check out past Edu contests). Unrated participants and unofficial participants are different concepts.

      • »
        »
        »
        »
        32 hours ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        What's the difference btw? In the last edu round there was a difference of 500 in my official and unofficial standings ranks this times it's about 50.

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

          In Edu rounds, almost all people are official participants -- I think just virtual participants are excluded as unofficial participants. (So your rank in unofficial standings may change over time.)

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

My solution for problem is different from the editorial : 306925674

If we make the given string palindrome, then it must have equal no. of characters in the first half and the second half, so solution goes as follows, for every character we find the freq. of them in first and second half and then equalize accordingly.
while equalizing, we can keep track of min index and max index in between we have to shuffle all characters. (this is done after removing already equal characters from start and end)
T.C. -> O(26*n) and can be easily done in O(n) but the current T.C. is fast enough to pass
»
21 hour(s) ago, # |
  Vote: I like it +11 Vote: I do not like it

Really great editorial on D.

Here I would like to discuss an alternative approach which is a bit more bland I guess but which I found easier to implement. (Read the editorial answer if you already haven't, it's really great.)

Basically after reducing the substring as discussed in the editorial, the answer would be a prefix or suffix. Let's say its a prefix. Then, there are two cases:

  1. The required length is <=n/2. In this case, the right half of the string decides the left half, and hence, the length of the prefix we need to shuffle is the position of the last character, where the character does not match the corresponding character of the palindrome determined by the right half. If that length is <=n/2 we have our answer.

  2. The required length is >n/2. In this case, the only necessary and sufficient condition is that the frequency of elements in the left half is >= that in the right half, and their parity is same (as the length of the string is even, and equal number of characters were removed from start and finish, so length of the reduced string is also even). This is because if the frequency is less, then the left half won't have sufficient characters to fill in corresponding places with the right half to form the palindrome. If it is more, then we can always arrange the characters with some characters in both left and the occupied right half in the prefix (this is always consistent as sum of character frequency is required length (fixed for fixed length).) The parity ensures that all characters have even final frequency, which is necessary for even length palindrome. We can perform this check using prefixes in (26*n) time.

We can repeat the similar process once again by reversing the string so as to get the shortest length for the suffix case also. Overall it's complexity is (26*n) which works pretty well under the given constraints.

Solution Link