awoo's blog

By awoo, history, 5 months ago, translation, In English

1398A - Bad Triangle

Idea: Roms

Tutorial
Solution (Roms)

1398B - Substring Removal Game

Idea: BledDest

Tutorial
Solution (Ne0n25)

1398C - Good Subarrays

Idea: Roms

Tutorial
Solution (Roms)

1398D - Colored Rectangles

Idea: BledDest

Tutorial
Solution (pikmike)

1398E - Two Types of Spells

Idea: Roms and BledDest

Tutorial
Solution (Roms)

1398F - Controversial Rounds

Idea: Roms

Tutorial
Solution (Roms)

1398G - Running Competition

Idea: BledDest

Tutorial
Solution (BledDest)
 
 
 
 
  • Vote: I like it
  • +76
  • Vote: I do not like it

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

How to solve e

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

Can someone give an example of why greedy solution for D is incorrect?

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

    2 3 1

    2 5

    3 4 4

    6

    Greedy will give : (6,5)+(4,2) = 38 But we can take (5,4)+(4,6)+(2,3) = 50

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

      in problem D , .. i assigned color 0, 1, 2 and stored all in a single vector pair(as constraints were low) . sorted in reverse . then used two loops to have pairs whose color is different and not visited earlier ... where i did wrong ???

      cin >> r >> g >> b; vector<pair<ll, ll>> f; for(ll i = 0; i < r; ++i) { ll x; cin >> x; f.push_back({x, 0ll}); } for(ll i = 0; i < g; ++i) { ll x; cin >> x; f.push_back({x, 1ll}); } for(ll i = 0; i < b; ++i) { ll x; cin >> x; f.push_back({x, 2ll}); } sort(f.begin(), f.end(), greater<pair<ll, ll>>()); map<pair<ll, ll>, bool> lit; ll fs = f.size(); ll ans = 0; for(ll i = 0; i < fs; ++i) { for(ll j = 0; j < fs; ++j) { if(f[i].se != f[j].se and !lit[f[i]] and !lit[f[j]]) { ans += f[i].fi * f[j].fi; lit[f[i]] = lit[f[j]] = 1; break; } } } cout << ans << endl;

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

    Consider the case

    1 1 2
    20
    20
    19 19
    

    As you can see the optimum answer is 760, but greedy would give 400, issue being greedy only cares about maximum values, and not take into account the number of pairs available of each color.

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

    Take Case

    1 1 2
    15
    12
    10 10
    

    Greedy will give 15*12=180

    while maximum will be 15*10+12*10=270

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

      plz can any one help me..

      I recently got this question in coding contest i was not able to solve can you please help me out.

      ques:-in a TV show game of thrones there are n houses on a battle field and n-1 bidirectional roads connecting houses it is known that you can reach any house from anywhere each house has its own army. A battle is fought between two army(houses) who have a path between their houses one army can take part in one episode of the show the winning and loosing teams can still fight but on the next day. you are the friend of the directors help him to find the minimum episodes he need to air this season and also the number of battles fought on the kth episode . in case of multiple outcomes return output which the maximum number of fights take place after.

      input1 = n input2 = k input3 = 2d a[0][i] path to a[1][i]

      Sample Tc: n = 4 arr = [[1,1,1],[2,3,4]] k = 2

      output=(3,1){3-episodes,1-battle on 2 nd day} in each episode 1 battle {1,2} {1,3} and {1,4} on second day 1 battle.

      Sample Tc: n = 4 arr = [[1,1,4],[4,2,3]] k = 2

      output=(2,2)

      on first day {1,4} on second day {4,3}and{1,2} hence minimum episodes is 3 and on second day 2 battles.

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

    Simplest counterexample:

    1 1 2
    3
    3
    2 2
    

    Greedy will give 3*3=9 but we can do better with 3*2+3*2=12.

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

    Greedy = 16 + 16 = 32

    dp = 4*3 + 4*3 + 4*3 + 4*3 = 48

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

