36champ's blog

By 36champ, 5 months ago, In English

Problem A — Convergence

Problem Idea: 36champ
Solution Idea: 36champ

Hint 1
Solution
Code
Rate The Problem!

Problem B — Cake Leveling

Problem Idea: 36champ, Proof_by_QED
Solution Idea: Proof_by_QED

Hint 1
Hint 2
Solution
Code
Rate The Problem!

Problem C1, C2 — Seating Arragement

Problem Idea: 36champ
Solution Idea: 36champ, Proof_by_QED

There are two solutions to this problem presented below.

Hint 1
Solution (C1)
Code (C1)
Rate The Problem! (C1)
Hint 2
Hint 3
Solution (C2)
Code (C2)
Alternate Hint 1
Alternate Hint 2
Alternate Solution
Alternate Code
Rate The Problem! (C2)

Problem D — Magical Tiered Cake

Problem Idea: 36champ
Solution Idea: 36champ

Hint 1
Hint 2
Hint 3
Solution
Code
Rate The Problem!

Problem E — Snaking Arrangement

Problem Idea: 36champ
Solution Idea: 36champ

Hint 1
Hint 2
Solution
Code
Rate The Problem!

Problem F — The Cake Is a Lie

Problem Idea: 36champ
Solution Idea: 36champ

Hint 1
Solution
Code
Rate The Problem!
  • Vote: I like it
  • +97
  • Vote: I do not like it

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

second

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

Thank you for the fast editorial

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

good contest. relatively fast editorial

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

misjudged the constraints on D

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

Why does ternary search not work on C2?

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

Can we solve C1 by dp

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

Im very sad that i did so poorly in a very cool contest, nonetheless congrats to the writers for such an amazing contest

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

I think I came up with the weirdest solution for C1 and C2 out of all the submissions. Glad that it worked :)

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

    What was your solution? If you don't mind sharing?

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

      First I precomputed the next occurrence of an introvert for every index of the string and stored it.

      Then I iterated through the string always seating an introvert and extrovert whenever I could. For the case of ambivert there were two cases:

      1. If the ambivert was only able to become an introvert or only able to become an extrovert, make him so. Never any loss in that.
      2. If an ambivert is able to become both, again two cases here:

      2.a) If the remaining count of unseated tables is greater than the remaining count of introverts, one table will go untouched, so it is clearly better for an ambivert to act as an introvert and open it.

      2.b) If the remaining count of unseated tables is less than or equal to remaining count of introverts. This means that all of the tables will be opened regardless because there are enough introverts in the string. So, extroverts/ambiverts are more valuable now. So, we will make the ambivert an extrovert by default with the exception of one case, when, till the next occurrence of an introvert from that index, there are more extroverts/ambiverts than the amount of extroverts that can be currently seated(done using the pre-computed array). Because in that case, making our ambivert an extrovert wouldn't matter as some extroverts would be kicked out of the party before the next introvert occurs.

      Hope that made sense, in short, we can just iterate through the array and decide on the fly whether an ambivert needs to be an extrovert or an introvert, and solve this problem in O(n).

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

        Did you solve it in order of N time ?

        I also had an intution but getting wrong answer everytime loll

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

        And How did you decide that the amibivert is going to be an introvert or an extrovert ?

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

        Same soln

        Exactly same soln!

        Just that I didn;t think about that one exception case in 2.b) and just assigned the ambivert as an extrovert :(

      • »
        »
        »
        »
        3 months ago, hide # ^ |
         
        Vote: I like it -8 Vote: I do not like it

        I think the explanation for Case 2.b is missing something.

        Consider this test case:

        n = 16, x = 5, s = 3 IEIIAEEEIEEEEEEI

        At the A, there are 2 unopened tables and enough free seats before the next I, so according to the explanation, A should act as an extrovert.

        However, if A acts as an extrovert, the last I opens the final table at the very end of the sequence, so its remaining 2 seats are wasted because there are no people after it. If A opens the table earlier, those seats can be used by the later extroverts.

        I tried this test case with your code, and it outputs 13, while I believe the correct answer is 15.

        Am I missing some invariant, or is the explanation for Case 2.b incomplete?

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

My post contest discussion stream for A-D problems here

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

by when will ratings update?

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

C1 will be much easier if I don't overthink

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

    can you help me to figure out the c1 question: test case : 11 3 1 4 1 5 9 2 6 5 3 5

    how it's answer comes 5, by my calculation the minimum number group call would be 6.

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

is there any way to do C2 with some sort of exchange argument? or we stored "unplaced ambiverts" so far and when we hit an extrovert / introvert we can retroactively place them

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

Can someone describe the solution for E in more details?

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

    I also find the editorial answer insufficient. The first thing the solution should do is create a anti-diagonal array. In the given example it will be

    anti[0] = {3}
    anti[1] = {-1, 3}
    anti[2] = {-1, 3, -1}
    

    For each antidiagonal transition we find the number of ways a new snake could be added and multiply the answer by it. For a new snake that is present the answer is simply 1. Otherwise we find the number of ways it is possible to add element to anti[i] to obtain anti[i+1]

    If anti[i][j] != -1 && anti[i][j] == anti[i+1][j] the newly added ? is at place >j

    If anti[i][j] != -1 && anti[i][j] == anti[i+1][j+1] the newly added ? is at place <=j

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

could someone please tell me, why it is failing for problem B

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<long long >a(n);
        for(auto & it: a)cin>>it;
        vector<long long>ans;
       long long sum= 0;
      long long maxHeight=a[0];
        for(int i = 0;i<n;i++){
            sum+=a[i];
            if(a[i]<maxHeight){
                maxHeight= (long long)(sum/(i+1));
            }
            ans.push_back(maxHeight);
        }
        for(auto & it :ans){
            cout<<it<<" ";
        }
        cout<<"\n";
    }
}


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

