Блог пользователя Bahamin

Автор Bahamin, история, 15 месяцев назад, По-английски

✨ Hope you enjoyed the problems! ✨

2127A - Mix Mex Max
Idea: Bahamin
Preparation: Hamed_Ghaffari

Hints
Solution
Code

2127B - Hamiiid, Haaamid... Hamid?
Idea: _R00T and Error_Yuan
Preparation: _R00T and Hamed_Ghaffari

Hints
Solution
Code

2127C - Trip Shopping
Idea: Bahamin
Preparation: Bahamin and _R00T

Hints
Solution
Code
Bonus

2127D - Root was Built by Love, Broken by Destiny
Idea: _R00T and Bahamin
Preparation: _R00T and Hamed_Ghaffari

Hints
Solution
Code

2127E - Ancient Tree
Idea: Bahamin
Preparation: Bahamin and Hamed_Ghaffari

Hints
Solution
Code

2127F - Hamed and AghaBalaSar
Idea: Hamed_Ghaffari and _R00T
Preparation: Hamed_Ghaffari

Hints
Solution
Code

2127G1 - Inter Active (Easy Version)
Idea: Ali_BBN
Preparation: Hamed_Ghaffari and Ali_BBN

Hints
Solution
Code
Bonus

2127G2 - Inter Active (Hard Version)
Solution & Analysis: LMydd0225 and Error_Yuan
Preparation: _R00T and Hamed_Ghaffari

This version used an almost completely different approach from G1. Here's the step-by-step analysis:

Analysis (Steps & Hints)
Code

2127H - 23 Rises Again
Idea: sweetweasel and eren__
Preparation: Hamed_Ghaffari and _R00T

Hints
Solution
Code
Apology
  • Проголосовать: нравится
  • +243
  • Проголосовать: не нравится

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Auto comment: topic has been updated by Bahamin (previous revision, new revision, compare).

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Fast editorial!

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +9 Проголосовать: не нравится

why my standing/leaderboard still broken, any ideas?

Edit: I no longer have this problem

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +7 Проголосовать: не нравится

Thank you for preparing such a clear and detailed editorial. It was truly insightful

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

although I attempted only 4 problems I enjoyed the contest.. thanks for fast editorial

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

GPT able to solve H....highly unexpected....

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

can anyone clarify where i have done wrong for 2127B - Hamiiid, Haaamid... Hamid?_

// this is code
void inc(int pos, int d) {
    for (; pos < n; pos |= pos + 1)
        f[pos] += d;
}

code:

import java.util.*;

public class Main { public static int solve(Scanner sc) { int length = sc.nextInt(); int position = sc.nextInt() — 1; String path = sc.next();

int leftWalls = 0, leftEmpty = 0;
    int rightWalls = 0, rightEmpty = 0;

    for (int i = 0; i < position; i++) {
        if (path.charAt(i) == '#') leftWalls++;
        else leftEmpty++;
    }

    for (int i = position + 1; i < length; i++) {
        if (path.charAt(i) == '#') rightWalls++;
        else rightEmpty++;
    }

    if (leftWalls == 0 || rightWalls == 0) {
        return 1;
    }

    int fromLeft = leftWalls + Math.min(rightWalls, leftEmpty);
    int fromRight = rightWalls + Math.min(leftWalls, rightEmpty);

    return Math.min(fromLeft, fromRight) + 1;
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();

    while (t-- > 0) {
        System.out.println(solve(sc));
    }
}

}

  • »
    »
    15 месяцев назад, скрыть # ^ |
    ← Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Maybe the best option to escape is choose a direction and don't change it.

    Try this test case:

    testcase

    The answer should be 2 because Mani can choose the 3rd grid and block it at first, and Hamid need to use 2 days anyhow.

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +22 Проголосовать: не нравится

Easy backtrack solution for H (not as easy as the random shuffle solution, but it did not require any parameter tweaking so I will take it as a win)

