GMEDIAN - Editorial(Unofficial)

I derived the vandermonde’s identity to get this done and still couldnt do it because
“”“Bonus: Calculating queries of the type nCr % 1000000007 can be solved by precalculating upto n = whatever you need :stuck_out_tongue: . Details of this can be found here: nCr % p in O(1)
“””

1 Like

You can use DP to find number of sequences (of all lengths) which start and end at a particular element.Then finding number of sequences which use that element for even and odd length becomes easy .CodeChef: Practical coding for everyone

Testcases are weak. I solved it in O(N^4)

Lol I wrote a brute O(N ^ 3) for each test then optimized it by precalculating an array in O(1000 ^ 3), then each test only takes O(N ^ 2). Only takes 0.3 secs. (Of course, that other thing was just 1000 ^ 3 / 6, which obviously will pass)

This is my solution which only got 5 points,I did everything right, then why I did not get 30 points? I used the Fermat Modulo Theorem to calculate (n C r) % p
:-
https://www.codechef.com/viewsolution/21540233

My approach same as editorial , but precompte nCr using dp and i think it’s quite easy.
This is my code:
CodeChef: Practical coding for everyone

Using a proof of Vandermonde’s identity to illustrate how to pick all the balanced enclosing sequences around a selected middle pair:

Suppose the chosen two central elements are at positions in the sorted array with s elements before the first one and t elements after the second. Then consider any selection of the same number of elements from each side, say j elements from each side.

Now invert the selection in the “before” section, so a previously selected item becomes unselected and vice versa. This now gives s{-}j elements on that side. Combining this with the “after” section, you have a total of (s{-}j) + j = s elements from the total pool, no matter what j is.

You can easily see that every balanced selection can be turned into a corresponding choice of s elements in this way, and vice versa - every choice of s elements can be turned into a distinct balanced enclosing choice. So the number of options for any one central pair is \binom{s+t}{s}.

example:

a b c d e f g h i j k l m n

chosen central pair h & j (s=7, t=4)

a b c d e f g X - X k l m n

choose say three from each side:

. b c . e . . X - X k l m .

invert the "before"s:

a . . d . f g X - X k l m .

and we have a choice of exactly 7 elements.

And any choice of 7 elements from the 7+4=11 available can produce a unique balanced set:

a . c d e f . X - X k l . .

invert the "before"s:

. b . . . . g X - X k l . .

giving 2 each side.

If you have a run of identical elements (more than two), you can make the summation a little more efficient too.

My code.

2 Likes

(m+n)C(min(m,n)) is the same as {m+n \choose m}={m+n \choose n} and it can be derived directly, without using Vandermonde’s identity.

We need to get the number of all combinations from m+n elements, m of which are on the left side and n - on the right side, that have equal numbers of elements on each side. Lets replace the left-side part of each combination with its complement - the set of elements on the left side that don’t belong to the original combination. If an original combination had k elements on each side the resulting combination will contain (m-k)+k=m elements. This number does not depend on k and is the same for all the resulting combinations. And if we take any combination of m elements and perform the same operation on its left side, we will obtain a combination with equal number of elements on each side. So we have a one-to-one correspondence, and the number of original combinations is equal to the number of resulting combinations, which is {m+n \choose m}.

Update: joffan essentially describes the same.

1 Like

Whats wrong with this code

for(long int i=0; i<n-1; i++){
    ans += 1;
    ans += min(right, left);
    if(a[i]==a[i+1]){
        long int right_ = right -1;
        ans += 1;
        ans += min(right_, left);
    }
    left += 1;
    right -= 1;
    ans %= MOD;
}

i am iterating in array and updating the ans. left and right are number of elements in the left and right respectively

What’s wrong in this solution: CodeChef: Practical coding for everyone

I used another binomial identity–the hockey stick identity–for an O(n^2) solution.

https://www.codechef.com/viewsolution/21386816

Sums of binomial coefficients where n is changing have a closed form. There doesn’t exist a closed form where k is changing.

\sum_{i=0}^n \binom{i}{k} = \binom{n+1}{k+1}
\sum_{i=m}^n \binom{i}{k} = \binom{n+1}{k+1} - \binom{m}{k+1}
1 Like

Guys my code does the exact same thing. Please someone look into this why it’s giving WA.
CodeChef: Practical coding for everyone

Since we have to sort each subsequence
to find its median, we will sort our
original sequence (i.e. A1,A2,…,AN) at
first itself. The ordering won’t
change. For example, the good
(desired) subsequences in the sequence
[9,5,7,6,8,1] are same as that in the
sequence [1,5,6,7,8,9].

There is a logical error here. I assume that you mean that the number of good subsequences is independent of the ordering of the sequence. But consider [1, 2, 3, 2, 3] and its ordered counterpart [1, 2, 2, 3, 3]. In the former, [1, 2, 3, 2] and [1, 2, 2, 3] must be counted as two distinct good subsequences, while in the latter, [1, 2, 2, 3] is the only corresponding good subsequence.

I am surprised that this solution gets AC. Either the test cases were weak or I don’t understand the problem correctly.

I gather that this solution counts [1, 2, 2, 3] twice for the sequence [1, 2, 2, 3, 3] (in the step where we choose 1 number form the 2 numbers to the right of 2’s, we use 2C1 which counts both the 3’s individually). But, isn’t this wrong? The problem becomes a lot more difficult if we try to take into account that many numbers can be non-distinct and then the Vandermonde’s identity expression is invalidated!

@aryanc403 Done :slight_smile:

If I was in hinting mood I would just say “Pascal’s triangle”.

More fully, because of the ability to derive binomial coefficients - the entries of Pascal’s Triangle - by summing two earlier values, it’s very quick to generate the values you need in this problem. And of course since we’re just adding, we can take mods as we go.

The problem with your code arises in counting the number of good subsequences of even length.
One of your failed test case will be
N=6
A=1 2 2 2 2 3
Output should be 54
Your code’s output : 46
I suggest you to read the editorial completely. I’ve shown the same test case as an example to pass subtask 2

1 Like

Your code fails for sequence having multiple duplicates. Also, you can’t just add the min(left,right) directly to your ans. I suggest you to read the example I’ve given in the editorial above

In [1,2,2,3,3] -> [1,2,2,3] (with 3 of index 3) and [1,2,2,3] (with 3 of index 4), both are good subsequence

Its late, but thanks!:slight_smile: