zwezdinv's blog

By zwezdinv, history, 5 months ago, In English

Thanks for participation!

1994A - Diverse Game

Editorial
Code

1994B - Fun Game

Editorial
Code

1994C - Hungry Games

Editorial
Code

1994D - Funny Game

Hint
Editorial
Code

1994E - Wooden Game

Hint
Hint.answer
Hint.proof
Editorial
Code

1994F - Stardew Valley

Editorial
Code

1994G - Minecraft

Hint 1
Hint 2
Hint 3
Editorial
Code

1994H - Fortnite

Editorial
Code
  • Vote: I like it
  • -129
  • Vote: I do not like it

»
5 months ago, # |
  Vote: I like it +133 Vote: I do not like it

D is a cute problem

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

What is E?

»
5 months ago, # |
  Vote: I like it +8 Vote: I do not like it

Why are people down voting?

»
5 months ago, # |
  Vote: I like it +20 Vote: I do not like it

Editorial is so fast.Thank You!

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

CDE are very nice

»
5 months ago, # |
  Vote: I like it +97 Vote: I do not like it

the editorial came faster than my girlfriend

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

thank you for fast editorial

»
5 months ago, # |
  Vote: I like it +278 Vote: I do not like it

Think of the most stupid solution you can do

Great editorial for problem G

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

C is also solvable in $$$O(n)$$$ using two pointers.

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

    Yeah I tried it using two pointers but got wrong answer on 776th number of test case 2. Can you check my code, where am I wrong?

        cin >> n >> k;
        vll v(n), zero(n + 1, 0);
     
        rep(i,0,n) cin >> v[i];
     
        rep(i,0,n){
            sum += v[i];
            if(sum > k) {
                sum = 0;
                zero[i] = 1;
            }
        }
     
        per(i,n - 1,0){
            if(zero[i]) zero[i] = zero[i + 1] + 1;
            else zero[i] = zero[i + 1];
        }
     
        // rep(i,0,n) cout << zero[i] << " ";
        // cout << nL;
     
        sum = 0;
        l = 0;
        r = 0;
        while(l < n){
             while(r < n && sum + v[r] <= k){
                sum += v[r++];
            }
            
            ans += ((n - r - 1) - (r < n - 1 ? (zero[r + 1]) : (0))) + (r - l);
            // cout << r << " ";
            sum -= v[l++];
            if(r == n && sum <= k) ans++;
            // cout << ans << " " << l << nL;
        }
       
        cout << ans << nL;
       
    
    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Cant identify exact mistake in code but for input

      1
      6 16
      1 13 12 8 6 10 
      
      

      correct output is $$$15$$$, your code prints $$$14$$$ instead.

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

        Thanks for test case. I will check it.

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

    Bro please share your approach Thank you bro

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

    can you please explain how?

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

    This is my code: 271271875

    So if we start at mushroom $$$i$$$ with $$$g=0$$$ before performing any operations we will hit $$$g=0$$$ again at some $$$j\geq{i}$$$.

    There might be multiple possible values of $$$j$$$ for $$$i$$$ since $$$g$$$ might become zero multiple times over and over again.

    We only need to calculate the smallest such $$$j$$$. Let that number be $$$l_i$$$.

    If $$$j$$$ doesn't exist that means we got to the end of the array and still $$$g\leq{x}$$$. In that case we can assign $$$l_i=-1$$$.

    Code to calculate array l

    Now if we choose $$$l=i$$$, $$$r$$$ can't be $$$l_i$$$ because in that case g will result in zero. Now if we choose $$$l=i$$$, $$$r$$$ can't be $$$l_i$$$ because in that case g will result in zero.

    But $$$l_i$$$ is not the only $$$r$$$ we aren't allowed to pick. Lets say we don't pick $$$r=l_i$$$ and instead go further. Next index $$$k$$$ when $$$g$$$ will hit $$$0$$$ will be

    Spoiler

    Now let $$$dp_i$$$ represent how many values $$$r\geq{i}$$$ we can't pick. The transition is $$$dp_i=1+dp_{l_i+1}$$$. Array $$$dp$$$ can be calculated in $$$O(n)$$$ by for loop going from last to first element.

    Now we will for every $$$1\leq{l}\leq{n}$$$ calculate the number of possible values of $$$r$$$. It is equal to $$$n-l-dp_l$$$ (0-indexed). The total sum is the answer.

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

      How will the next index $$$k$$$ when $$$g$$$ will hit $$$0$$$ be $$$l_{l_i +1}$$$?
      edit: got it
      edit q: how this works? wont the time complexity be $$$O(n^2)$$$?

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

        No, we will go through array $$$a$$$ exactly twice while calculating $$$l$$$ because we know $$$l_i<l_{i+1}$$$ so there is no need to go back to calculate each $$$l_i$$$.

        Time complexity for that part will be $$$O(2n)$$$ which is $$$O(n)$$$.

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

        can you explain how you got it lli+1 ?

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

    i did like this,idk what is the issue

    cant we solve problem C like this-

    1.first applying sliding window and count all the subarras having toxicity less then equal to x.

    now it said process can continue and we have to see if at last it is not zero,so we calculate the min element less then or equal to x from end of the array, let kth posn.

    2.and then again applying sliding window from start to kth posn to find all subarray whose sum>x.

    then just add both 1 and 2.

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

      Unfortunately that won't work, try to find another solution.

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

      Wait I didn't fully understand your solution can you explain it little more?

      Because it looks like $$$O(n^2)$$$ complexity.

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

        i was writing the soln,but during typing I got what was wrong,thanks!!

        by the way i wanted one tip.

        i am able to solve problem up to 1100 rating outside contest.but in the contest idk why I cant solve.

        and also I take much time I think,is there any way to upgrade my skill and speed

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

          Solving problem in practice and in contest is same so don't think about rating or points while in the contest.

          And only way to improve skill and speed is by practicing I guess.