We will go in increasing order of vertice id, adding new edges from the current vertice to ones with larger id. Notice that if we can add an edge, it is never beneficial to not do so. That's it.

Spoiler
»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

A, B, C and D are all high-quality! To my surprise the E need virtual tree (or DSU on tree according to the discuss of other participants). Unfortunately I mastered none of them.

Perhaps they are commonly used in advanced problems. Anyway, thanks for sharing these amazing problem and I think I need to learn them now. :D

»
15 месяцев назад, скрыть # |
← Rev. 4  
Проголосовать: нравится +15 Проголосовать: не нравится

For problem E, I wasn't aware of virtual tree, and I think the "other solutions" include my small-to-large solution.

We can run DFS on the tree and retrieve the set of colors from each subtree, but only the reference to the sets instead of copying it every time. Determining the color of this node is identical to the editorial's strategy. By merging all the sets into the largest set among them, the total number of insertions during merging is $$$\mathcal{O}(n\log{n})$$$ and each insertion takes $$$\mathcal{O}(\log{n})$$$. Therefore, this solution runs in $$$\mathcal{O}(n\log^2{n})$$$ time in total.

My submission: 332889063

  • »
    »
    15 месяцев назад, скрыть # ^ |
    ← Rev. 3  
    Проголосовать: нравится 0 Проголосовать: не нравится

    I was actually able to do the part you have mentioned. Can you please help me with the part on how to apply color to non-colored(unknown colored nodes) such a way final answer doesn't increase ? I wasn't able to do this part. :( .

    My code .
    • »
      »
      »
      15 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится +1 Проголосовать: не нравится

      You just need to keep these nodes uncolored, and after this phase, you can run another traversal on the tree which propagates the parent's color to its children, and color the uncolored nodes with this color.

      • »
        »
        »
        »
        15 месяцев назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        Same Thought bro

      • »
        »
        »
        »
        15 месяцев назад, скрыть # ^ |
        ← Rev. 4  
        Проголосовать: нравится 0 Проголосовать: не нравится

        damn :| . I thought about this approach as well.

        If the parent node isn't colored, then what to do ? ( I tried started propagating one of the child's color to its parent. ).

        Above approach worked until, test case that has zero colored nodes.




        0 / | \ 2 2 2 k = 2 , Now how to decide which color to give to the root node ? ( I started propagating one of the non-zero child's value to parent. Then I got stuck at below... ) 2 / | \ 0 0 0 ---- If was passing value form parent to child, second test case fails, and vice versa first one fails. May be I should have done both ...
        • »
          »
          »
          »
          »
          15 месяцев назад, скрыть # ^ |
          ← Rev. 2  
          Проголосовать: нравится 0 Проголосовать: не нравится

          If you followed these "small-to-large" steps, then the only uncolored nodes can only be the root of an uncolored subtree, in which case you can just start the traversal from the root and all nodes will be naturally colored with the parent's color. If the whole tree is uncolored, you can just color the whole tree with color $$$1$$$ and then the cost is $$$0$$$.

        • »
          »
          »
          »
          »
          15 месяцев назад, скрыть # ^ |
          ← Rev. 2  
          Проголосовать: нравится +1 Проголосовать: не нравится

          In the first case, you can set the root's color as $$$2$$$ during the "small-to-large" phase (children to parent propagation). In the second case, you leave the uncolored nodes in this phase, and during the second traversal you propagate the root's color ($$$2$$$) to its children (parent to children propagation).

  • »
    »
    14 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Hi, I just solved this problem with the same approach, but I'm concerned about one thing. I store the set of colors for every node in the tree, and don't delete them after I use them. Still, my code only takes around 80MB of memory even though I think its memory complexity is O(n^2). Could you explain why this is the case?

    My code: 339460078

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

god DAMN.

Light speed editorial

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +32 Проголосовать: не нравится

solution outline for E without mentioning virtual tree:

iterate nodes in dfs order (i.e. pre-order traversal). i claim that we can assign colors to nodes where $$$c_u=0$$$, such that we don't increase the number of "doomed" vertices (as mentioned in the editorial's hints).

