yse's blog

By yse, 11 months ago, In English
Rating Predictions

Thanks to reirugan for helping proofread the editorial!

2117A — False Alarm

Author: yse

Hint
Solution (written by yse)
Code (C++)
Rate the problem!

2117B — Shrink

Author: yse

Hint 1
Hint 2
Solution (written by yse)
Code (C++)
Rate the problem!

2117C — Cool Partition

Author: yse

Hint 1
Hint 2
Solution 1 (written by yse)
Code (C++)
Solution 2 (written by reirugan)
Code (C++)
Rate the problem!

2117D — Retaliation

Author: yse

Hint
Solution 1 (written by yse)
Code (C++)
Hint 1
Hint 2
Solution 2 (written by yse)
Code (C++)
Rate the problem!

2117E — Lost Soul

Author: yse, cry

Hint 1
Hint 2
Solution (written by yse)
Code (C++)
Rate the problem!

2117F — Wildflower

Author: yse

Hint 1
Hint 2
Solution (written by yse)
Code (C++)
Rate the problem!

2117G — Omg Graph

Author: Intellegent

Hint
Solution (written by Intellegent)
Code (C++)
Rate the problem!

2117H — Incessant Rain

Author: cry

Hint 1
Hint 2
Solution (written by cry)
Solution (written by LMeyling)
Code (C++)
Rate the problem!
  • Vote: I like it
  • +87
  • Vote: I do not like it

»
11 months ago, hide # |
 
Vote: I like it +25 Vote: I do not like it

omg

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

super fast editorial

i think the people who rated E high solved it using some crazy DP idea

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

Thanks for fast editorial

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

fast editorial!

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

Found F to be easier than E . Anyone else with the same opinion ?

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

Can $$$C$$$ be hacked with collisions?

»
11 months ago, hide # |
 
Vote: I like it +43 Vote: I do not like it

I forgot I named my dijkstra function that.

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

Can someone explain the case where the tree has 2 leaves, more clearly please

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

    When a tree has 2 leaves we can first ignore the leaves and just find the length of the root till some vertex where the tree splits to the 2 leaves. From that we can get 2^(the length of the root till said vertex) as all good possibilities. Then for the leaves, to make sure none of the sums over lap, and with the only possible values being 2 and 1 the only way to do that is to have one leaf be all odds (1,3,5...) starting with one and then continuously having 2s, and the other having all evens, starting with 2 instead. If one leaf is longer than the other however, we can essentially do the same thing we did were we get the difference between the leaves lengths and then get 2^(the difference). the difference-1 comes from the fact that the leaf with the odds, will be forces to have another 2 as if not it will overlap with the evens.

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

      the difference-1 comes from the fact that the leaf with the odds, will be forces to have another 2 as if not it will overlap with the evens. More on this ?

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

        For instance say we have a tree with two leaves that are length 5 and 3, from where they branch off from the line from the root. Say the one of length 3 is the evens having the subtree sums values 2+x,4+x,6+x; where x is whatever the sum is from where the tree branches off into the two branches till the root. This makes forces the sums of the other leaf to be 1+x,3+x,5+x if they where both of length of 3. However now with the extend length of the leaf of length 5 we cannot have the '4th' node be the value 1, as that would make the leafs values be 1+x,3+x,5+x,6+x where 6+x is a duplicate. First let o=2^(length of branch off to the root) as those values can always be either 1 or 2, and won't have overlap, and difference equal the the difference in length of the two leaves. Now the answer would be equal to (o * 2^(difference)) + (o * (2^(difference-1))), as the 'odd' or 'even' leaves could be either one, and when the shorter one is the 'even' leaf, there is an extra node that is forced into being 2, which is where we get the differece-1 from.

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

          Great. Thanks

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

          I was slightly confused regarding it, could you pls clarify if my way of thought is correct?

          Assume I take this example

              a
              |
              b
             /\
            c  e
           /    \
          d      f
                /
               g
               \
                h
          

          Now assuming the case for which 2^(y-x) fails by assuming d = 2 and h = 1.

          For sure c = 2, g = 2

          Now applying 2^(y-x) logic,

          since I am flexible lets assume f = 1

          Here I notice that subtree sum of c (c--d i.e. 2+2 = 4) and subtree sum of f (f-g-h i.e. 1+2+1=4) hence SAME hit, which is not allowed.

          Hence increasing the fixated Ness/locking another node to 2 in longer depth leaf path ensures until I reach LCA via the smaller depth leaf path(bottom to up travel) for such case, we never find equal sum for any subtree sum since f-g-h fixed values ensure the greater depth leaf's path's(or rather some node in the path) subtree sum is always greater than the max subtree possible by the smaller depth leaf's path's node (Here c-d, ie. the immediate node below the LCA ) without merging to other since LCA will be reached to improve sum.

          Also since subtree of LCA means entire inverted Y shape is part of one subtree so no comparison (i.e. c,d,e,f,g part of one single tree).