»
5 months ago, # |
  Vote: I like it -11 Vote: I do not like it

for B According to given solution if s = 010000 and t = 001111 then answer is "NO" but actually answer is "YES" . Please correct me if I am missing any thing.

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

    no

    010000 -> 011111 l = 2, r = 6

    011111 -> 001111 l = 1, r = 2

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

    s = 010000; l = 5, r = 6 => s = 010001; l = 4, r = 5 => s = 010011; l = 3, r = 4 => s = 010111; l = 2, r = 3 => s = 011111; l = 1, r = 2 => s = 001111 = t. The answer is "YES". i = 2 — the position of the first '1' in s, and there are only '0' in the interval [1, i) in t, so, according to given solution the answer is also "YES".

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

      one question, why do we need to do this operation in the reversal order(starting with an end)?

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

        We don't need, we can do l = 2, r = 3, ..., l = 5, r = 6, and then, l = 1, r = 2.

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

          thank you) cause in the editorial, there was written — "acting from the end"

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

    The answer would be "YES". Note that "NO" would be the answer iff there is a '1' in t and '0' in s, and the count of '1' in s before this index is 0.

    E.g. 
    s = 00100
    t = 01000
    

    It is to be noted if '1' appears in s before a particular index, all the elements after the index can be made either '0' or '1'. Self xor is allowed, and thus, we can make any element that's '1' to 0.

    In the above example, at index = 1, it's impossible to make '0' to '1' (to match the strings).

    Here is my solution, you can have a look: 271233150

»
5 months ago, # |
Rev. 2   Vote: I like it -9 Vote: I do not like it

Personally, I think $$$E$$$ should be should be placed as $$$C$$$, as it only requires knowing about OR, which is pretty popular as a simple adhoc problem.

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

    this problem is too troll, if it's placed as C i think i can prolly solve it in 10 min lol

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

    hey can you share some resources to study for bitwise operation. I always find it difficult whenever i have to deal with these types of question

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

    after solving it today, i also agree with you. the idea isn't that complicated

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

Another idea for A:

we use the fact that x != (x+1), so we set b[i][j]=a[i][j]+1 and if a[i][j]=(n*m) we make b[i][j]=1.

