cry's blog

By cry, 5 weeks 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 - A Gift From Orangutan

Problem Credits: vgoofficial
Analysis: vgoofficial

Solution
Code (Python)

2030B - Minimise Oneness

Problem Credits: vgoofficial
Analysis: vgoofficial

Solution
Code (C++)

2030C - A TRUE Battle

Problem Credits: vgoofficial
Analysis: vgoofficial

Solution
Code (C++)

2030D - QED's Favorite Permutation

Problem Credits: vgoofficial, cry
Analysis: cry

Solution
Code (C++)

2030E - MEXimize the Score

Problem Credits: vgoofficial, cry, satyam343
Analysis: cry

Solution
Code (C++)

2030F - Orangutan Approved Subarrays

Problem Credits: vgoofficial, satyam343
Analysis: vgoofficial

Solution
Code (C++)

2030G1 - The Destruction of the Universe (Easy Version) and 2030G2 - The Destruction of the Universe (Hard Version)

Problem Credits: vgoofficial, 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

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

Thanks to the authors for the interesting tasks :D

»
5 weeks ago, # |
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 :(

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

Fast solution Updated! Thanks

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

StringForces

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

Can you please not frozen the standings?

I want to read others' submission for problem G NOW as the editorial is not published yet.

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

    System Testing may have begun, it gets completed in ~1 hour or lesser depending on the pretests accepted submissions.

    Though submissions will be available now, you can use the status tab as soon as a contest finishes to view and filter other submissions.

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

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

»
5 weeks ago, # |
  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?

  • »
    »
    5 weeks ago, # ^ |
    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://codeforces.me/contest/2030/submission/286799491

    • »
      »
      »
      5 weeks ago, # ^ |
        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] > 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.

      • »
        »
        »
        »
        5 weeks ago, # ^ |
          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.

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

          Yeah you're right, this works since it is a permutation. In this case, $$$pref[i]$$$ is always greater or equal to $$$i$$$

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

        This is a nice observation that makes the code more simple. If pref[i] > i, it means there's a number missing from 1 to $$$i$$$. Then you can use the prefix array to do all necessary checks while updating the input string. I wish I'd have thought of it during the contest.

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

      Elegant solution.

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

      I cannot see your solution. I am seeing N/A in the code. Any reason for this.

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

      how did you come up with this idea? did you solve such a question before if so do let me know.

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
        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

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

    I tried doing it using segment tree and set, here is my solution, 287045819

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

Thanks for the contest and fast editorials ༼ つ ◕_◕ ༽つ

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

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

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

    u don't even need prefix-sum. You can use prefix max and suffix min. That should be sufficient I guess.

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

      How would that work?

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

        check my comment above

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

        if there is someone, who is smaller than current index on its right side, means, you can't have 'LR' ( boundary at index 'i' ) .

        Same goes for left side.

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

          Oh I actually did something different. I did prefix sums, but instead of maintaining a set, I maintained the sum, and checked if the sum was $$$0$$$. Your solution is good too.

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

      Even prefix max is enough, my solution

»
5 weeks ago, # |
  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 :(

»
5 weeks ago, # |
  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

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

    the thing that caught my eye was the D'''''''' lmao

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

Started happily with A and B. C crashed and burnt for me. couldn't figure out :(.

»
5 weeks ago, # |
  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

»
5 weeks ago, # |
  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.

  • »
    »
    5 weeks ago, # ^ |
      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.

  • »
    »
    5 weeks ago, # ^ |
      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

  • »
    »
    5 weeks ago, # ^ |
      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" )

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

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

»
5 weeks ago, # |
  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)

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

    Well well well, the author looks familiar as well... :)

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

      our supreme problemsetter went ahead and took the chance to steal the statement from HIMSELF

»
5 weeks ago, # |
  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

  • »
    »
    5 weeks ago, # ^ |
      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

    • »
      »
      »
      5 weeks ago, # ^ |
        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!

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

let f(t) be the number of non-empty subsequences† of t that contain only 0

should be 0's

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

Nice contest! I just fumbled in D problem.

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

thanks for fast editorial

»
5 weeks ago, # |
  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

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

I wish I could have cracked B this time thought it was a nice experience as it was my first contest solved A and almost solved B and C will improve!

»
5 weeks ago, # |
  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.

»
5 weeks ago, # |
  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.

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

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

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

      I just got it as soon as I posted the comment. My bad. It also fined me half of an hour during contest.

      Thanks for your reply.

  • »
    »
    5 weeks ago, # ^ |
      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.

    • »
      »
      »
      5 weeks ago, # ^ |
        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

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

https://codeforces.me/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

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

    wats wrong with this approach

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

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

