Bufu's blog

By Bufu, 4 weeks ago, In English

Thank you for participating in Codeforces Round 1115 (Div. 2)! We hope you enjoyed the problems.

The round was prepared by Bufu and TomitaMatei, and coordinated by cry.


2252A - Boss Fight

Solution
Code (C++)

2252B - Always Changing

Solution
Code (C++)

2252C - Risky Tower

Solution
Code (C++)

2252D - Array Replacement

Solution
Code (C++)

2252E - Generational Triplets

Solution
Code (C++)

2252F - Spectral Components

Solution
Code (C++)
  • Vote: I like it
  • +40
  • Vote: I do not like it

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

A was too hard

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

    its alright i guess

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

    A was a bit tricky :(

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

    By the 30th minute, I still couldn't solve A

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

    I agree

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

    I agree!!!

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

    I agree

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

    I agree.

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

    i agree

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

    l agree

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

A and B were challenging for a newbie like me! Great problems!

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

A and B were harder than C

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

How would u come up with difference array in D?

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

    Yeah I was somewhat wondering about this too. It took me like an hour to see it by which time I was unable to code up the solve. I was just looking at the sample cases and somewhat of throwing mental compute at it until a pattern emerged but I can't really see a way to naturally derive it. Perhaps its all in the algebra or you just had to pattern recognize.

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

    In my opinion, I think this is a standard technique whenever the operations is about adjacent elements, A good starting point is to consider two ideas: Prefix sums & Difference arrays One of them would be 90% the invariant. This problem can be approached in a very similar way: 1110E - Magic Stones

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

      I had seen this exact problem (Magic Stones) just a few days ago, so I went straight for the difference array approach. I could not solve Magic Stones on my first try.

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

    I will try to explain for you my line of thoughts, I hope it helps. First, I begin to see when it is useful to apply the operation. This happens when $$$a_i \lt a_{i + 1} - a_i + a_{i - 1}$$$, which you can rearrange to be $$$a_i - a_{i - 1} \lt a_{i + 1} - a_i$$$. So from here I start considering the difference array $$$d_i = a_i - a_{i - 1}$$$ and try to see how doing an operation at index $$$i$$$ will affect our new array, $$$d$$$. It swaps $$$d_i$$$ and $$$d_{i + 1}$$$ and you can start translate all conditions and restrictions forced by the statement on the array $$$a$$$ to be new conditions on $$$d$$$ as done in the tutorial.

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

    My thoughts on this: If you apply the operation twice to a certain aᵢ, it actually restores aᵢ, because aᵢ-1 + aᵢ+1 — (aᵢ-1 + aᵢ+1 — aᵢ) = aᵢ. This implies that aᵢ is possibly being swapped or something similar, so, taking the difference of the array is at least a direction worth trying. (And along the path you'll eventually find that di and di+1 are swapped) A similar approach also appears in some other problems, if I remember correctly.

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

How am I supposed to come up with the answer of D.

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

    first you can see that every operation is reversable. then you can notice that what happens is that whatever the difference was to the left element, is now the difference to the right element, and vice versa. now you can rearrange the differences in continuous segments that all have the condition a[i-1]%2==a[i+1]%2 however you want. then its simply obvious that you want to sort these and apply the smallest first in order to have the lexicographically smallest possible result

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

Can someone give problems or advice on improving problems like B?

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

Why does A and B were too hard?

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

Does anybody have alternate approaches or ideas as to how one would derive a solution for E?

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

    Yes, first steps are same we need conclusion that a^c = 2(a&c). Then let's write a, c in two lines. This equation means that our table is built from three types of blocks (first line, second line) (0 1, 1 1) (1 1, 0 1) (0, 0) So now we built dp[size] — how many different tables with 2 x size table. This could get the answer for a, b, c where c is less than 2^log_2(n). Now we want to calclulate situation with c more than our bound. It means that we already know how the first two rows of the table are looking(it built from first or second block). And finally we get another dp, which means can we get prefix of number c using this blocks. Using this dp and the first one we look where will be first difference between c and n. 385993550

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

    This is how I approached E ....

    S(n) is number of all such triplets such that a<b<c<=n whose xor is 0 T(n) is number of such triplets such that c is n so, S(n) = T(n) + T(n-1) ..... T(1)

    suppose n is such that n has more than 3 bits in binary representation( for n < 8 .... only T(3) =1 and T(6) is 1 , else all are zero)

    suppose n = 2^k + 2^r + x ..... r < k, x < 2^r (if n is a perfect power of 2 ... T(n) = 0) since (k+1)th bit set we must choose b & c such that b have (k+1)th bit set and c does not. So b = 2^k + y ....... but we need 2*b — n < 2^k which implies 2y < x + 2^r ...... so y < 2^r

    This would imply c = 2^r + z ..... (c need (r+1)th bit set and z<2^r) now we have 2b-n < 2^(r+1) ..... 2^k <= 2^k + 2y < 3.2^r + x < 4^r .... r > k-2 .... so r = k-1

    So n = 2^k + 2^(k-1) + x , b = 2^k + y , c = 2^(k-1) + z ..... where x,y,z < 2^(k-1) also notice xor of n,b,c is zero if and only if xor of x,y,z is 0. 2b = n + c implies 2y = x + z

    if x = y = z .... then they have to be 0. But otherwise we need not have x > y > z , x < y < z is also possible ..... But essentially we have a way to build all k bit solutions from all the pairs of <= k-2 bits .... we need to keep track of highest and lowest numbers of all triplets

    p(k) is number of such triples such that highest number is k bit then ..... p(k) = 1 + 2*(p(2) + p(3) .... p(k-2))

    1) lastly use induction to T(n) is either 1 or 0 2) Binary representation of all the n's such that T(n) = 1 and n is a k-bit number is of the form (111...111)(0)(any binary number) where starting 1 appears even number of times 3) c's corresponding to all above n are all (k-1) bit numbers and are of the form (111...1)(0)(any binary number) where starting 1's appear odd number of times

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