»
5 months ago, # |
  Vote: I like it +27 Vote: I do not like it

I totally did not guess D, and wrote wrong solution for E but luckily I had some bugs so it passed.

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

Thanks for fast editorial!

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

Any Minecraft players :D

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

Easier sol for A: 271203621

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

can anyone explain the approach of c with two pointers without DP

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

    you have to dp some way or the other

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

    If we choose i as l, we have maximum n-i choices for r. (0<=i<n). When we take i as I and r as n-1 and if we calculate g for all positions from i to n-1, we may encounter ct positions where g is zero. This is a significant finding, as it means there are ct indices that cannot be r if i is chosen as l. To keep track of this crucial information, we introduce an array c[n], which stores the number of zeros encountered when i is chosen as I and r is chosen as n-1. Now, we only need to find array c[n]. Now, we will define an array sum[n], which will store g if we calculate it backward from n-1 to 0. Lets say the resulting array is something like this [-,-,-,-,0,-,-,-,0-,-,-,-] let say the position of zeros be x and y, (y>x). So for all indices x<i<=y, we take i as l and n-1 as r, then we will encounter only one position with g equals zero. Similarly, for all 0<=l<=x and r=n-1, we will encounter two indices with g=0. Extend this to a general case, and we have an array that gives c[i] (number of indices at which g=0 if l=i and r=n-1). Then we have ans=summation(n-i-c[i]).

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int t;
        cin>>t;
        while(t--){
            int n,x;
            cin>>n>>x;
            int arr[n];
            for(int i(0);i<n;++i) cin>>arr[i];
            int sum[n]={0};
            int c[n]={0}; // counts the number of zeros from i to n-1
            sum[n-1]=arr[n-1];
            int ct=0; // counts number of zeros
            long long ans=0;
            for(int i(n-1);i>=0;--i){
                if(i!=(n-1)){
                    sum[i]=(arr[i]+sum[i+1]);
                    c[i]=c[i+1];
                }
                if(sum[i]>x) sum[i]=0;
                if(sum[i]==0){
                    ++ct;
                    c[i]=ct;
                }
            }
            for(int i(0);i<n;++i){
                ans+=(n-c[i]-i);
            }
    
            cout<<ans<<endl;
        }
    
        return 0;
    }
    
    • »
      »
      »
      3 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you for the detailed explanation This is one of the easiest to understand and most optimal solutions to the problem

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

Editorial of F: "Lets solve independently for each component." Task statement of F: "It is guaranteed that you can reach any house from any other by walking on the roads with NPCs only."

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

    Editorial means to solve for each component consisting of only black edges.

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

Thank you for fast editorial!

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

D was a really cool problem. orz

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

Can somebody please explain why my solution for D is getting accepted. I have literally zero idea (according to me, it should have given TLE).
Here is the link: 271248022
Any help would be appreciated.

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

help me, why we need to solve from end in problem C ?

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

    dp(i) means count of all good subarrays with i as starting point which is easier to do as transition is O(1) time comp. while if we take for dp(i) as count of good subarrays such that ending at i then transitions are not possible in O(1) but O(n) complexity as there are continuous ranges of index one or more j<=i at which we can always end at i with a nonzero value.

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

For Problem 1994E - Wooden Game we could also sort tree sizes decreasingly, and then add each one to the answer if it adds bits for all possible highest positions, checking with (ans | size) == (ans ^ size), and decreasing size until this requirement is met.

a = readTreeSizes()
sortDecreasingly(a)
ans = 0
for (size in a) {
    while ((ans | size) != (ans ^ size)) {
        size--
    }
    ans |= size
}
println(ans)
  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    as size of the array and arr[i] both are up to 1e6, then how can we say that it won't give TLE ??

    Is there any proof for this??

    Please share it will he helpful

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

      The sum of all tree sizes (k and n) is up to 1e6 over all testcases, so in total you will have at most 1e6 decrements.

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

    sorting in not required

»
5 months ago, # |
  Vote: I like it +12 Vote: I do not like it