let the "color set" of $$$u$$$ be the set of pre-assigned colors in subtree $$$u$$$. Note that we can obtain this information via small-to-large merging.

For each node $$$u$$$, consider the number of colors that appear in multiple colors sets. If there is exactly 1 such color, let it be $$$s_u$$$, otherwise let $$$s_u=-1$$$.

  1. for nodes where $$$s_u\neq -1$$$ and $$$c_u=0$$$, we want to set $$$c_u:=s_u$$$.

  2. for nodes where $$$s_u=-1$$$ and $$$c_u=0$$$, we set $$$c_u:=c_{fa_u}$$$ where $$$fa_u$$$ is parent of $$$u$$$ or $$$c_u:=1$$$ if $$$u=1$$$.

Notice that in the first case, we don't add doomed vertices since $$$u$$$ doesn't become doomed and none of $$$u$$$'s ancestors can become doomed (they must have been doomed already due to occurences of $$$s_u$$$ in $$$u$$$'s subtree).

In the second case:

a) If all of u's children's color sets are disjoint, then $$$u$$$ doesn't become doomed, and none of $$$u$$$ ancestors can become doomed: $$$fa_u$$$ can't become doomed since $$$c_u=c_{fa_u}$$$, and higher ancestors can't become doomed (otherwise $$$fa_u$$$ would've already made them doomed).

b) If there are >=2 colors that appear in u's children's color sets, then u was doomed already. Then by the same argument in a), we can show that none of u's ancestors become doomed.

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Cant understand why i'm getting WA on test 5 in problem D. Probably missing some case where i have to multiply 0. Can someone help? 332893206 Update: NVM got it.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +67 Проголосовать: не нравится

A better solution for H:

Take out a single biconnected component and it's easy to see that any node in this component can have a maximum degree of $$$3$$$. Otherwise any $$$2$$$ neighboring edges from this node can form a cycle (since the graph is biconnected), leading to at least $$$6$$$.

It's also easy to see that there are at most $$$4$$$ nodes with degree $$$=3$$$ in this component, otherwise there will be at least $$$2^3=8$$$ cycles. Since at the end we keep at most $$$2$$$ edges from any node, we can choose an unused edge and delete it from a node with degree $$$=3$$$ without changing the answer. Since we don't know yet which edge is used, we can only brute force to try all possibilities in $$$O(3^4)$$$ time.

Now we have a graph where every node has degree $$$\le 2$$$, which means every component is either a cycle or a chain, and we can do standard dp on this graph to obtain the answer. The dp can be done in $$$O(n)$$$ time if implemented carefully.

Back in the original problem, we build the block-forest of the original graph and do a tree dp on it. For every biconnected component, we do the brute force and dp above with the answers obtained in the subtrees.

The total complexity is $$$O(3^4 n)$$$ (or maybe $$$O(3^4 n^2)$$$), which is way much smaller than the author's solution. The downside is that this solution is very very hard to implement and hard to debug. I wrote ~7K and took >2h to debug it, so this solution won't pay off in the contest, unfortunately :(

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +59 Проголосовать: не нравится

I think for H, you should probably assume that a backtracking/heuristic solution exists, since the limits are really small to allow these sorts of solutions. At least testers should have found a better solution and buff $$$n$$$.

Some alternative solutions:

Matching (+some rant)
DP
Better DP
»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

I present to you the dumbest solution for A. I noticed the pattern... but didn't realize I could just code it like that

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int mex(int a, int b, int c){
    for(int i=0; i < a + b + c + 2; ++i){
        if(i != a && i != b && i != c){
            return i;
        }
    }
}

int max(int a, int b, int c){
    return max(a,max(b,c));
}

int min(int a, int b, int c){
    return min(a,min(b,c));
}

int main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        vector<int>a(n);
        for(int i=0; i < n; ++i){
            cin >> a[i];
        }
        int num = 1;
        for(int i=0; i < n; ++i){
            if(a[i] != -1){
                num = a[i];
            }
        }
        for(int i=0; i < n; ++i){
            if(a[i] == -1){
                a[i] = num;
            }
        }
        bool ok = 0;
        for(int i=0; i < n - 2; ++i){
            if(mex(a[i],a[i + 1], a[i+2]) == max(a[i],a[i + 1], a[i+2]) - min(a[i],a[i + 1], a[i+2])){
                ok = 1;
            }else{
                ok = 0;
                cout <<"NO"<<endl;
                break;
            }
        }
        if(ok){
            cout << "YES"<<endl;
        }
    }
}