The condition in problem D had shown up before, in Problem 1110E. I wasn't able to solve it back when I tried that problem, but I immediately was able to recognize it in this contest. (edit: seems like it was already mentioned in this comment)

Also A and B were hard for me XD -- Here's how I did for the first half an hour:

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

    Seeing even a Master turn out like this gives me peace of mind— I thought I was an idiot :(

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

I think there's an alternative approach to E: I'm not sure how to prove this, but a brute-force check indicated that the bit representations for a, b, c follow a pattern. The 64 bits can be broken into chunks of 1 or 2 bits:

  • 2 bits, across the 3 numbers it must be (01, 10, 11)
  • 1 bit, across the 3 numbers it must be (0, 0, 0)

I submitted a solution that recursively (+memoization) counts the number of ways in which such triplets (a, b, c) can be constructed, and it passes all test cases.

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

D is insane, just guess and pray

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

Could you solve C by binary searching on the min threshold you take and then binary searching on how many times you take that threshold?

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

For E, answer = number of numbers between 3 and n such that if you write their binary representation, the first (most significant) consecutive group of ones has even length.

I kinda guessed this by brute force (shame on me), but it's quite an interesting result and it shouldn't be very hard to prove. Also, no dp or weird techniques, yay!

Submission: 385997563

A few interesting observations (assume n to be infinite here):

  • If you fix b, it needs to be even and not have two consecutive ones in binary
  • The possible a's for this b will be of the form (b / 2) + (some mask of b not including the first bit of b)
  • Thus, possible c's will be of the form (3 * b / 2) — (some mask of b not including the first bit of b)
  • With that in mind, if we look at some c, we can uniquely derive the b that it came from: we know the first bit of b = the first bit of c, the second bit of c will be the b/2 part of the first bit (notice that they have to be consecutive), and from now on we look at groups of consecutive ones, we know that the smallest bit from there exists in b/2, so it (shifted to the right) exists in b, if it has something to the left that is an undeleted bit of b, so we don't really care (still uniquely determined b) and continue
  • Indeed, the only thing we need to worry about is that if the first group has odd length, the last bit is assumed to be existing in b/2, so inductively each bit in an odd (1-indexed) position has to be part of b/2, but we know that the first bit has to be in b, so a bit exists in both b and b/2, so there are two consecutive bits in b => contradiction with the first statement

I might come back with a formal proof for the first two observations (or maybe someone else can comment one), the others being direct results of those two... but anyway, quite an interesting result :)

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

    Yes, in order to get 2a&b= a xor b The main observation is that we should have above each 1 in both a and b, a 0 1 or 1 0 and vice versa

    so if we consider that a<b, then look at the blocks of bits in b that are equal to 1, and look at the most significant bit and its block of ones, it must have an even size, since we must be able to split it into pairs so that in this pair we put a 0 then 1 below it in a. Then the other blocks of ones can have any size, if a block has an even size, we can just fill a with pairs of 01, otherwise we can put a 1 in a in the bit that is to 0 and above that block then alternate starting by 1

    for example if b=[1111]000[111]0, then a=[0101]001[101]0, [] means a block of ones in b. So like this our condition will be verified

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