What's $$$mod$$$ in the editorial to H?

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

    mod is m that we need to find

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

      Then I don't get how querying any string with non-modulus hash in $$$[h_1-a_1-m, h_1-a_1-1]$$$ is enough to find $$$m$$$. Suppose $$$h_1=10^{15}$$$, $$$a_1=0$$$, $$$m=1000$$$ and we ask about some string with real hash equal to $$$999999999999050$$$ and get $$$50$$$ as answer. How can we know that $$$m=1000$$$? It could also be equal to $$$100$$$ for example.

      • »
        »
        »
        »
        5 months ago, # ^ |
        Rev. 3   Vote: I like it +12 Vote: I do not like it

        We know that $$$h_2 \ge h_1 - a_1 - m$$$($$$h_2$$$ is hash of found string), so $$$m$$$ can't be $$$100$$$. $$$m = (h_1 - a_1) - (h_2 - a_2)$$$, where $$$a_2$$$ is $$$h_2$$$ $$$mod$$$ $$$m$$$.

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

        if m=100 in this case 999999999999050<h1-a1-m

»
5 months ago, # |
  Vote: I like it +29 Vote: I do not like it

G < F I think, but I have another solution with simple dp.

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

Must D has a sulution?Why don't I see "No" in the code of editorial of D?

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

    Note that we have chosen $$$x+1$$$ numbers, so by the pigeonhole principle, some two numbers will be equal modulo $$$x$$$.

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

    There always is a solution

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

Note that we have chosen $$$x+1$$$ numbers, so by the pigeonhole principle, some two numbers will be equal modulo $$$x$$$.

Why is this true?

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

    Because there are $$$x + 1$$$ numbers from $$$0$$$ to $$$x - 1$$$.

»
5 months ago, # |
  Vote: I like it +41 Vote: I do not like it

Idea for D for anyone who is a bit confused :)

The answer will always be possible due to the pigeonhole principle.

For example let’s say we have 5 vertices labeled a, b, c, d, e . We start with the 4th operation. In this case, we are looking at the vertices where the values are taken modulo 4. The possible remainders are 0, 1, 2, and 3. Since we have 5 vertices, at least one remainder must repeat (by the pigeonhole principle).

For example, if vertices a and b both have the same remainder when divided by 4, we can connect these vertices.

What if there is more than one pair of vertices with the same remainder when divided by 4? We can choose any of these pairs. It won’t affect the final answer.

Suppose we connect a and b . We can mark this component as A . Now we have 4 different components: A, c, d, and e . Next, we proceed with the 3rd operation. Using the same logic, we connect two components where vertices share the same remainder modulo 3.

Why Not Start with the First Operation? If we started from the first operation, we might encounter problems. For example, suppose we connect a and b in the first operation and then b and c in the second operation. When we reach the third operation, the remainders modulo 3 for vertices a, b, c, d, and e might be something like 0, 0, 0, 1, 2 . Now we can’t pick any pair of vertices to connect because vertices a, b, and c are already in the same component.

By starting from the (n-1) -th operation and working our way down to the first operation, we ensure that we can always find pairs of vertices to connect, gradually reducing the number of components until the graph is fully connected.

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

    By starting from the (n-1) -th operation and working our way down to the first operation, we ensure that we can always find pairs of vertices to connect, gradually reducing the number of components until the graph is fully connected

    how can we prove this ?

    upd: Resolved

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

    Thanks for explanation :)

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

    Great explanation!

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

    way better explanation than the editorial

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

    Thanks for your explanation, it makes far more sense to me than the editorial one.

    But still I have 2 doubts:

    1. "What if there is more than one pair of vertices with the same remainder when divided by 4? We can choose any of these pairs. It won’t affect the final answer." -> Why??

    2. "Now we can’t pick any pair of vertices to connect because vertices a, b, and c are already in the same component." -> why can't such case happen when operations are performed in reverse order?

    update: understood by doing dry run in both order.

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

    Thank you so much

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

    I have also used same logic..

    Simpler implementation: MY SUBMISSION LINK

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

