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

Автор yse, 11 месяцев назад, По-английски
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!
Разбор задач Codeforces Round 1029 (Div. 3)
  • Проголосовать: нравится
  • +87
  • Проголосовать: не нравится

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

omg

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

super fast editorial

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

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

Thanks for fast editorial

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

fast editorial!

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

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

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

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

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

I forgot I named my dijkstra function that.

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

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

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

    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 месяцев назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      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 месяцев назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        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 месяцев назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          Great. Thanks

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

          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 месяцев назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

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 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

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

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

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

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

I think B & C are wonderful :)

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

Thanks for the editorial

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

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

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

Good contest

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Did anyone solved E using DSU?

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

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

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

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

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

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

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

Awesome editorial

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

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

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

    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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

Can anyone help me with the logic behind problem D?

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

    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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

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

Problem G is beautiful when u use DSU.

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

Problem C, same second solution in python get TLE.

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

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

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

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

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

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

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

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

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I forgot the lca algorithm :((((

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

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

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

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

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

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 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

c is really good.

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

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)

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

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

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

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 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    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 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +3 Проголосовать: не нравится

    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)$$$.

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

Very Good Jop

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

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

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

nice contest

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

H can be done online using a treap.

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

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 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
Spoiler

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

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

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 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 месяца назад, скрыть # |
Rev. 7  
Проголосовать: нравится 0 Проголосовать: не нравится

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

361849565

help is appreciated