Elegant approach for Problem E: Let's try constructing a solution for $$$(a,b,c)$$$ bit by bit. Assume right now we have $$$(0,0,0)$$$, so the differences are $$$b-a = c-b = 0$$$.

We ask ourselves, what combinations of bits will maintain $$$b-a = c-b$$$? The options for combination are those that have either $$$0$$$ bits or $$$2$$$ bits: $$$(0,0,0), (1,1,0), (1,0,1), (0,1,1)$$$.

First, we must put $$$(0,1,1)$$$ because otherwise $$$a \lt b \lt c$$$ is not satisfied. Then, $$$b-a = 2^x$$$ and $$$c-b = 0$$$.

If we then put $$$(1,0,1)$$$, the differences turns into $$$b-a = 2^x-2^{x-1}=2^{x-1}$$$ and $$$c-b = 0+2^{x-1}$$$, so this is good and the equality is maintained after this combination: $$$(0,1,1),(1,0,1)$$$.

On the other hand, if we put $$$(0,1,1)$$$, $$$b-a = 2^x$$$ and $$$c-b = -2^{x-1}$$$ and there is no way any future moves can salvage this as their sum is at most $$$2^{x-1}-1$$$, so the best we can do is reduce the difference between $$$b-a$$$ and $$$c-b$$$ by $$$2\cdot (2^{x-1}-1)$$$, but the current difference is more than that. By similar logic, if we put $$$(1,1,0)$$$ we have $$$b-a = 2^x+2^{x-1}$$$ and $$$c-b = 0$$$ and no future moves can salvage this. If we put $$$(0,0,0)$$$, $$$b-a = 2^x$$$ and $$$c-b = 0$$$ and no future moves can salvage this since again, their sum can be at most $$$2^{x-2}$$$.

Thus, we've proven that whenever we do $$$(0,1,1)$$$, it must be immediately followed by a $$$(1,0,1)$$$, and that will maintain the equality, but if we put anything else, it will never maintain equality.

Now, assume that currently, the equality $$$b-a=c-b=e$$$ (for some $$$e$$$) is maintained, and we want to find a future combination of moves such that the equality is maintained again. We have just shown that $$$(0,1,1),(1,0,1)$$$ is a viable option and the only option if we ever decide to place down a $$$(0,1,1)$$$.

A similar proof can be used to show that whenever we put a $$$(1,1,0)$$$, it must always be followed by a $$$(1,0,1)$$$.

If we put down a $$$(0,0,0)$$$, this doesn't affect the equality, so we can do this as many times as we'd like.

Lastly, if we put down a $$$(1,0,1)$$$, the equality is $$$b-a = e-2^x$$$ and $$$c-b = e+2^x$$$. There is no way any future moves can salvage this as their max sum is $$$2^{x-1}-1$$$, so again we can at most reduce the difference by $$$2\cdot (2^{x-1}-1)$$$ but the current difference is $$$2 \cdot 2^x$$$, so it's more than that.

Thus, we've proven that the valid combinations we can place down are only $$$(0,0,0)$$$ or $$$(0,1,1)$$$ followed by $$$(1,0,1)$$$ or $$$(1,1,0)$$$ followed by $$$(1,0,1)$$$.

Now, what to do with this information? Since we only care that upper bound <= n, we look at the $$$c$$$ value. The options we see (based on above) are $$$0$$$, $$$11$$$, $$$01$$$ respectively. If we use these to construct $$$c$$$, then each of them perfectly match to a triplet based on the rules we created above, as all options are distinct and all have a different number of 1s, so perfect bijection.

more formal proof?

Other than the rule of "only using 3 options", we also note that in the beginning, it must start with a $$$11$$$. Thus, the problem turns into: "how many binary strings <= n can we construct such that it starts with $$$11$$$ and it uses only $$$11$$$, $$$01$$$, $$$0$$$?" Note given ANY string, if we ignore the prefix block of $$$1$$$s, then we can construct any odd length block of $$$1$$$s with a $$$01$$$ followed by $$$11$$$s, and a even length block of $$$1$$$s by just putting $$$11$$$s, and just fill the rest with $$$0$$$s since we can use it anytime we want. Thus, we turn our attention towards the prefix block of $$$1$$$s. We can only place down $$$11$$$s, and not $$$01$$$s, so the prefix block of $$$1$$$s. Must be even length. This is the only constraint. Thus, the problem then turns into "how many binary strings <= n such that it starts with a even number of consecutive $$$1$$$s before the first $$$0$$$".