»
11 months ago, hide # |
 
Vote: I like it +14 Vote: I do not like it

I supposed H has a solution of space $$$O\left(n\right)$$$, and an obvious online solution using $$$n$$$ segment trees requires $$$O\left(n\log n\right)$$$ space, so I simply changed segment trees to balanced trees and then got an online solution of time complexity $$$O\left(n\log n\right)$$$ and space complexity $$$O\left(n\right)$$$

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

    If you use only one segment tree the space complexity is also $$$O(n)$$$.

    Edit: sorry i misunderstood your point.

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

wrote down so many equations for D but i could only get like half the information needed. wish i was better at maths T-T

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

I tried to solve D, using the fact that when a[n-1] = 2*a[n], I will do operations only of type 2, and the conditions, a[n-i] = (n-i+1)*a[n] should hold for all i, so first I calculated how many operations of first type are required to get a[n-1] = 2* a[n], apply that much operations of 1st type to the entire array, and then check the condition which I told.

But I am getting wrong answer on test 3, can somebody please look into my submission and tell the probable error, https://mirror.codeforces.com/contest/2117/submission/323482331

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

    Consider the following test case:

    1
    2
    1 5
    

    which your solution gives YES. Applying operation one 3 times makes it -2 -1 which satisfies a[n-1] = 2*a[n] but requires applying operation two -1 times which can't be done.

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

https://mirror.codeforces.com/contest/2117/submission/323528702

Someone please check if this solution for problem E is hackable. Thanks!

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

https://mirror.codeforces.com/contest/2117/submission/323510631 why this code giving WA.. In this code I am finding max index of a particular element and after that while iterating again finding max index of a elements in current segment..and so on Problem C is nice greedy problem thanku to authors

»
11 months ago, hide # |
 
Vote: I like it +2 Vote: I do not like it

Am I the only one acoustic enough to think of D involving a system of linear equations

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

    i did too but after realizing the significance of pairing the operations i quickly abandoned the idea (edit: i was considering some system for all a shb i at first but the pairing helped me realize the significance of APs)

    edit

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

    that was actually the way to think

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

    That was how I did it :)

    I used the first and last values given to solve for two variable system of equations. Really satisfying to see that a system of equations is a way to solve that problem.

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

    Even I did the same thing. Then I realized you can just solve for the first and last element and verify if the others satisfy it or not. It was actually satisfying to see the direct application of such a trivial high school math concept. https://mirror.codeforces.com/contest/2117/submission/323479092

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

the fact that even almost all testers found f easier then e and agreeing with me is ...

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

Problem G is using A* star algorithm here . f = h + g ; ( very basic thing in intro to AI courses). and worth to check out.

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

Can anyone provide one test case where my code for C will give wrong answer? I mean I have tried as many as I could but still couldn't find one test case where it could go wrong. And also I can't check the 1233rd input of the provided test cases where it gives wrong answer. my code

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

Greedy E !!! my solution to E solution Although code is not very well written but idea is simple....

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

I think B & C are wonderful :)

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

Thanks for the editorial

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

when would our rating update, i'm new so i don't know about that

»
11 months ago, hide # |
Rev. 2  
Vote: I like it -14 Vote: I do not like it

Good contest

»
11 months ago, hide # |
 
Vote: I like it +33 Vote: I do not like it
»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Can anyone tell me what is wrong with this implementation of problem C? I replaced the map implementation with a set, and it got all AC, but this map implementation is messing up on a very specific case (test case 1562) and I am unable to figure out the test case or reason why it fails.

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

(problem G) Can anyone hack this/send the test case that it fails on, I can't work out what is wrong. Thanks!

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

