cry's blog

By cry, 18 months ago, In English

Below is a timeline of the changes made to the round from start to finish. I hope this can depict what setting a contest is actually like for aspiring problemsetters. Please give me feedback about this in the comments. What else about the round would you like to know? Was this helpful?

Round Timeline

Please check out this amazing animated video tutorial by one of our beloved testers redpanda!!!

2030A - Подарок от орангутана

Problem Credits: Proof_by_QED
Analysis: Proof_by_QED

Solution
Code (Python)

2030B - Минимизируйте единичность

Problem Credits: Proof_by_QED
Analysis: Proof_by_QED

Solution
Code (C++)

2030C - ИСТИННАЯ битва

Problem Credits: Proof_by_QED
Analysis: Proof_by_QED

Solution
Code (C++)

2030D - Любимая перестановка ЧТД

Problem Credits: Proof_by_QED, cry
Analysis: cry

Solution
Code (C++)

2030E - MEXимизируйте счёт

Problem Credits: Proof_by_QED, cry, satyam343
Analysis: cry

Solution
Code (C++)

2030F - Орангутано-одобренный массив

Problem Credits: Proof_by_QED, satyam343
Analysis: Proof_by_QED

Solution
Code (C++)

2030G1 - Уничтожение Вселенной (простая версия) and 2030G2 - Уничтожение Вселенной (сложная версия)

Problem Credits: Proof_by_QED, satyam343
Analysis: satyam343

Solution (Easy Version)
Solution (Hard Version)
G1 Code (C++)
G2 Code (C++)
  • Vote: I like it
  • +206
  • Vote: I do not like it

| Write comment?
»
18 months ago, hide # |
Rev. 2  
Vote: I like it +44 Vote: I do not like it

Thanks to the authors for the interesting tasks :D

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

Sad for F... I did every observations required to solve problem but i could not combine it :(

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

Fast solution Updated! Thanks

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

StringForces

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

The difficulty of problem F may be a little bit low.

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

D felt like I needed a data structure I don't know of. Any ideas?

  • »
    »
    18 months ago, hide # ^ |
    Rev. 4  
    Vote: I like it +11 Vote: I do not like it

    I don't get the editorial too much. Please look at my submission, it is more straightforward. All you need to so is make an array with the max from the front and min from the back. For the array, 1 4 2 5 3, mx is [1,4,4,5,5] and mn is [1,2,2,3,3]. For any index such that s[i] == 'L' && s[i+1] == 'R' it is enough the check if max[i] <= mn[i+1], if so, the index is not a problem. Store the number of problems. It is not necessary to store the indices. When you change an index to L from R or vice Versa, check the indexes around it to see if the bad count would be affected. If bad==0, SAY yes else say no. Submission: https://mirror.codeforces.com/contest/2030/submission/286799491

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

      I don't see why you use both a max prefix and a min suffix.
      Build first a max prefix array $$$pref$$$. As you said, you can iterate over $$$i$$$ and check if $$$s[i] == 'L' && s[i+1] == 'R'$$$. It is a potential block, since it will prevent any element with index below or equal to $$$i$$$ to go to a position with index over $$$i$$$ (and reciproquely).
      But it is only a problem if this prevents us to sort the array. This isn't a problem if all elements below index $$$i$$$ can be sorted independantely, i.e. there are values from $$$1$$$ to $$$i$$$. In this case, $$$pref[i] == i$$$. If $$$pref[i] \gt i$$$, then the block is active, and we count one conflict.

      Count the number of initial conflicts, and for each query, you can check locally if this resolves a conflict or create one.

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

        Ok i was confused at first but i think your solution relies on the fact that it is a permutation. To be honest, I completely missed the fact that it was a permutation hence my general solution, still I think my solution is easier to think about and also more general. I assume in your solution if pref[i]<i you do nothing since the conflict would be counted before? But the fact that I even have to think about that makes my solution a little better I think.

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

      Elegant solution.

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

    you can see that for any substring of S, that doesnt contain the substring LR in itself, that substring is sortable. thus it is sufficient to check whether the substrings of LR partition the string into parts that, when sorted make the entire array sorted. you can do that by constructing intervals where l=min(pos[i],i) and r=max(pos[i],i). what this means essentially is just an interval that starts where the number is and where its supposed to be(or vice versa). then another observation is that if 2 intervals intersect, then you can merge them and they can be considered one interval. next for each query you can just check whether this changes the amount of LRs in the entire string and if an LR dissapears then check if it previously "ruined" the array, or if an LR appears, check if the new LR "ruins" the array. sorry if this is unclear, but ask any questions, id be glad to explain further.

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

      i did something similar , to check if segment is sortable or no

      compare its sum [ l ,r ] it should be equal to [ l, r ] when the array is sorted ( we created a sorted permutation from( 1 , n )) to compare the segments

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

My solution to D is in $$$O(n + q)$$$, using prefix sums. https://mirror.codeforces.com/contest/2030/submission/286798401

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

f similar to https://open.kattis.com/problems/wiknow although still couldn't solve it :(

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

Can we just appreciate the Round Timeline :)