Please tell me in C why are we initializing the mapped value of 0 as 1

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

    Because there is an empty prefix of length 0. So if any other prefix itself will be a good prefix then it should also be counted in answer, so it is paired with an empty prefix of length 0.

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

    Because consider you don't initialise m[0] with 1, then if you come across a subarray of sum = 0 for the first time then you won't consider it as your answer, but indeed it should have been counted in your answer. Hope it helps :)

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

Can someone please tell me what am I doing wrong in C? Please help!

Here is my code

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

    Your code doesn't take into consideration the subarrays that begin at the index 0. You need to initialize the answer with mp[0], not 0.

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

      Can you please explain a little bit more? I still didn't understand.

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

        Yo have to take a zero value initially ,for the empty string part. for index 0 take value as zero and build a prefix array with 1 indexing on top of that.Otherwise you woould leave that edge case from consideration for example if string starts from the index zero itself.

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

      And consider the case where "...20..."

      The prefix sum contains after first step the same value at both positions, after decresing by i two different values. But you would like to match those two positions, because it is a good subarray of size 2.

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

    You need to initialise mp[0] as 1 , not 0. This is because if the sum of a prefix of the array is 0, you need to consider this prefix. If mp[0] is initialised as 0 you will not add such prefixes to the answer. Also you need to change ++mp[pref[i]-i] to ++mp[pref[i]-(i+1)] as the array indexing is 0 based.

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

      How this code concept of ++mp[pref[i]-(i+1)] is working. What's the intution behind this logic. why we are adding (x)(x-1)/2 to the ans;

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

    actually you are missing the case of one length subarray. Basically you are taking all possible pairs(nc2) when pref[j] -pref[i] = j-i. But this doesn't count the single length case. So just add this, pref[i] = i+1 (0<=i<n), ans++;

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

If you prefer a video format for solutions, I explain A-E here (timestamps are in the description).

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

    Your video was awesome!!

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

    Awesome Work,galen_colin It's always nice to see better Coders help others who are at earlier stages

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

    Good work.Thanks!

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

    Your explanation was really amazing. Especially for problem C. Please keep up with the good work.

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

Here is a solution for Problem C using naive O(n2) solution and pragmas. https://codeforces.com/contest/1398/submission/90028294

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

how People did 1398E - Two Types of Spells with segment tree?

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

    I think seg tree + binary search in this case could be used to get the answer to: What is the sum of the K greater spell powers? (but I think using two sets is way easier)

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

I had a solution for F that also uses the harmonic numbers for $$$O(n \log n)$$$, but is otherwise very different.

First, let's get rid of $$$?$$$'s that aren't unclear at all. If a block of $$$?$$$'s is bordered by the same number on both sides, or an end of the string on one side, we will only ever assign either $$$0$$$ or $$$1$$$ to these $$$?$$$. So just do that now.

Now $$$s$$$ consists of alternating blocks of $$$0$$$'s and $$$1$$$'s with some $$$?$$$'s in between. Lets find all these blocks, and for each of them save

  • $$$l$$$, the number of consecutive $$$?$$$'s to the left of the block
  • $$$m$$$, the length of the block
  • $$$r$$$, the number of consecutive $$$?$$$'s to the right of the block

Going through these blocks left-to-right, we can greedily create $$$\lfloor\frac{l+m+r}{x}\rfloor$$$ blocks of size $$$x$$$ from a block. If we use some of the $$$r$$$ $$$?$$$'s to the right, we save that and adjust the next blocks $$$l$$$ value accordingly.

To get to $$$O(n \log n)$$$, we consider that only blocks with $$$l + m + r \geq x$$$ are interesting to us. By increasing $$$x$$$ from $$$1$$$ to $$$n$$$, we can discard blocks whenever $$$l + m + r < x$$$. Since every character is counted for at most two blocks ($$$1$$$ block for $$$0$$$ and $$$1$$$, $$$2$$$ for $$$?$$$), there is at most $$$\frac{2n}{x}$$$ blocks for every value of $$$x$$$. Thus, the overall runtime is $$$O(n \log n)$$$.

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