Problem G can also be solved with lazy prim algorithm. We firstly just run the algorithm from root until vertex $$$n$$$ is connected. Then we calculate an answer, but that may not be the optimal one. We can just continue running the algorithm until there is no edge not yet visited, and each time we visit an edge, we try to update the answer.

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

G is very easy with DSU. Maintain the smallest and largest edge weights for each component in the DSU, then update the answer if node 1 and n are in the same set. Code: 323560818

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

Here is another solution for C. Let us maintain an array that stores, for each $$$a_i$$$, the positions at which it appears in descending order. Next, we traverse through the array, and maintain two variables:

  • last_max: This tells us the maximum next position that an element in the previous segment has (among all elements in the previous segment).

  • next_min: This tells us the minimum next position that a scanned element has (among all scanned elements)

Now, we can make a cut (i.e., create a new segment) at index $$$i$$$ iff last_max is less than or equal $$$i$$$ and next_min is greater than $$$i$$$. The first condition tells us that all elements in the previous segment are contained in the current segment, whereas the second condition tells us that if we make a cut at position $$$i$$$, we are guaranteed that the next segment will have all elements in the current segment.

Here is the implementation code 323500146

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

Did anyone solved E using DSU?

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

I think G is an application of Kruskal's algorithm, perhaps most people think so

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

    yeah I solved it using Kruskal's algorithm (mst) + maximum path query(binary lifting)

    mst already minimizes the maximum edge on the path

    then i just iterated over all the edges considering it the minimum edge (x, y) ans = wt[edge] + max of get_max_edge(1, x), get_max_edge(1, y), get_max_edge(x, n), get_max_edge(y, n)

    minimum ans is the global answer

    here is my submission: 323473024

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

B was way too easy, even for its position. Personally, I think it was easier than A

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

Can anyone tell what is wrong with this submission for Problem C. It is failing on test case 2 https://mirror.codeforces.com/contest/2117/submission/323569603

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

C was way more challenging than D. Maybe that's cause I tried bruteforcing C.

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

Awesome editorial

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

F deserves an example. I had no idea what was ask from me.

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

    Subtree sum of a node basically means summation of all the weights of nodes under it (which can reached from the node in some way).

    You need such a placement of weights on each node such that for all nodes in tree each subtree sum is unique.

    Assume


    a | b /\ c e / \ d f / g \ h

    Here we need such a placement of weights 1 and 2 on each node such that subtreesum(a), subtreesum(b),...,subtreesum(h) are all unique values.


    subtreesum(a) = weight(a) + weight(b) +...+ weight(h) subtreesum(b) = weight(b) + weight(c)+...+ weight(h) subtreesum(e) = weight(e) + weight(f) subtreesum(f) = weight(f) + weight(g) + weight(h) and others similarly

    So we need to find how many such permutations of weight distribution is possible such that all the subtreesum are unique values.

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

guys any tips I have solved till D. Took a lot of time for C my implementations was map<int,queue> m;

I want to improve.

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

Dude B was easier than A and D was easier than C (╥﹏╥), Why did you confuse my brain (╥﹏╥)

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

Can anyone help me with the logic behind problem D?

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

    I don't know about the first solution but the second one is just using linear equations to find the number of operation of both the first operation and second operation and checking if it's same for all or not

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

I thought of C like, the answer would be: the number of times the first number appears in the array,but the solution got rejected :( can someone explain like some testcase where this might go wrong?

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

    It dosent work for the testcase 5 1 5 4 5 5 1 , from your logic the answer should be 4, bu the answer is 3 — 5 , 1 5 , 4 5 5 1 . Like every element is the previous set must be repeated in the current set not only the first one

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

is anyones submission stuck at in queue while practicing? Like I submitted my solutions today for practice but all the submissions are stuck at In queue even after the system tests are complete. Is it ohk? or any issue? or how much time will it take to work? Edit : some of the submissions got accepted just now, maybe it'll work in a few minutes

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

Can anyone help me understand why my solution for problem G got a WA ? Here's my code: 323611984 I know there's an easier way to solve it, but I’d really appreciate it if someone could point out why this version fails. Thanks in advance!

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

Found D to be easier then C . Anyone else with the same opinion?

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

    yeap, also if we use python for C we get TLE but if we use CPP the solution gets accepted. Typical cpp behavior :)

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