why the answer in the problem A with sorting approach is cnt/2 not ceil(cnt/2)?

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

Can someone explain C2 in more detail? I'm not understanding either of the alternate solutions. I bricked it lol

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

    Initially, treat all ambiverts as extroverts. That is, if the next friend is an introvert, place them at an empty table, and if the next friend is an extrovert or an ambivert, place them at a partially-full table.

    Now consider the case where our next friend is an extrovert, but we have no partially-full tables to assign them to. It would have been nice if we had the foresight to place a previous ambivert instead at an empty table, opening up a seat for this extrovert, and also creating a new partially-full table for any future extroverts. This is possible if and only if there exists a previous ambivert which we treated as an extrovert, and there is at least one empty table remaining. Then, the optimal strategy would have been to place that previous ambivert at an empty table, and now once we arrive at the present moment, we'd be able to seat this new extrovert.

    Of course, treating this ambivert as an introvert (to seat this extrovert and future extroverts) could mean we're unable to seat a future introvert. Fortunately, extroverts and introverts have the same weight with regards to the final answer, so this doesn't matter (plus one extrovert + minus one introvert gives the same final answer).

    This approach can be implemented in $$$O(N)$$$ time. For details, see my submission.

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

      Thanks, yes this regret greedy makes sense, unfortunate that I did not think of it tho lol.

      I'm still not understanding their maintain min and max number of tables allocated solution though, I will think on it

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

      Amazing!

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

I think I noticed a greedy for B. Somebody else did? Once I have it done, I will submit it :)

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

I solved Problem E by only observing the samples and guessing the conclusion. Is there a detailed proof?

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

Tutorial for problem only gives a hint about anti-diagonals, which is cool, however there is no explanation of how to calculate the answer. Solution code is quite long, but in tutorial it's covered by just:

This can now be solved comfortably in O(n2) or in O(n) with more optimization. It's nice to know that "This can now be solved", but how to solve it? :)

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

https://codeforces.me/contest/2232/submission/376691826