It was really insightful as to how problems are actually made and well the coordination skills of satyam343

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

can anyone help me find mistake in my code for D.My logic was same.. here is my submission 286813972

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

Another solution for D is to precalculate all intervals in the array that are permutations of their indices and process the queries with a segment tree. You can store the array intervals' ends in a set and, for each bad index LR in the string, you can consider it as two intervals: one ends at L and the other starts at R. For the permutation to be sortable, every string interval's end must also be an end in the set of indices we calculated for an array.

So the segment tree on the LR string can store nodes that contains the leftmost character on the range, the rightmost character on the range, and whether or not all the endings in the range are also in the set of array indices. If they are, then we are able to sort that range. The tree can be updated in $$$O(\log{n})$$$, and the answer is the root of the segment tree.

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

    This is probably the most leetcode submission idea. I had something similar. Takes forever to implement though.

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

    could u explain this part more ?

    and whether or not all the endings in the range are also in the set of array indices

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

    u dont need segment tree bcz ur check function ```if (left.right == 'L' && right.left == 'R') { if (ends.count(mid)) { seg_tree[node — 1] = {left.left, right.right, true}; } else { seg_tree[node — 1] = {left.left, right.right, false}; } } else { seg_tree[node — 1] = {left.left, right.right, true}; }

    ``` u are only checking when u are the next node from the leaf of segment tree ,

    u can check it manually on array ( 2 adjacent indicies who = "LR" )

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

    you could also try xor hashing. congrats on cm btw.

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

When you have seen the problem statement somewhere else...