This can be easily solved by brute forcing all lengths of binary strings, then brute forcing over all even length prefixes of $$$1$$$s. We then place a $$$0$$$ after, then the remaining string can be whatever combination we want (we can construct any string using $$$11$$$, $$$01$$$, $$$0$$$ by above strategy). Thus anything from $$$0$$$ to $$$2^k-1$$$ (for some $$$k$$$) is possible.

For example, consider if our length is $$$x$$$ and our prefix length is $$$y$$$ (where $$$y$$$ is even). Then, the prefix of $$$1$$$'s is $$$(2^y-1) \cdot (2^{x-y})$$$ and $$$k = x-y-1$$$ so we can have everything from $$$0$$$ to $$$2^k-1$$$ as suffix. Here, we can easily handle the overflow case by just taking the minimum of $$$n$$$ without the prefix ($$$y$$$ $$$1$$$s at the front), and the suffix we can obtain. That is, we take $$$min(n-(2^y-1) \cdot (2^{x-y})+1, 2^{x-y-1})$$$.

Submission

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

No idea why I got disliked, just say yall got a herd mentality T_T

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

There could be a harder version of C where the problem becomes a bit more sophisticated. Basically it would be the same C problem but the jenga wouldn’t collapse if you take M elements from a level. So basically the problem would be like The tower collapses if the condition is met, the stability of index i drops to 0 or less, if it is not possible output -1.

It can be solved with Co-ordinate compression + segment tree with binary search at each n, giving us a total time complexity of n*log^2(n*m). That's the code i solved C which was pretty inefficient for this specific problem but it works.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e18;
struct segtree{
    vector<pair<ll,ll>> seg_tree;
    ll n;
    segtree(ll n) : n(n){
        seg_tree.resize(2*n);
    }
    void update(ll i, ll val){
        i += n;
        for(seg_tree[i].first=val,seg_tree[i].second = 1; i > 1; i>>=1){
            seg_tree[i>>1].first = seg_tree[i].first+seg_tree[i^1].first; 
            seg_tree[i>>1].second = seg_tree[i].second+seg_tree[i^1].second;
        }
    }
    pair<ll,ll> query(ll l, ll r){
        pair<ll,ll> res = {0,0};
        for(l += n, r += n+1; l < r; l>>=1, r>>=1){
            if(r&1){
                pair<ll,ll> tmp = seg_tree[--r];
                res.first += tmp.first;
                res.second += tmp.second;
            }   
            if(l&1){
                pair<ll,ll> tmp = seg_tree[l++];
                res.first += tmp.first;
                res.second += tmp.second;
            }
        }
        return res;
    }
};
 
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll tt;
    cin >> tt;
    while(tt--){
        ll n,m;
        cin >> n >> m;
        vector<ll> v(n);
        for(int i = 0; i < n; i++){
            cin >> v[i];
        }
        vector<vector<ll>> mat(n,vector<ll>(m));
        vector<vector<pair<ll,ll>>> marked(n,vector<pair<ll,ll>> (m));
        vector<pair<ll,ll>> sorted;
        ll timer = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                cin >> mat[i][j];
                marked[i][j].first = mat[i][j];
                marked[i][j].second = timer++;
                sorted.push_back(marked[i][j]);
            }
        }
        sort(sorted.rbegin(),sorted.rend());
        map<pair<ll,ll>,ll> mp;
        timer = 1;
        for(int i = 0; i < (int)sorted.size(); i++){
            mp[sorted[i]] = timer++;
        }
        segtree st(n*m+10);
        ll ans = m;
        for(int i = n-1; i>=0; i--){
            for(int j = 0; j < m; j++){
                ll val = mp[marked[i][j]];
                st.update(val,mat[i][j]);
            }
            ll l = 0;
            ll r = n*m;
            ll tmp = INF;
            while(l<=r){
                ll mid = (r+l)/2;   
                pair<ll,ll> res = st.query(0,mid);
                if(res.first>=v[i]){
                    tmp = res.second;
                    r = mid-1;
                }else{
                    l = mid+1;
                }
            }
            if(tmp!=INF) ans = min(ans,tmp);
        }
        cout << ans << endl;
        
 
    }
}
  • »
    »
    4 weeks ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I overcomplicated the problem in a similar way. But you don't need a segment tree for something like that right? I think my submission will also work if you tweak my binary search in function solve() a bit.

    Basically the idea is exactly like the editorial, except I binary searched the answer and stored the corresponding amount in the priority queue in each layer as a check function. Then I would have the sum of the biggest k elements at each layer and able to get answer. Answer is pretty slow so might need some extra optimizations.

    Refer to submission: 385939195.

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

A is hard for me TAT

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

I did B with DP. Mainly the same idea as the first part of editorial, but after counting the number of ones and zeros in the entire string, I use dp to find the longest alternating subsequences of the following conditions:

(Additionally, let D[i] = #ones — #zeros in subsequence i)

starting with 0, ending with 0 (0b00) (D[0b00] = -1)

starting with 0, ending with 1 (0b01) (D[0b01] = 0)

starting with 1, ending with 0 (0b10) (D[0b10] = 0)

starting with 1, ending with 1 (0b11) (D[0b11] = 1)

Now, imagine keeping one of these subsequences j and removing everything that was not in that subsequence. The difference between the removed ones and the removed zeros is equal to:

(#ones in entire string — #ones in subsequence) — (#zeros in entire string — #zeros in subsequence)

= #ones in the entire string — #zeros in the entire string — D[j],

and if its absolute value is less than or equal to 1, then we let this alternating sequence contribute. Sorry for my poor writing. My submission 386004241 might be helpful.

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

Emm, I wrote a Fenwick Tree to solve C in time complexity O(nmlog^2(nm)), still WA and unable to hack my code......

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

A is too hard.It tooks me about 20 minutes.

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

There's a solution without attention for problem E.

We use binary digit DP. Let $$$f[i][d][p][q]$$$ be the number of assignments for the lowest $$$i$$$ bits such that $$$a+c-2b=d\cdot2^i$$$, where $$$p$$$ and $$$q$$$ indicate whether the lower bits are required to satisfy $$$a \lt b$$$ and $$$b \lt c$$$. When adding new highest bits $$$x,y,z$$$, we require $$$x\oplus y\oplus z=0$$$, and the new carry is $$$(d+x+z-2y)/2$$$. We then scan the bits of $$$n$$$ from high to low while keeping the prefix of $$$c$$$ equal to the prefix of $$$n$$$, together with the required carry and whether $$$a \lt b$$$ and $$$b \lt c$$$ have already been determined. When the current bit of $$$c$$$ becomes smaller than the corresponding bit of $$$n$$$, the remaining lower bits are unrestricted and can be counted directly using $$$f$$$. Finally, we add the state where $$$c=n$$$, the carry is zero, and both inequalities are strict. The complexity is $$$O(\log n)$$$ per test case.

You can find my implementation here : 385956802.

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

In which world A and B are supposed to be easy.

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

Thansk for aditorial

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

I succeed in doing ABC. :)
Why many prople says A is hard?
A is easy.Reasons:
1. It's the only one problem what close the solution submission channel.
2. You can think that make all the cards in a row different,so you should put card what more first.
But it's a greedy round.The problems are all have greedy tag expect E. :<

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

C was easier than B.

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

in problem A, I think one of the edge case is missing in the given editorial and the solution.

when their are multiple values which have the same max_frq then, which value is need to be considered as 'X'?. It must the be the min_value from all the values whose frq is same as max_frq.

Why min_value need to consider as 'X'? because all 'X' can be taken or maybe some dropped, if we need to drop some 'X', then considering min_value for 'X' is better choise.

but editorial didn't specified this, and the solution consider 'X' value as any value whose frq is max_frq.

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

    I don't think choosing the minimum value among all maximum-frequency values is necessary.

    Let the maximum frequency be $$$f$$$. If there are at least two distinct values with frequency $$$f$$$, then

    $$$ 2f \le n \Longrightarrow f \le \left\lfloor\frac{n}{2}\right\rfloor $$$

    Hence, no value appears more than $$$\left\lfloor\frac{n}{2}\right\rfloor$$$ times. Under this condition, the elements can always be arranged by interleaving, regardless of which maximum-frequency value is chosen as $$$X$$$.

    So I don't see why the smallest value among all maximum-frequency values must be selected. It seems that any value with maximum frequency should work.

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

      Ah, I see it now! That’s a really neat observation & proof— the case I was worried about is actually never a issue. I was treating it as a valid edge case without first checking whether it was reachable.

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

CF problems are evolving it seems.

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

why this code doesn't work for C.

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

good contest

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

I think my code is wrong/has some logical error for A but passed due to incorrect test cases

My approach:

first i took a map to find the freq array

then made a pair as {freq,damage}

sorted them

then used 2 pointer on first and last one to add dmg alternatively

It should fail on [(5,10),(4,9),(4,8)]-->{freq,dmg}

I think there is an error in test cases or missing

This is my code : 385926129

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

There is any other approach for A?

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

is there any dp approach for B ?

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

anyone who hasn't figured the solution of B.

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

Yay !

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

In problem B's editorial, how does the solution make sure that the characters are deleted alternatively?

»
8 days ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

A is very similar to the problem task scheduler in leetcode , you could read their editorial for better understanding and other alternative approaches like pq