why does this code fail for C

    int n, x;
    cin >> n >> x;
    take(a, n);
    vector<int> dp(n + 1, 0);
    for (int i = 1; i <= n; i++)
    {
        dp[i] += dp[i - 1] + a[i - 1];
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (a[i - 1] > x)
            continue;
        int prev = lower_bound(all(dp), dp[i] - x) - dp.begin();
        int prevprev = lower_bound(all(dp), dp[i - 1] - x) - dp.begin();
        ans += (i - prev) + (prevprev);
    }   
    cout << ans << endl;
»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Has anyone solved problem D with max-flow?

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

    I’ve tried, but it seems to be wrong.I can’t guarantee that the graph is connected.The graph I make may contain loops.

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

can someone explain the segtree approach for C ?

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

C without DP: 271240394

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

    why the loop runs from n-1 and not from start?

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

      reason 1. for any loop dp[i] =function(dp[j],dp[j2],dp[j3].....)

      then if all j's are less than i then we have to calculate lesser dp values for lesser i's first in order to do above calculations correctly,so we have to calulate all dp's looping from i=small vale to i=maxvalue

      similarly if all j's are greater than this i then we do looping in decreasing order

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

    Can you pls explain your solution? Thanks.

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

I heard that this contest is hard to remark.

Anyway I didn't sign up for it :D

»
5 months ago, # |
  Vote: I like it +8 Vote: I do not like it

My submission 271300995 for F got TLE on testcase 81 for some reason. Anyone have any idea why?

»
5 months ago, # |
  Vote: I like it +49 Vote: I do not like it

I wonder why problem G is much too easy.

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

74TrAkToR never let me down :))))

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

    In all, being 74 rounds means higher chance of being sponsored but higher chance of low quality.

    Better quality of problems — higher chance of Global

    That is really laughing.

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

Wait... D has no answer '-1'?

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

for the first time ever the tutorial code is really clean, also F reminds me of https://cses.fi/problemset/task/2179

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

The simple solution for problem A is to increase every element by one and n*m integer change into 1.

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

1994E - Wooden Game

You are given a forest of k rooted trees. Lumberjack Timofey wants to cut down the entire forest by applying the following operation:

Select a subtree† of any vertex of one of the trees and remove it from the tree.

Timofey loves bitwise operations, so he wants the bitwise OR of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain.


Why do I think that each operation can only remove one subtree, so it can't get all the numbers from 1 to size?

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

    Start deleting from the leaf nodes one by one . Then as soon as you get the required size, you can delete the entire tree.

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

During contest in Last 15 minutes site stops working after robot verification starts that causes not able to submit solution have writtens.

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

C is solvable in O(n) using prefix sum and 2-pointer: https://mirror.codeforces.com/contest/1994/submission/271259457

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

I have a question about problem E.Think about this test case: 2 trees with size 20 and 12,the shape of size 20 tree doesn't matter and the shape of size 12 tree look like this:

1 2 
1 3 
1 4 
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12

every other vertex is the son of vertex 1.the Editorial code gives the ans = 31,which requries to get a 8-size subtree and a 3-size subtree from the 12-size tree.I don't think it's possible to get both subtree at the same time!

Edit:oh I figured it out.First delete a vertex,then delete the whole tree will do..

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

    can you suggest, how to handle bitwise problem efficiently during contest. They always makes me uncomfortable

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

    [user:man-ray]can you please explain. I'm having same doubt.

»
5 months ago, # |
Rev. 2   Vote: I like it -9 Vote: I do not like it

.

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

https://mirror.codeforces.com/contest/1994/submission/271343266

This is my code for pB . I want to know where I wrote it wrong.

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

    The problem only happens when you use char array, suggested by my testings. I still don't know the cause though, if anyone knows please explain, thanks.

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

    The problem occurs when you have an array of size exactly n for an n character long string. If you change from

        scanf("%d",&n);
        char s[n], t[n];
        scanf("%s",s);
        scanf("%s",t);
    

    to

        scanf("%d",&n);
        char s[n+1], t[n+1];
        scanf("%s",s);
        scanf("%s",t);
    

    it would work fine. I believe this happens because the last character of an array of char must be '\0' or null character but I don't know the details lol.

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