can someone please expplain why this fails, the main diff is in absence of min(ans, sum/i+1) i do not get when my logic fails.

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

    Amazing contest!

    Refering to your question, I dont really know why? The logic is simmilar. The most i can guess is off-by-one errors. The logic really needs to be sum += a; h = min(h, sum / i); and i to go from 1 to n;

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

      its because averaging out is a wrong soln when its larger than the current height. 2 6 1 is a gud example. avg is 3, but i cannot do that, frost cannot be shifted back

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

can we sovle c1 by couting number of extroverts and introverts after every A using prefix approaoch

and then making good decision every time an ambivert comes

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

very beautiful problem D!

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

Also, there is another greedy approach for C2.

https://codeforces.me/contest/2232/submission/376748313

that's a shame that I submitted this in the last 30 secs and because of not getting free variable in long long i got WA on pretest 18 :)

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

the second one is a nice problem

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

    can u please check out my query, i did not use min, but i am not getting how do we come to think of min, what was ur thought. i thought, if the next val if smaller than my ans, i will set the maxH to average. else i will cut to the ans i am already having.

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

      You must use min think about 4 100000 0 100000 10000 example. While I was solving the question İ thought about a similar testcase and observed that if the min of the prefix is smaller than the average of the prefix then the answer is the min

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

        fair, it took a few min to get the soln, 2,6,1 is a good testcase that helps to understand.maybe the bad assumtipn was that if current height is less than prevH, i gotta distribute it. i cud have extras, and the math will just distribute it backwards too, which is against the rules. averaging out works, only if its smaller than prevH. it can be larger becaue u can have extras. like in 2,6,1 i get till 2,2 but 4 is xtra, and i cannot give it to i = 0.

        but man it was tricky to get this during contest, do u think about extreme testcases, is it natural to not get the complete picture initially, how did u get ahead of this prob

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

cake leveling solo leveling reference??

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

got WA at C2 because of $$$x \cdot s \gt 2^{31}-1$$$ and overflowed(:lmao)

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

Guys, can somebody figure out the problem with my code (C2 problem), I am not able to comprehend the case where this could go wrong, if anyone is interested please help find the issue and share with me. I get wrong answer on test 16 some case.