The tutorial of D saids that we should choose larger sticks, and the code also sorts length in decreasing order. However, sorting length in increasing order also gets AC though I cannot clearly explain it. Does anyone have explanation or proof for this?

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

    You initialized all the values of dp with $$$0$$$. Thus, you allowed it to skip any number of sticks from the prefix of any color. So as long as the suffix of each set is taken and the values are matched in the same sorted order, it gives the same answer.

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

      Thank you for your explanation and now I understand that the matched values are the same in either order.

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

For people who haven't already seen it in the contest announcement blog and are interested, here's a 3b1b-styled visual editorial for problem C using tmwilliamlin168's code. Link to comment in announcement with details.

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

In D, since the dp will iterate over all the possible combinations, why does sorting the array matter?

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

    It matters because if for the current dp state we're using some previous dp states, then we have to have already computed those previous dp states

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

    Try this test case:

    1 3 6
    2
    8 6 7
    5 2 4 1 10 4
    

    If you don't sort the arrays, your solution gives 142 and the optimal is 147, this is because it's optimal to take (10,8) and (7,5) and is impossible with the arrays unsorted (those pairs cross each other)

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

      I got it. Since we may be skipping some of the pairs, its better to sort them first so we only pick a pair of larger length. thanks.

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

I like problem E very much. The same method as solving Dynamic median number problem by two Binary Beap(s) , but not easy to discover for me. Thanks a lot.

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

.

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

Problem G — I managed to get by using bitset to get all available sizes of horizontal segments. Run time is 1900ms ... I guess I'm lucky. (Submission 89952340)

FFT is the way to go!

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

    Actually, you can get even faster bitsets which fit comfortably in the time limit. Example: 90376940.

    I think this is because the pragmas (in particular -Ofast and -unroll-loops) improve performance significantly on bitsets.

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

Dammmnnn, the answer of letter C is magnific!

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