Hello Codeforces I have a doubt regarding problem D. I dont understand why this Code is passing the testcases .It should give TLE right? so why it is passing the testcases and getting accepted. Any help would be appreciated. Thank you

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

can anyone prove or uphack my solution for A? i just try $$$100$$$ different permutation of $$$[1, 2, \ldots, n \times m]$$$ and it got accepted.

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

    I didn't prove, but except for the obvious $$$n=m=1$$$ case it seems like the worst chance for a random permutation to satisfy it is $$$1 \over 3$$$ when $$$n \times m=3$$$, so the chance of not finding an answer with 100 tries is $$$~2.46 \times 10^{-18}$$$. Even if we try hacking it $$$10^9$$$ times it will still almost surely pass.

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

      ... I didn't see that the random seed could be hacked, lol

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

Problem D, why n=1?

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

I believe upper bound for sum in problem G is n-1, can anybody prove it formally?

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

submitted code for a 959 A diverse game getting failed testcase on vscode and output is wrong but here it got accepted.how so???

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

It was a really nice contest.. I don't understand why so many downvote.. Upsolving was really fun.. Especially d, e, and g.. Thanks for such contest

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

In Editorial D , I think it must be before operation x not after operation x , there will be x+1 connectivity components in the graph .

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

CAN ANYONE TELL HOW TO APPROACH BIT MANIPULATION PROBLEMS?

is there any theory like how we should approach and all??

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

    there are certain properties of each of the bitwise operation, and bit manipulation problems requires you to implement those properties into your code. Ex: if a XOR b = c, then a XOR c = b, ...

    Just learn and solve along the way, I guess

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

      by the way i wanted one tip.

      i am able to solve problem up to 1100 rating outside contest.but in the contest idk why I cant solve.

      and also I take much time I think,is there any way to upgrade my skill and speed

»
5 months ago, # |
Rev. 2   Vote: I like it -13 Vote: I do not like it

can someone tell me whats wrong with the code , i have tried to read the editorial of the G question and replicated the same , but it exceeds time limit.

ll m=1000000000+7;

string m1;
ll k;
vector<vector<int>> arr;
vector<ll> crr;
ll n;

bool rec(ll i,ll sum){
    if(sum<0){
        return false;
    }
    if(i==k){
        if(sum==0){
            return true;
        }else{
            return false;
        }
    }
    ll c1=crr[i];
    if(rec(i+1,sum-((n-c1)*(1ll<<i)))){
        m1+='1';
        return true;
    }else if(rec(i+1,sum-((c1)*(1ll<<i)))){
        m1+='0';
        return true;
    }else{
        return false;
    }
}

ll solve()
{
    I n>>k;
    int brr[k];
    string s1;
    I s1;
    for(int j=0;j<k;j++){
        brr[j]=(s1[s1.size()-j-1]-'0');
    }
    for(int i=0;i<n;i++){
        string s;
        I s;
        vector<int> V;
        for(int j=0;j<k;j++){
            V.push_back(s[s.size()-j-1]-'0');
        }
        arr.push_back(V);
    }
    for(int i=0;i<k;i++){
        ll c1=0;
        for(int j=0;j<n;j++){
            if(arr[j][i]==1){
                c1++;
            }
        }
        crr.push_back(c1);
    }
    ll sum=0;
    for(int i=0;i<k;i++){
        if(brr[i]==1){
            sum+=(1<<i);
        }
    }
    // for(int i=0;i<n;i++){
    //     for(int j=0;j<k;j++){
    //         O arr[i][j]<<" ";
    //     }
    //     O endl;
    // }
    if(rec(0,sum)){
        O m1<<endl;
    }else{
        O -1<<endl;
    }
    return 1;
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
        arr.clear();
        crr.clear();
        m1="";
    }
    return 0;
}- 
Your code here...
»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

cant we solve problem C like this-

1.first applying sliding window and count all the subarras having toxicity less then equal to x.