(Of course, it's okay since the problem requirement is totally different)

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

Alternate solution to problem C: Notice that the substring "010" is bad, we just need to check if there is a '1' that is not inside "010" then the answer is YES, otherwise NO

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

    Can u please elaborate I had similar approach so I have check if 010 in string or 010 in start or 101 in end but got wa

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

      Basically, the idea for substring "010" is there are 2 options to choose from: "01" or "10".

      We know that the "and" operations will be evaluated first, so even if Alice moves before Bob, no matter which she chooses to do the "or" operation on, Bob can "counter-attack" that move by choosing the other one. Which means the substring "010" will always become "000".

      For any other cases where there is a '1', because Alice moves first, Alice can pair that '1' with whatever other digit that is next to it to do the "or" operation and get the result of '1'. Bob cannot "counter-attack" it, because he can not make that '1' become '0' before Alice if the substring is not "010", so Alice will have at least one '1' at the end. She will win.

      It's just my intuition and some deducing while solving the problem, hope it helps!

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

Nice contest! I just fumbled in D problem.

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

thanks for fast editorial

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

Great contest, and thank you for the fast editorial ! I was so close to finish D ... Next time I will

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

I think ignoring the fact that D is a permutation leads to a easier solution. Is this bad writing(still a great problem!) or was this intentional by the writers, just curious.

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

I can't get the proof of the given observation for problem E in the tutorial. Can anyone please explain it?

Let b = {0, 0, 1, 1}

f0 = 2, f1 = 2. Depending on the observation the score should be 4. But I can't get more than 3, doesn't matter how I perform the partition.

It is possible that I compiled the problem wrong. Hoping your help.

Thanks in advance.

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

    Partition into multisets {0, 1} and {0, 1}, score is 2 + 2 = 4.

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

    Wait. I think I get it. I am petitioning the array into subsegments during the whole contest. But It can be partitioned into subsequences.

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

      I also was thinking the same, partition word hints that we need to divide the array into subsegments. Not happy with the statement of Problem E

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

https://mirror.codeforces.com/contest/2030/submission/286828473

I thought a little different for problem D but idk where it's going wrong. The basic intuition is the string should be of type R*L* (RRR...LLL). So, I stored the indices of L and R in two separate sets. If highest(SetR)<lowest(SetL) is true, answer to that query will be True. And if the array starts in correct order (1,2,3..) I neglect those indices, same for ending (..n-2,n-1,n). I'm unable to understand why this approach isn't working?

Got the error, I'm dumb

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

cool contest, maybe C was a bit too easy though?

»
18 months ago, hide # |
Rev. 5  
Vote: I like it +7 Vote: I do not like it

How I solved A, B, C during the contest and D shortly after?
I would like any constructive feedback.

2030A - A Gift From Orangutan

My Thinking

2030B - Minimise Oneness

My Thinking

2030C - A TRUE Battle

My Thinking

2030D - QED's Favorite Permutation

How did I solve it ?
»
18 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

First 3 questions felt like speedForces but D was good, i could not do it. ps: thanks for the fast editorial!!

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

JUST SHARING MY THOUGHTS ON E


I felt E was quite hard enough. Instead of finding sum of all subsequences Mex, even if they had asked just number of different subsequences, such that their Mex is some given number(x)... that would have been good fit (IMO)

May be this could have been 2 part question,

1) first part: you have to compute number of ways,

2) second part: you have to compute the sum

cc : cry

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

Just reached master, thank you authors for these lovely problems.

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

My solution to D, using ordered sets in C++. I really liked the way this problem looked like a standard div3E/F exercise on data structures!

P. S. Back to blue with this contest! ^~^

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

Problem E

“Suppose we partition the elements of an array b into any number k of non-empty multisets S1,S2,…,Sk”

What does this mean? Can I partition [2,0,1,0,3] into [2,1,0,3] and [0]?

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

Another proof for problem B:

We can express $$$g(t)$$$ as the total number of subsequences, excluding those containing only zeros:

$$$g(t) = (2^n - 1) - (2^z - 1)$$$

where z is the number of zeros. So the absolute difference becomes:

$$$|g(t) - f(t)| = |(2^n - 1) - (2^z - 1) - (2^z - 1)| = |2^n - 2^{z+1} + 1|$$$

As we aim for the smallest difference, let's assume it's 0:

$$$|2^n - 2^{z+1} + 1| = 0$$$
$$$2^n + 1 = 2^{z+1}$$$
$$$z = \log_2(2^n + 1) - 1$$$

as we can see, z can’t be an integer in this case.

Let's check for 1:

$$$|g(t) - f(t)| = 1$$$
$$$|2^n - 2^{z+1} + 1| = 1$$$
$$$2^n = 2^{z+1}$$$
$$$z = n - 1$$$

Thus, constructing a string with n - 1 zeros is the best option.

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

Problem D can be done using xor hashing as well in O(n).

286885996

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

    can you explain your approach ?

    • »
      »
      »
      18 months ago, hide # ^ |
      Rev. 6  
      Vote: I like it +6 Vote: I do not like it

      As mentioned in the editorial, we can swap an element at position i to position j iff there is no LR b/w the positions. So we can divide the array into subarrays where the ith subarray ends at L and i + 1 th subarray starts at R.

      So when can we make the permutation non decreasing? If the set of indexes and the set of elements of each subarray are same. To check if two subarrays are equal we can use xor hashing.

      Implementation wise you can calculate a prefix array where prefix[i + 1] = pref[i] ^ hash[i] ^ hash[p[i]]. So when two subarrays are equal their prefix xor will be 0. So we can just maintain the good indexes where prefix xor is 0. So now we just need to check if the index where each subarray ends should be a good index i.e it's prefix xor should be zero.

      Let me know if any part is unclear.

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

    In case someone needs it, there's a nice tutorial about xor hashing.

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