Problem G is beautiful when u use DSU.

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

    bro can you please explain your logic

    • »
      »
      »
      11 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it
      • Greedily keep merging node u, v with the smallest edge weight till 1 and n is connected
      • store minimum edge weight of each component during merge
      • final answer :- currEdgeweight (edge after which 1,and n got connected: this is maxEdge weight in component ) + minEdge weight of component (1, n) which you have maintained.
    • »
      »
      »
      11 months ago, hide # ^ |
      Rev. 2  
      Vote: I like it 0 Vote: I do not like it

      So the prerequisite is DSU & MST.

      You sort the edges first, and start merging the vertices of an edge in the increasing order.

      And you maintain a min array where u store the min edge value of the current node.

      Now check if 1-n is connected and update the ans as min(ans, current weight + min edge value of current node so far).

      Try to dry run on some test cases u will get the picture, try a test case where min value is still connected to the path but doesn't lie on the actual path from 1-n.

      My code: https://mirror.codeforces.com/contest/2117/submission/323621729

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

        Thanks! With the DSU logic, everything is much clearer. I had tried to solve it using Dijkstra before, but got a wa. Now DSU the logic makes sense.

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

        Also, do I need to compare anything anymore once 1 and n are in the same component? Isn’t that the point where I already have my answer?

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

          No I was confused on the same point, that first 1-n connected component should give the right answer.

          But that isn't the case, the min edge value of parent node 1 will change, once u keep merging newer edges.

          It's the test case I mentioned above. Where the min edge is not part of the actual path.

          E.g.
          6 node. 
          vertices: weights
          1-6 : 10
          1-2 : 12
          2-3 : 5
          3-4 : 19
          4-5 : 20
          

          Here the answer is 17 instead of 20. if you break early u will get 20 as the answer.

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

            Yes, I noticed the same after getting a WA submission. I thought that if a component connects to any other component with a smaller minimum value, the answer might decrease. So your initial logic is absolutely necessary and correct.

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

              For problem G, can someone please explain why can't we use Binary Search + BFS to find the max value in the tree which is premissible & form all the connected edged less than current value of binary search & form a path from 1 to n. and then find the minimum value in that path(i.e all the edge which connect 1-n ).

              https://mirror.codeforces.com/contest/2117/submission/323689502

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

                The reason is that there might be a better path that uses an edge with a higher value. Take this input for example:

                1
                6 6
                1 2 6
                2 3 4
                3 6 6
                1 4 7
                4 5 2
                5 6 7
                

                The algorithm will see "Oh, the graph is connected when we consider edges with weight less than or equal to 6, so we use only those edges", which will return an answer of 4 + 6 = 10 by going through 1 — 2 — 3 — 6, even if the path 1 — 4 — 5 — 6 is of weight 2 + 7 = 9.

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

    MST soln blows my mind bro....., wasted 1 hr thinking Dijkstra during contests :'(

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

Problem C, same second solution in python get TLE.

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

Can problem H be solved with a dynamic segment tree? I got RE

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

can someone explain on high level why this didn't work for G https://mirror.codeforces.com/contest/2117/submission/323524848

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

Congratulation yse You have achieved the record for the fastest and most helpful editorial in codeforces

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