https://codeforces.me/contest/2232/submission/376771561

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

    There is a testcase that leads to WA (found with the help of AI, which means I don't know whether it is the right one makes your code failed)

    1 18 3 7 EEIAAEEAAAEIIIAAEA

    expected: 14 actual: 15

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

      Thank you, understood the issue with the help of the case.

      I'm assigning Ambiverts to new tables first but the problem is if lets say one table is full, and 2 other tables have ambiverts each, I'm assuming I can replace the ambivert with an introvert by moving it to an already partially filled table, (table 2), but I never marked that the ambivert on table 3 has nowhere to go except only table 2, after moving it to table 2, I don't have a slot for next introvert. My ambiguous counter says I still have a replaceable ambivert but I don't.

      https://codeforces.me/contest/2232/submission/376791142

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

There's an alternative nlog(n) solution to C2 using a segment tree which can be optimized to linear time. Check my submissions.

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

I think I found a great solution for both the C1 and C2 problems:

  • The basic idea is to traverse through the string, and we check for each character whether we can get a seat for the person or not, if we can we simply increment the final ans variable by 1.
  • Three extra variables we use are : num, ans and cea, all initialised to zero initially. num represents the number of tables currently containing nonzero persons, and ans represents the total people that can be seated. cea represents the number of ambiverts that are seated as extroverts.
  • We make an assumption that we generally make the 'A's seat as an 'E', because the occupancy for 'E' is more in number. Thus in general, we increment cea everytime we encounter an 'A'.

  • Now, while traversing through the string, we only get into trouble finding a seat for 'E', if either num == 0 or if ans/n == s (All the tables are fully occupied).

  • In this case we check whether we previously have an 'A' that was seated like an 'E'. If yes, we simply give that 'A' a completely new table, and make 'E' sit on 'A's initial position. We also decrement nea by one in this case. Notice that since we always make 'A' seated as an 'E', we also need to check this condition whenever we encounter an 'A'

  • An introvert can always be seated, unless the value of num == x.

  • The traversing of the string can be broken once we get a value of answer which is greater than x*s, in which case we print the answer as x*s. Otherwise print the final ans after traversing the complete string. This way we can get the ans directly in a single pass in O(n) time.

  • This is my code for both C1 and C2 : 376854024 I was working on these lines during the contest, but sadly couldn't complete the code and hence got only 2 AC submissions in the contest :(

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

I really liked problem D, was a nice twist on tower of hanoi. But i was wondering, whats the optimal algorithm, that moves all the layers in the fewest moves. The solution was within 2^n — 1 moves but not the most optimal, it would do extra work. So a problem variation that asks for the fewest possible moves would be interesting. I thought of this, but it was very hard to solve myself. I'm very curious to find the solution.

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

    Boy, you and I ruined the 67 comment count.

    As for your question, it would probably have been much much harder. The whole idea that we could move MORE than 3 rings(max) at once was pretty complicated already and required some pretty nieche greedy tricks already.

    It would probably be the same thing only that more restrictions on HOW and IN WHAT ORDER you decide to move them. I am courious how the code might look!

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

    The algorithm mentioned is the editorial will produce the shortest possible sequence

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

thanks !

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

The (i+2)-th ambivert is seated when they are extroverted, and is still seated when they are introverted.

Can someone explain what does this mean in the solution C2 proof.

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

    For $$$f(i)$$$, the first $$$i$$$ ambiverts are seated like they are introverted. (I just say the first $$$i$$$ ambiverts are introverted for short.)

    Therefore, the difference between $$$f(i + 1)$$$ and $$$f(i + 2)$$$ is how the $$$(i + 2)$$$-th is acting (extroverted for $$$f(i + 1)$$$ and introverted for $$$f(i + 2)$$$).

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

In Problem C2, Found the following way to think about the diminishing returns logic more intuitive -

Let f(i) = maximum people seated when the first i ambiverts are forced to act as introverts, remaining ambiverts act as extroverts. Let T_i denote this timeline.

Claim: if f(i) > f(i-1), then f(i-1) > f(i-2).

Note that T_i and T_{i-1} are identical up to the i-th ambivert, they only diverge there. This means just before the i-th ambivert arrives, T_{i-2} has exactly one fewer active table than T_{i-1}.

Since f(i) > f(i-1), forcing the i-th extra table was beneficial. This means at some future point j (post the ith Ambivert), all of T_{i-1}'s tables are completely full. At that same point, T_{i-2}'s tables are also completely full — the same stream of people filling fewer tables fills them just as easily.

So at point j, T_{i-1} has m fully filled tables and T_{i-2} has m-1 fully filled tables, and both face an identical remaining suffix from j onward.

From here it is obvious — one extra fully filled table with the same suffix ahead is strictly better, that is s extra people already seated. The only escape would be if the venue hits full capacity x*s, but f(i) > f(i-1) guarantees it didn't — otherwise T_i couldn't have improved on T_{i-1}.

So the advantage is permanent. Hence f(i-1) > f(i-2).

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

problem E is really amazing

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

I found a solution with fewer steps than the standard recursive approach for problem D, but it got a wrong answer. Is there any case where a "faster" (shorter step count) solution is invalid?

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

C also solved binary search[submission:377858111]

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

why this if else is not trivial for me :(


if (a[p-1]==0){ shift(shift ,i,other,p-1); id.push_back(p); x.push_back(i); y.push_back(j); // 1 shift(shift,other,j,p-1); } else{ shift(shift,i,other,p-1-a[p-1]); // shift 2 ^ (k-1)-1 id.push_back(p); x.push_back(i); y.push_back(j); // 1 shift(shift,other,i,p-1-a[p-1]); // 2^ (k-1)-1 shift(shift,i,j,p-1); // 2^(k-1)-1 }
»
5 months ago, hide # |
← Rev. 21  
Vote: I like it 0 Vote: I do not like it
As the editorial seems insufficient, here is another way to think about E:
»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Man, The Problem B is easier than A

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