Those who see my profile picture are handsome mans,haha

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

Can someone explain why doing 2(fi+1+⋯+fn−1) to find the dp score in problem E does not overcount? Like we would count all subsequences longer which we will just consider later? Also wouldn't each dp overcount as well? Like every dp[2][2] is also a dp[1][2] so we would overcount by a lot?

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

F can be solved using XOR hashing and Fenwick tree: submission link

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

Problem D

1 3 5 2 4

L L R L L

if i swap 5,2 ,then 5,4 ,then 3,2 , i can sort it ,but answer is NO

why?

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

Alternate solution for problem E:

We're going to calculate, for each value $$$i$$$ from $$$0$$$ to $$$n-1$$$, its contribution to the sum.

Let $$$f_i$$$ be the number of occurrences of value $$$i$$$. Following from the same observation as the editorial, value $$$i$$$ contributes $$$min(f_0, f_1, f_2 ... f_i)$$$ to the sum if the array is fixed. For short, let the result of that computation in the original array be $$$M_i$$$ and, in some subset, $$$m_i$$$. We want to compute for each value $$$i$$$ the sum of $$$m_i$$$ for every subset of $$$a$$$.

For the contribution of value $$$i$$$, the result of $$$m_i$$$ in each subset is not affected by the values greater than $$$i$$$ we choose. Let $$$S_i = \sum_{j=i+1}^{n-1}f_j$$$, that is, the sum of occurrences of values greater than $$$i$$$. We can compute the sum of $$$m_i$$$ for subsets less than $$$i$$$ and multiply that by $$$2^{S_i}$$$.

To compute the sum of $$$m_i$$$ over all subsets considering values less than $$$i$$$, we're going to calculate, for each possible value $$$x$$$ up to $$$M_i$$$, how many subsets exist such that $$$m_i = x$$$. Let $$$F(j,x) = \sum_{k=x}^{f_j}{{f_j}\choose{k}}$$$, that is, the amount of ways to choose at least $$$x$$$ elements from the amount of occurrences of $$$j$$$. The amount of subsets of values less than $$$i$$$ where $$$m_i = x$$$ is given by $$$G(i,x) = (\prod_{j=0}^iF(j,x)) - (\prod_{j=0}^iF(j,x+1))$$$, that is, the amount of subsets where the amount of occurrences of every value up to $$$i$$$ is at least $$$x$$$ minus the amount of subsets where the amount of occurrences of every value up to $$$i$$$ is at least $$$x+1$$$. From that, we know for each $$$x$$$ its contribution to the sum.

The answer to the problem can therefore be obtained by the following sum:

$$$\large{\sum_{i=0}^{n-1}2^{S_i}\cdot\sum_{x=1}^{M_i}{x \cdot G(i,x)}}$$$

These two outer sums amount to $$$\mathcal{O}(\sum_{i=0}^{n-1}f_i) = \mathcal{O}(n)$$$. However, the computation of $$$G(i,x)$$$ seems to make the overall complexity $$$\mathcal{O}(n^3)$$$ because of the computation of $$$F$$$ and $$$G$$$.

How can we compute G faster?
How can we compute F faster?

From all of this, we can calculate the answer to the problem in $$$\mathcal{O}(n)$$$ total.

Code: 288412616

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

in first test case 5 3 1 4 2 5 3 RLRLL 2 4 3

in q=3

we have-

R L L L L

1 4 2 5 3

now we can apply operations i this formm---

1 4 2 3 5 1 4 3 2 5 1 3 4 2 5 1 3 2 4 5 1 2 3 4 5

thus sorted then why NO??

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

Got a simpler solution for D:(https://mirror.codeforces.com/contest/2030/submission/326341437) just use MEX and done.

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

hello

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

I suggest that the meaning of "partition" in Problem E should be clarified. This word is very likely to cause misunderstanding and I wasted quite a lot of time because of it...

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

Problem F also we can solve with Merge Sort Tree and the same idea of nexts.