What's possibly in the test cases 25 and 39 for C everyone? I got TLE in Java in #25 after rejudge.

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

    Same for me..I used cpp and got TLE in test case 25 after rejudge..It's basically array of size 20000 consisting of only 1s

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

      the output is not 200000, so it's not only ones. Besides I've tried 200000 1's locally and it's not that slow :(

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

Why can't I use persistent segment tree to solve H? I know a persistent segment tree only costs O(nlogn) memory totally,but it seems to match the condition of 192 MB.

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

can someone suggest some way of solving D through Binary Search..

I tried this but got wrong on test case 2

#include <bits/stdc++.h>
using namespace std;


bool check(int n,vector<long long>&arr, int mid){
    for(int i=0;i<n;i++){
        if(arr[i]<((i+1)*mid)) return false;
    }
    return true;
}
int main() {

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
   

    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        int n;
        cin>>n;
        vector<long long>arr(n);
        for(auto &x:arr) cin>>x;
        long long s=0;
        long long e=arr[0];
        long long ans=0;
        while(s<=e){
            int mid = s+(e-s)/2;
            if(check(n,arr,mid)){
                ans=mid;
                s=mid+1;
            } 
            else e=mid-1;
        }

        long long other = arr[n-1]- (n)*ans;
        for(int i=0;i<n;i++){
            arr[i]=arr[i]-(i+1)*(ans);
        }
        // for(auto &i:arr) cout<<i<<" ";
        // cout<<endl;
        for(int i=n-1;i>=0;i--){
            arr[i]=arr[i]-(n-i)*other;
        }

        // for(auto &i:arr) cout<<i<<" ";
        bool flag=0;
        for(int i=0;i<n;i++){
            if(arr[i]!=0) {flag=1;break;}
        }
        if(flag) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;

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

in the 2nd solution to the D problem: how can we prove that the solution using a1 and a2 will be valid for all ai? is it simply because of induction? also secondly, why don't we need to check whether the value of y obtained after the division to be an integer value for it's acceptance as a valid solution (as we are checking for it to be negative)?

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

    In the solution the author isn't proving anything; the ($$$x$$$, $$$y$$$) he found is the only pair that is possible (i.e., if there exists a solution then it must be this ($$$x$$$, $$$y$$$); but it's possible that this ($$$x$$$, $$$y$$$) is not actually the solution). That's why he actually performs the operations with this ($$$x$$$, $$$y$$$) to check; it is the solution iff all elements become $$$0$$$).

    That's also the same reason he doesn't check whether $$$n+1$$$ divides $$$2a_1-a_2$$$; he just performs integer division and if $$$y$$$ is actually a decimal, then some elements won't become zero (because $$$y$$$ has been "forcefully" rounded).

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

I forgot the lca algorithm :((((

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

Does anyone know why this solution was hacked for D? Tried to greedily if-else all possible cases i could think of :(

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

can anyone please tell me why this dijsktra dont works in some cases.323677800

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

For problem G, can someone please explain why can't we use Binary Search + BFS to find the max value in the tree which is premissible & form a path from 1 to n. and then find the minimum value in that path.

https://mirror.codeforces.com/contest/2117/submission/323689502

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

in F problem solution, adding the statement

(d = depth_x — depth_y)

"in the case where the longer branch takes odd sum values, the (d + 1)-th node from the leaf will also have to be fixed as 2, to avoid collision with the shorter branch's d-th value"

helps understand it easier.

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

First time poster here. Need help debugging my solution.

I used yse's idea on solving problem C, on pypy 3.10. My solution was accepted but, after hacking, it went TLE on test 9. Didn't make sense to me, no O(n log n) solution should TLE with N=2*10^5.

After the rejudging, I attempted a few small optimizations and all went TLE on the same test. I thought it should be some pypy issue on handling sets, until I noticed the same idea implemented by Anni_official had resisted the hacking: 323469107

For comparison, here is my original implementation, the one that failed on rejudge: 323446043

The implementations were way too similar, no obvious reason why they should behave so differently, until I tweaked the input handling, resulting in these submissions: 323651217 323651381

The first TLE'd on case 9, the other was accepted. Only difference was that strings, rather than integers, were handled to the sets.

I was unable to replicate this issue on my IDE+environment (PyCharm, also running pypy 3.10, mac OS, M1 processor). I ran the same code of both submissions, measuring elapsed time (including the input handling), and both run in about 20–40 ms (with the integer version more often on the lower end of this range). I can't see the whole test case but I built a (hopefully) similar test case, containing a 2*10^5-long list of random integers whose second half is equal to the first half shuffled.

Any help (on either what's happening or how to debug this, since "it works on my IDE" and we can't experiment directly with the judge) is appreciated.

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

    That's because you used set. Using set or dict naively gives risk of getting hacked.

    For actual testing, try my hack case. I'm sure your solution is slow on pypy.

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

      Indeed, about 7 seconds. Test case 9 is a similar input sequence. Seems like it would also be easy to break the string version except the hack just wasn't produced during the contest itself.

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

        Hashing method for strings is complicated, and it's hard to craft an input to attack stringified integers. I do not know anti-hash attacks for them.

        (Personally I do not recommend using strings, because they are slow.)

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

c is really good.

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

Could someone help me with problem C? I know the editorial uses O(n) solution, but during contest i came up with O(n log n) approach using binary search.

I'd like to stick with this idea and try to make it work (for learning purposes). Here is my submission: (https://mirror.codeforces.com/contest/2117/submission/323735379). It gives WA on test 2.

(Also i added a detailed explanation of algorithm (in russian) as a comment in the code)

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

    I solved it using binary search as well. I don't understand Russian, so I won't be able to understand your comments, but you can take a look at my solution:

    325357943

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

Correct me if I'm wrong but the editorial for E, shouldn't it say:

We also need to check if position i already contains a match, or if ai=ai+1 or bi=bi+1.

Instead of:

We also need to check if position i already contains a match, or if ai=bi+1 or bi=ai+1

My understanding is if ai = ai+1, then bi can be equal to ai, or if bi = bi+1, then ai can be equal to bi

Which set i as valid

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

love the C(solution2), same as I thought, but i cant realize it in time.

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

Does anybody know the time complexity for the second code for problem C?

I am looping over all elements which is O(n), but every now and then I clear seen, I am assuming the worst case for seen is to be holding almost all of the elements which also makes clearing it O(n), making the worst case to be O(n^2)..

The solution makes absolute sense but how does it pass? Doesn't seem optimal.

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

    Thanks I think I got it.

    I am assuming the idea behind this is that O(n^2) means for every iteration i from 1 to n I will also iterate with j from 1 to n, which is not the case here, no, here the worst case would only happen when iteration i is equal to n — 1 (assuming 0-index) and the condition (seen.size() == curr.size()) is satisfied, in this worst case, the whole array is only cleared ONCE in the whole loop, and the iterations prior to i == n — 1 would be simply O(logN) I am not clearing anything just basic if conditions and set.inserts, so even saying that this is the worst case overall is debatable, it probably just isn't.

    In other words the solution fits the time perfectly.

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

    Sorry if I wasn't clear in my editorial. Notice that we only insert each element into seen at most once. So we have at most $$$n$$$ insertions. We certainly do not need to delete more elements than we have inserted, so we also have at most $$$n$$$ deletions. In total, the runtime is $$$O(n\log n)$$$.

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

      Thank's alot! It was a good contest, keep it up!

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

      orz,i am new to the arithmetic,in the game time idont realise that ican use set to solve it,but looking for queue or sth, I dont solve lots of problems, but its my best, cause i can solve such interesting problem without a Particularly complex algorithms

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

Very Good Jop

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

For problem F, how do we know that there can be at most 2 leaves?

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

    because lets say there are 3 or more leaf nodes then all of these leaf nodes are themeselves subtrees and by php atleast two of them will have same value

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

      Thank you for your reply. But I do not know what PHP is. Could you elaborate?

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

        Pigeonhole principle

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

          Ok thank you, I still have no idea how we can be sure we have max of 2 leaf nodes

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

            Look at all of the possibilities:

            1 1 1

            2 1 1

            2 2 1

            2 2 2

            In all cases, at least two leaves have the same value, contradicting the problem statement.

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

              Thank you, I understand it now. One major point I was missing was that leaf nodes are also, by themselves subtrees. Once I realized this, it all made sense, thank you very much.

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

nice contest

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

QUESTION E, JAVA CODE

import java.io.*; import java.util.*;
public class Main{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static String brt[];
    static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
    
    public static void main(String args[]) throws IOException{
        var t=Integer.parseInt(br.readLine()); while(t-->0){
            
            var n=Integer.parseInt(br.readLine());
            var a=new int[n]; var b=new int[n];
            brt=br.readLine().split(" "); for(var i=0;i<n;i++)a[i]=Integer.parseInt(brt[i]);
            brt=br.readLine().split(" "); for(var i=0;i<n;i++)b[i]=Integer.parseInt(brt[i]);
            
            var a1=new HashMap<Integer,Integer>();
            var b1=new HashMap<Integer,int[]>();
            
            long an=0;
            
            for(var i=0;i<n;i++)
            {
                if(a[i]==b[i])an=Math.max(i+1,an);
                
                var prea=a1.getOrDefault(a[i],-1);
                an=Math.max(prea+1,an);
                
                var preb=b1.getOrDefault(a[i],new int[]{-1,-1});
                if(preb[0]==i-1)an=Math.max(preb[1]+1,an);
                else an=Math.max(preb[0]+1,an);
                
                a1.put(a[i],i);
                
                var tm=b1.getOrDefault(b[i],new int[]{-1,-1});
                tm[1]=tm[0]; tm[0]=i;
                b1.put(b[i],tm);
            }
            
            var a2=new HashMap<Integer,int[]>();
            var b2=new HashMap<Integer,Integer>();
            
            for(var i=0;i<n;i++)
            {
                if(a[i]==b[i])an=Math.max(i+1,an);
                
                var prea=a2.getOrDefault(b[i],new int[]{-1,-1});
                if(prea[0]==i-1)an=Math.max(prea[1]+1,an);
                else an=Math.max(prea[0]+1,an);
                
                var preb=b2.getOrDefault(b[i],-1);
                an=Math.max(preb+1,an);
                
                var tm=a2.getOrDefault(a[i],new int[]{-1,-1});
                tm[1]=tm[0]; tm[0]=i;
                a2.put(a[i],tm);
                
                b2.put(b[i],i);
            }
            
            bw.write(an+"\n");
        }
        bw.flush();
    }
}
»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

H can be done online using a treap.

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

Can anyone give me a test-case which would cause this code to fail? Its for problem E https://mirror.codeforces.com/contest/2117/submission/328590801

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

in problem F, can anyone prove that in the case where depth[y] > depth[x], assigning all nodes on the path from v to x (excluding v and x) to 2 will always ensure that the s[] values of all subtrees in the branch x are different from all s[] values of all subtrees in the branch y, regardless of how we assign the nodes on the path from v to y (excluding v and y)? this property doesn't seem very intuitive to me

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

can someone tell me why is this giving tle on TC 33 question — G

// this is code
#include <bits/stdc++.h>
using namespace std;

vector<int> func(vector<vector<pair<int,int>>> &g,int src){
  int n=g.size();
  vector<int> ans(n,2e9);
  ans[src]=0;
  set<pair<int,int>> s;
  s.insert({0,src});
  
  while(s.size()){
    auto node=(*s.begin()).second;
    s.erase(s.begin());
    
    for(auto ch : g[node]){
      if(ans[ch.first]>max(ans[node],ch.second)){
        ans[ch.first]=max(ans[node],ch.second);
        s.insert({ans[ch.first],ch.first});
      }
    }
  }
  
  return ans;
  
}

void solve() {
  int n,m;cin>>n>>m;
  vector<pair<int,pair<int,int>>> edges;
  vector<vector<pair<int,int>>> g(n+1);
  
  for(int i=0;i<m;i++){
    int u,v,w;cin>>u>>v>>w;
    edges.push_back({w,{u,v}});
    g[u].push_back({v,w});
    g[v].push_back({u,w});
  }
  
  vector<int> dist1=func(g,1);
  vector<int> dist2=func(g,n);
  int ans=2e9;
  for(auto x : edges){
    int u=x.second.first,v=x.second.second,w=x.first;
    ans=min(ans,w+max(w,min(max(dist1[u],dist2[v]),max(dist1[v],dist2[u]))));
  }
  
  cout<<ans<<"\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
»
9 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

In the solution of H:

Let Notice that for the former case, we are setting $$$b_{x_k} = -1$$$ and in the latter case, we are setting $$$b_{x_k} = -1$$$.

yse

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

Please Anyone help me in my code for C problem i got stucked getting wrong answers

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

In question F, suppose a tree:

Number of nodes: 5

Edges are: 1-2, 1-3, 2-4, 2-5

Number of leaves in this tree = 3.

According to the tutorial solution, ans = 0.

But, in this testcase: ans = 4.

For node [1], value can be 1 or 2. For each value of node [1], value of [2] can be = 2, value of [3] can be = 1 and (value of [4] can be = 2 and value of [5] can be = 1) or (value of [4] can be = 1 and value of [5] can be = 2).

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

as for problem c, is it legal for a segment to be composed of only one number?if so,the first segment is exactly composed of one number to make it easier for the following segment to be legal,and then we can solve it greedily,but actually i was wrong according to the test running result ,can anyone help me pls???

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

for question 3. can someone tell me why my code gives wrong answer for test case 3

361849565

help is appreciated