»
5 weeks ago, # |
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 ?
  • »
    »
    5 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    In C, not able to understand how alice wins in if there is 1 in end but odd number of strings. eg. 001: 0 or 0 and 1 -> 0

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

      They can place the Boolean operator anywhere if the place is not captured already, like Alice will first place the OR operator between 0 and 1, I did the same mistake like you thinking that they will place it sequentially

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

        Thanks @ritamiitism. This should have been highlighted in problem statement. :(

»
5 weeks ago, # |
  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!!

»
5 weeks ago, # |
  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

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

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

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

Could somebody please tell me why is my code for D incorrect at 4th test case? What am I missing?

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define fr(a, b) for (ll i = a; i < b; i++)
#define fa(a, b) for (ll i = a; i >= b; i--)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int tC;
    cin >> tC;
    while (tC--)
    {
        int n, q;
        cin >> n >> q;
        int a[n];
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        string s;
        cin >> s;
        while (q--)
        {
            int index;
            cin >> index;
            index--;
            if (s[index] == 'R')
                s[index] = 'L';
            else
                s[index] = 'R';
            int ans = 1;
            int k=0;
            for (int i = 0; i <= n - 1; i++)
            { 
                if (a[i] > i + 1)
                {
                    k= max(k,a[i] - 1);
                    while (i < k && s[i] == 'R')
                    {
                        i++;
                        k = max(k, a[i] - 1);
                    }
                    if(i ==k)
                    i--;
                    else if (i < k)
                    {
                        while (i <= k)
                        {
                            if (s[i] == 'R')
                            {
                                ans = 0;
                                break;
                            }
                            i++;
                            if(i <=k)
                            k = max(k, a[i] - 1);
                        }
                    }
                }
            }
            if (ans)
            {   
                int k=1e7;
                for (int i = n-1; i >=0; i--)
                {
                    if (a[i] < i + 1)
                    {
                        k = min(a[i] - 1,k);
                        while (i > k && s[i] == 'L')
                        {
                            k = min(k, a[i] - 1);
                            i--;
                        }
                        if(i == k){
                            i++;
                        }
                        else if (i > k)
                        {
                            while (i >= k)
                            {
                                if (s[i] == 'L')
                                {
                                    ans = 0;
                                    break;
                                }
                                i--;
                                if(i>=k)
                                k = min(k, a[i] - 1);
                            }
                        }
                    }
                }
            }
            if (ans)
            {
                cout << "YES" << endl;
            }
            else
                cout << "NO" << endl;
        }
    }
    return 0;
}
»
5 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

.

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

I don't know if it's because of my rank but I think you need some work on the statements like problem C for example

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

    I think C was pretty clear, but that clarification for B was important. I personally believe it should've been included in the statement

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

      I was think that you must move from left to right I didn't get that it isn't work like that but the problem was very easy after that

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

        Yeah true actually, that part is not clear from the statement. It's ambiguous

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

Love the round timeline! It's really fascinating to learn how contests are prepared, and how much time and efforts it takes. Thank you :)

»
5 weeks ago, # |
  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! ^~^

»
5 weeks ago, # |
  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]?

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

Hope everything will be better

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

In the problem B, I thought Alice and Bob can place the boolean operator from starting of the string sequentially. But now I realised after seeing the solution that they could place it anywhere. That's why it was giving wrong answer on test 2. The problem should have been mentioned it correctly, even the testcases and explanation was like sequential...

»
5 weeks ago, # |
  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.

»
5 weeks ago, # |
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

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

    can you explain your approach ?

    • »
      »
      »
      5 weeks ago, # ^ |
      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.

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

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

»
5 weeks ago, # |
  Vote: I like it -8 Vote: I do not like it

Those who see my profile picture are handsome mans,haha

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

Thanks authors for the contest.

»
5 weeks ago, # |
  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?

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

    in dp[i][j] you are guaranteed to increase the score by j using the occurences of ith element.

    Each occurence of element i increases the score by 1

    you are multiplying 2^(suffix sum) because you are calculating the contribution of every i's in every subsequence.

    example case 1 : 0 1 2 case 2 : 0 1 2 4 40 400 case 3 : 0 1 2 3 4 5 6 7

    they might have different mex's we dont care , what we do care about is the individual contribution.

    if any one feels like this is wrong feel free to rectify me :)

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

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

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

I didn't cheat the code style was popular to me and my friend he is my teammate so we use the same style by default, and we got it from our monitor

»
4 weeks ago, # |
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?

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

In problem E, in the 2nd case where $$$j \geq min(f_0, ..., f_{i-1})$$$, how can $$$j$$$ be greater than $$$min(f_0, ..., f_{i-1})$$$ when $$$j$$$ in $$$dp[i][j]$$$ itself is defined as $$$j=min(f_0, ..., f_i)$$$?

Moving from $$$i-1$$$ to $$$i$$$, prefix min either remains the same or decreases, right? But how can it increase?

cc: cry

»
4 weeks ago, # |
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