i am not getting problem E :(

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

can someone tell me the error in my submission 90040255. I used dp , but for test case 7 the function goes into infinite recursion , but I really cannot figure out how. Thank YOu in advanced

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

plz someone help me with C problem,i couldn't understand it.

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

    I read this solution in the Announcement. But for problem C, You can subtract 1 from every term and the problem reduces to finding subarrays with sum 0 which is pretty standard.

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

Hello all,

These are the links of my two submissions for problem D : 1. Accepted Solution 2. Getting runtime error on test case 14

The only difference between the two submissions is in the size of vectors which I am using for storing the lengths of red, green, and blue sticks. In the second submission, the size of vectors exactly matches the number of sticks while in the accepted submission the size of vectors is 1 more than the number of sticks.

In the whole code, I have made sure that I am never referencing any index which is out of bounds for the vectors. But still, I am getting runtime error on test 14 when I am using vectors of the exact size.

It would be really helpful if someone can let me know the reason for this.

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

Test case : 1 10 100 1000 1001 1002 Question A As per the accepted solution this testcase should fail? I mean it's obvious we've to printf 4,5,6 here but as per the accepted solution 1,2,n is wrong. Idk where i'm going wrong with this idea. Help!

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

Why does priority queue is not working in 1398D - Colored Rectangles? we need to select two largest values each time and if all have equal then we will select two values from that queue which has larger size. Getting wrong answer on test case 7 My code submission[submission:89996393] Please help .

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

I read this solution in the Announcement. But for problem C, You can subtract 1 from every term and the problem reduces to finding subarrays with sum 0 which is pretty standard.

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

    yeah, I also did it that way.

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

      @epsilon_573, I didn't understand. You said, we need to substract 1 from every term, 6 0 0 0 5 => 5 -1 -1 -1 -1 4 now, what?

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

in problem D , .. i assigned color 0, 1, 2 and stored all in a single vector pair(as constraints were low) . sorted in reverse . then used two loops to have pairs whose color is different and not visited earlier ... where i did wrong ???

cin >> r >> g >> b; vector<pair<ll, ll>> f; for(ll i = 0; i < r; ++i) { ll x; cin >> x; f.push_back({x, 0ll}); } for(ll i = 0; i < g; ++i) { ll x; cin >> x; f.push_back({x, 1ll}); } for(ll i = 0; i < b; ++i) { ll x; cin >> x; f.push_back({x, 2ll}); } sort(f.begin(), f.end(), greater<pair<ll, ll>>()); map<pair<ll, ll>, bool> lit; ll fs = f.size(); ll ans = 0; for(ll i = 0; i < fs; ++i) { for(ll j = 0; j < fs; ++j) { if(f[i].se != f[j].se and !lit[f[i]] and !lit[f[j]]) { ans += f[i].fi * f[j].fi; lit[f[i]] = lit[f[j]] = 1; break; } } } cout << ans << endl;- ~~~~~ Your code here... ~~~~~

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

am I the only one! who is still suffering for problem E?

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

Just copied and pasted editorial solution for problem D and got TLE on 8 test case. Is it normal or I am doing smth wrong?

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

I'm still not able to get C, someone please explain it ( why do we have to intialise like m[0]=1) with an example of why it is needed It would be a great help thanks!

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

    You must have done problem of finding subarray having zero sum. It is same

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

I am not able to get the solution of 1398C, can anyone please help with C++ program?

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

Can someone please explain me the last line in the Tutorial of C. I have tried DP and Greedy but both have failed.

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

    $$$pre[j]-pre[i-1]=j-i+1$$$ is the requirement. This can be reduced to $$$pre[j]-pre[i-1]=j-(i-1)$$$ So,simply find all such (prefix sum-index) which have same value(suppose x such $$$pre[i]-i$$$ have a value r) and choose two at a time as $$${C_2}^x$$$ So, it is simply sum of all such value as $$$\sum(x*(x-1))/2$$$.

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

      I tried doing that but that logic fails at certain times. This is the code I made.

      map<ll,ll> mp;
      ll sum=0;
      for(ll i=0;i<n;i++)
      {
      	sum+=(a[i]-48);
      	mp[sum-i]++;
      }
      ll c=0;
      for(auto x: mp)
      	c+=((x.second*x.second-1)/2);
      cout<<c<<endl;
      
»
5 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

For problem D, I have submitted the following code. It gives correct answer on my local machine however it gives WA for second test case. Can someone help me with this!! Submission: https://codeforces.com/contest/1398/submission/90305945

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

Why there is a greedy tag on the problem D?

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

    Picking numbers from largest to smallest is itself greedy I guess

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

Can someone explain me how to solve c? Thank you.

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

    Do a search on youtube they have some sood interactive explanations

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

Is there any implementation of E using segment trees for finding k maximums combining the two different set of elements.

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

please can someone provide more in detailed description of problem F. i saw from others solution that there are many ways to deals with it. can any one provide the intuition behind their solutions ?

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

Please can anyone tell why we need to sort the arrays before applying dynamic programming in problem D. Since we are calculating all the states of the DP isn't it unnecessary to sort them? However I get WA on test case 5 without sorting. Thanks in advance.

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

    It's necessary to sort as we need to proceed greedily. product of two numbers a*b is maximum when a is closer b. So, thereby sorting we are ensuring that we proceed on path of maximum value.

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

What means ld x = .0?

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

The code of problem E is so short that it really does shock me.

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

Div. 2 Educational round with FFT task? Are you kidding?

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

My recursive solution for problem D : https://codeforces.com/contest/1398/submission/90893065

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

A simple solution to c
https://codeforces.com/contest/1398/submission/91039888
just keep track of the states you have seen upto now and if the same state has been seen we add all the subarrays that can be formed with that index as the ending point

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

Didn't get problem G at all :( It makes me afraid!

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

problem E:Can anyone tell me what's wrong with this code as this is giving TLE on test 47. TIA.

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

I am getting wrong answer on test case 6 for PROBLEM D.... SUBMISSION Can someone please me what is wrong??

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

I think there is a typo in the problem E editorial that confused me a little.

"All we have to do it maintain the set of x largest by power spells"

Instead, i think it should be.

"All we have to do it maintain the set of x largest power spells"

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

C. Good Subarrays Video tutorial: https://youtu.be/ywZ5zBacczI