now it said process can continue and we have to see if at last it is not zero,so we calculate the min element less then or equal to x from end of the array, let kth posn.

2.and then again applying sliding window from start to kth posn to find all subarray whose sum>x.

then just add both 1 and 2.

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

For F, it's saying "If a component has an odd number of vertices with odd degree". How this is possible? Shouldn't each connected component always has even degree, since adding or removing an edge adds +2/-2 degree?

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

    nvm, i got it, it's the vertex with odd degree not the whole connected component.

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

I didn't get the proof of D. Suppose there are two edges that can be formed with a certain x. Suppose the edges are e1 and e2; again with a value less than x we can form one edge that is either of e1 and e2, let's say e1. What if we add edge e1 first with x and then again we have the only choice to add e1. How the solution is getting optimal so that it is passing this?

One more thing... When we are merging nodes why the assignment of parent doesn't matter. Suppose a can be parent of b and b can be parent of a. Why choosing any of them is fine?

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

    The pigeonhole principle suggests that such situation cannot exist. If each of $$$x+1$$$ values are one of $$$x$$$ kinds then there must be at least one value that appears more than once. If there are $$$x+1$$$ components, you can pick any one vertex from each component and perform modulo $$$x$$$, then each value has to be one of $$$0$$$, $$$1$$$, ..., $$$x-1$$$, total of $$$x$$$ kinds, so at least one value should appear on at least two components, so you can add an edge between them.

    The merging part is nothing other than a DSU. It's a basic topic so I suggest you to search for it and learn.

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

Would F be solvable in a similar way if there wasn't the constraint that all houses can be reached using NPC only roads? I got caught up on this case for a while until I reread the constraint.

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

D is one problem that has a very brilliant solution.

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

Problem D: "Since the order of operations is not important" Could someone please explain this part?

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

    It means we can add edges for any order of $$$x$$$, not only $$$1,2,3,...,n$$$.

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

E has a nice bigger friendly solution too. As said in the editorial, we can make any number from 1 till n in a tree with size n, we will try to answer greedily. It is quite clear we will definetely need the largest tree. We can remove it and add it to our answer. After that for every n, we will iterate from $$$ 1 ... n $$$ and we will get two variables $$$ x$$$ and $$$y$$$ such that $$$ x + y = n $$$. Now, we just need to maximize our answer. We can save a previous variable and then the calculation would just be

$$$ ans = max(ans, prev \big| x \big| y) $$$
code

There is also an overcomplicated implementation by actually storing all the trees in the forest and then doing subtree dp with dfs. Submission

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

In Problem E, the editorial says that if we have a bit set in sizes of two trees then we can set all other lower bits using that but this case is not possible if we can't always break the tree in desired size. For example for test case

1
2
8
1 1 1 1 1 1 1
8
1 1 1 1 1 1 1

there are two trees with size 8. but by breaking the second tree we can only obtain 1 tree of size 4 and 4 tree of size 1. Thus breaking that tree into tree of size 4 and size 2 simultaneously is not possible. So the answer for this case should be 13 (8 | 4 | 1).

Someone please correct me if i'm wrong.

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

    the answer for this case will be 15. you can remove one of the trees completely. then you can remove one leaf from second tree and then the remaining 7 nodes. total will be $$$ 8 | 1 | 7 = 15 $$$

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

The easiest approach for Problem-A is:

To transverse to each and every element of matrix A and decrement it by 1 i.e. A[i][j] -= 1. then find zero and replace it with n*m. AND report the following matrix as B.

271203176

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

I have made a video explaining the problem C. Hungry Games. Do check it out at:

https://www.youtube.com/watch?v=_FMUfMAVVVw

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

For problem D, I have a seemly stupid question but I got very confused. Are the integers given distinct in the array? Or does it matter if it is not?

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

first time to see problem like D and E. i have to it's genius idea and i love it so much

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

splendid round, thanks!

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

For E the statement “we can make every number from 1 to the size of the tree” of editorial is not correct as some even numbers cannot be made because of or with 1.

correct me if Im wrong.

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

D is cool