✅ AC on 332878042

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

is there a DFS based approach for D ?something where we count child with deg one and use factorial based logic after that ??

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

I like the problems, especially E. Thanks for the contest!

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Thank you for the great problem and lightning fast editorial! But imo, B is a little hard for Div.2B

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +10 Проголосовать: не нравится

Excellent contest, problems are intreresting indeed

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +9 Проголосовать: не нравится

Thanks the authors for the quality of the contest! Great solutions for B and C!

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Although I faced lots of difficulties, it really taught me something I didn't know. Thank you for the contest.

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Problem C is very good and interesting but for me problem D much more easier than C.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

what is "Stars and bars" in editorial of F?

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I learned a lot from problem F. I like the solution!

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +6 Проголосовать: не нравится

The solution of C provided is amazing. I have already made out case 1 and case 2 myself. How can we finally know using the intersection of intervals to figure out case 1 or case 2?

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

D is a very good problem

»
15 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +15 Проголосовать: не нравится

All of the problems were good. All of the solutions are verrry beautiful but unfortunately, H was accepted with an easy way. but in the end, it was a very, very good contest and thank you all.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

The symmetry in F for determining the contributions of the subtracted elements is very nice! Unfortunately during the contest, I found the following on upper-bound integer sums, and the formula looked $$$O(n)$$$ so I thought there must've been a different thinking path. It also looked intimidating lol, but it turns out that figuring it out yourself isn't too difficult.

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

got a randomized solution for G2 with $$$n (\log n + 1)$$$ queries for large $$$n$$$($$$n \gt 14$$$), 333152719. Though for small $$$n$$$ I have no idea how to solve it quickly, some randomised approach gave me the same 8n, so this solution makes no more than 8n queries

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

in problem C I do not understand where the solution takes k(the number of rounds) into consideration. I understood the solution as assuming we have infinite number of rounds to reorder the arrays and add gains as we wish. can someone please explain?

»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится +22 Проголосовать: не нравится

For problem H, we found an algorithm with $$$O(2^5n)$$$ time complexity.

https://codeforces.me/contest/2127/submission/333483386

»
14 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In the editorial's solution for B, there seems to be a typo; it says:

So the answer becomes either $$$\min((x−1)+1,n−R+2)$$$ or $$$\min(L+1,n−(x+1)+1)$$$

when it should say:

So the answer becomes either $$$\min((x−1)+1,n−R+2)$$$ or $$$\min(L+1,n−\underline x +1)$$$

which is correctly stated in the final solution:

$$$\max(\min(x,n−R+2),\min(L+1,n−x+1))$$$

»
14 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can someone pls help with checking why this is tle,

problem E,

https://codeforces.me/contest/2127/submission/336913209

»
14 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem D: Why does this solution get WA at test 2 ?? I used small to large merging on trees to store the distinct colors in subtrees of all nodes. My submission: 338256378

»
4 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For C, My approach was to first compute the midpoint (mean) of each pair and store it along with its original index. Then I sorted these midpoints. For every adjacent pair in the sorted order, I retrieved their original indices, removed their original contribution from the total sum, and added the maximum possible contribution after rearranging the four endpoints. I updated the answer with this new total. Finally, I output the maximum of the initial sum and all updated sums.

380163784