Vovuh's blog

By Vovuh, history, 4 weeks ago, In English,

All ideas belong to MikeMirzayanov.

1249A - Yet Another Dividing into Teams

Tutorial
Solution

1249B1 - Books Exchange (easy version)

Tutorial
Solution

1249B2 - Books Exchange (hard version)

Tutorial
Solution

1249C1 - Good Numbers (easy version)

Tutorial
Solution

1249C2 - Good Numbers (hard version)

Tutorial
Solution

1249D1 - Too Many Segments (easy version)

Tutorial
Solution

1249D2 - Too Many Segments (hard version)

Tutorial
Solution

1249E - By Elevator or Stairs?

Tutorial
Solution

1249F - Maximum Weight Subset

Thanks to neal for the additional editorial of this problem and providing the linear solution!

Tutorial
Solution (Vovuh, n^3)
Solution (PikMike, n^2)
 
 
 
 
  • Vote: I like it
  • +95
  • Vote: I do not like it

»
4 weeks ago, # |
  Vote: I like it +4 Vote: I do not like it

Thanks for the quick editorial.

»
4 weeks ago, # |
Rev. 3   Vote: I like it -9 Vote: I do not like it

[Deleted]

»
4 weeks ago, # |
  Vote: I like it +4 Vote: I do not like it

i didn't get the output of C1. Can someone make me understand

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

    for an input num, if num is good number print it, if it is not, find the next good number and print it.

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

For C2, why does a position of 2 in the ternary representation need to be replaced?

  • »
    »
    4 weeks ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    What does it mean a position of 2 in the ternary representation? It means that the corresponding power of 3 is used 2 times. But in the problem, it is said that there should not be any power repeated twice or more. That's why the position of 2 in the ternary representation replaced.

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

      Makes sense, I didn't see it that way (different approach to the solution). Thanks for clarifying.

»
4 weeks ago, # |
Rev. 2   Vote: I like it +22 Vote: I do not like it

For C2, I used a simpler greedy approach. First, I find the smallest number that is the sum of all powers of 3 till the sum becomes >= n. Let us call the sum Z. So this will lead to a number of form 111111 where 1 => The corresponding power of 3 is added to the sum. Now, suppose the highest power of 3 that was included in the sum is m. I go from mth to 0th power and try to subtract corresponding power from current Z. If it leads to a number that is still >= n, we reduce Z by the current power. Else, we continue. Intuition is that if we subtract a larger power, its sum is anyways going to be larger than the sum of all lower powers combined. So it will be a better choice for sure.

My Submission: https://codeforces.com/contest/1249/submission/63202537 (Got AC, I hope no hack :P )

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

    i submitted same logic but got wa , i changed the order too.

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

    That's a pretty elegant solution. It does seem quite intuitive, but, do you have any sort of proof of why this greedy approach works?

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

      My solution the same. I think we MUST include x=10000 from y=11111 form if n<=y because it's guaranteed that n strictly between 1111 and 11111 (because of previous step, where n>1111) and 10000 smallest option for this 63155044

»
4 weeks ago, # |
Rev. 2   Vote: I like it +51 Vote: I do not like it

I have an $$$\mathcal{O}(n \log n)$$$ solution to problem F: 63206899.

The key idea is to merge smaller DP states into bigger. Would have been a nice harder Div. 1 problem. :)

Update: it's actually $$$\mathcal{O}(n)$$$.

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

    It's also funny because I submitted $$$\mathcal{O}(n^4)$$$ in contest: 63153425

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

    Can you please explain your approach in detail? thanks.

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

    Is it possible for you to provide a quick explanation of your approach ? This looks interesting. I could only come up with O(n2) but O(n) is really impressive.

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

It was amazing contest!

»
4 weeks ago, # |
  Vote: I like it +38 Vote: I do not like it

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

    My thoughts exactly after seeing B. Lmao XD

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

    I did the same as you. Despite of the different of implementation, I think the author's ideal is the same, the way he implemented like using dfs :v

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

      Difference is that a simple vector/ArrayList would suffice for finding the cycle, so the image still applies. I don't expect a Div3B problem to require the participants to be familiar with DSU.

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

    I use Tarjan to slove problem B2.....

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

      Wow, you definitely win, I don't know Tarjan's lol

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

        It is a algorithm that you can find all the strongly connected components in a directed graph in O(n). Then you can count the size of each strongly connected component.

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

          That's definitely an overkill. I did the same as Spheniscine. Maybe I should look into Tarjan's. That definitely will come in handy.

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

          Orz wqyzstql,you are so strong !

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

      Tarjan is much more overkill =)))

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

        If DSU is a sword, Tarjan's is a chainsaw

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

          But I think DSU is a very common algorithm in some Algorithmic competition...And Tarjan is rarely used.

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

I didn't understand the tutorial of C1 here, more explanation please?

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

    Convert the number to base 3 (ternary). A number is good iff there is no $$$2$$$ digit in it.

    For the easy version, incrementing the number until it's good would be sufficient. For the hard version, a more-analytic solution is required.

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

      Why is it that 2 shouldn't be present in the number?

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

        Converting a number $$$n$$$ to base 3 is a way of representing $$$n$$$ as the sum of powers of 3. e.g. if $$$n = 1201_3$$$, $$$n = 1\cdot3^3 + 2\cdot3^2 + 0\cdot3^1 + 1\cdot3^0 = 3^3 + 3^2 + 3^2 + 3^0$$$. The problem requires the powers of 3 be distinct, which means a good number should have no $$$2$$$ in its base-3 representation, as that would mean more than one instance of that power.

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

          why add a power of 3 at pos0 will make remaining element 0 as said in the tutorial of problem c2. I'm not getting it would you please explain it also.

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

            I'd explain it like this:

            First, convert $$$n$$$ to base 3, and prepend a leading $$$0$$$. Store this in array $$$s$$$.

            Now we want to get rid of all instances of $$$2$$$ in $$$s$$$. How do we do that? Let's just focus on one instance, we'll call its index $$$i$$$.

            Now we want to increment $$$n$$$ until $$$s[i]$$$ changes. What happens then? You can imagine $$$s$$$ as being like a car odometer, except in base 3. Thus, the $$$2$$$ will roll up to become a $$$0$$$. When this happens, all digits to the right of $$$s[i]$$$ will be $$$0$$$, and $$$s[i-1]$$$ would increment.

            Since getting rid of one $$$2$$$ this way zeros out all digits to its right, we can now fix $$$i$$$ to be the index of the leftmost $$$2$$$.

            We're still not done yet though, because if $$$s[i-1] = 1$$$, it's now $$$2$$$, which we don't want. So we zero it out too and continue leftward until we find a $$$0$$$, which is guaranteed because we prepended a leading zero. We can then increment that $$$0$$$ to $$$1$$$, then convert the array back into the final answer $$$m$$$.

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

      Wow... I really didn't thought it would be a greedy type of problem. Thanks for explaining it tho. Now i see why I couldn't solve it. But I saw one guy used brute force and typed in all the possible value for C1. I was baffled, but he still got it correct.

»
4 weeks ago, # |
  Vote: I like it -20 Vote: I do not like it

I tried to solve B question both parts using dfs but getting the wrong output due to bug somewhere in the code. could anyone please help me debugging.it would be great help for me.

include <bits/stdc++.h>

using namespace std; vectorv[100005]; bool vis[100005]; int size=0; void dfs(int n) { vis[n] = true;
size++; for(int x : v[n]) { if(!vis[x]){ dfs(x); }
} } int main() { //int q; //cin>>q; { int n; cin>>n; int arr[n+5],hello[n+5]; for(int i=1;i<=n;++i){ cin>>arr[i]; if(i==arr[i]){ hello[i]=1; vis[i]=true; continue; } v[i].push_back(arr[i]); v[arr[i]].push_back(i); } for(int i=1;i<=n;i++) {
if(!vis[i]){ size=0; dfs(i); //cout<<size<<endl; for(int j=1;j<=n;j++){ if(vis[j]){ hello[j]=size; //cout<<hello[j]<<" "; }

}      
            //cout<<endl;     
        } 
    }
   for(int e=1;e<=n;e++){
        if(e==arr[e]){
            hello[e]=1;
        }
        cout<<hello[e]<<" ";
    }    
}

}

I have for a while commented on the query part for more simplicity.

»
4 weeks ago, # |
  Vote: I like it +3 Vote: I do not like it

I thought of a greedy for problem D1 which consisted of looking for the segment that had the most points that currently belonged to more than k segments and was updating. Can someone explain to me why this greedy doesn't work.

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

    Look at the testcase

    5 1
    1 9
    10 20
    21 30
    8 12
    18 22
    

    Now you would choose the [10; 20] segment, as it contains 6 points. But then you need to remove 2 more, where you could've deleted segment 4 and 5.

    In generał, you can try to disprove greedy by creating example in which the first choice ruins the rest of solution because now you have to pick lots of small elements to solution.

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

      Can you explain why the approach in the editorial is correct

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

        It's also a greedy approach, but instead we step through the segments in the order of their $$$l$$$, and preferentially remove the segments with the greatest $$$r$$$ when too many cover the current $$$l$$$ value (because removing those segments will lower the count more permanently for future values of $$$l$$$)

        See this submission for an example: 63225521

        By the way, this problem would be a perfect use-case for a double-ended priority queue (e.g. MinMaxPriorityQueue from the Guava library), but I don't know of any languages that have that in its STL. TreeSet/ordered set works though, with a slightly worse constant factor.

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

          Can you help me understand why my implementation does not work? My submission is 63270199

          I did the following: 1. sort all segments first by their r then by l then by index. 2. starting from the minimum to the maximum integer point, do the following. (a).as long as the current segment has not been removed and covers the current integer i, increment the counter cnt for i. (b).if cnt <= k, do nothing, move on to the next integer; else mark all the remaining segments that covers i as removed and increment the deletion counter that needs to be output. If the segment has been removed, do not increment this deletion counter.

          I thought this should work as the segments are sorted by their r first. So I am always processing segments that have smaller r. It turns out that I must be wrong somewhere...

          I'd appreciate your help.

          • »
            »
            »
            »
            »
            »
            4 weeks ago, # ^ |
            Rev. 14   Vote: I like it 0 Vote: I do not like it

            You shouldn't sort segments by $$$r$$$ first; you should sort them by $$$l$$$ first, then "sweep" from left to right, maintaining current covering segments in a TreeSet (or a MinMaxPriorityQueue if you're adventurous enough to try implementing one lol). It's the TreeSet/DEPQ that should maintain order by $$$r$$$, because you want to remove the minimum $$$r$$$ entries from the structure that are less than the current $$$l$$$ value (because they aren't covering our "cursor" anymore), as well as preferentially drop the maximum $$$r$$$ entries (and adding them to the answer set) if there are too many segments.

            For a TreeSet, a secondary comparator for $$$id$$$ is necessary because TreeSet will assume that entries are equal if the comparator returns 0, and so won't add "duplicates". Since $$$id$$$ is contextually guaranteed to be unique, it's a sufficient tiebreaker.

            This "sort by $$$x$$$ first, then put it into a priority queue/set that sorts by $$$y$$$" is actually a very common pattern in CP problems involving segments or time-intervals.

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

              Thank you for your quick reply! I implemented a solution based on your suggestion and it was accepted.

              As for the adventurous MinMaxPriorityQueue implementation, now I wouldn't ask you how to solve problem D easier version had I know how to implement that data structure, would I? :)

              I am pretty new to competitive programming so it has been quite a struggle here for me in codeforces contests. Mind if I add you as a friend?

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

                Certainly, though the "friend" list on this platform is more like a watch/follow; it doesn't even tell me who added me as a "friend".

                I'm glad it helped though! I've been programming for a long time, but it's only recently that I've started to pick up advanced optimization / data structure techniques. I started learning about them while solving the puzzles on http://adventofcode.com , then moved on to here after I've solved them all

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

                  Yeah, the UI here is not the best to navigate. There is a send message feature, maybe it is like sending emails?

                  I've been programming for a few years too but never paid attention to developing my algorithmic skill. Decided to form a habit of solving problems/joining contests to improve my problem solving skill. I first started on LeetCode a few months ago then decided to make it more painful by signing up here. The problems here are definitely a lot challenging.

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

                  Yes; "send message" is like private/direct messaging systems on forums

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

      Hi, mate i find jmrh's idea is similar to mine, but not completely the same. i thought that looking for the longest segment that is consisted by more than k points,or the segment consist the points(using the testcase upon, that more than K points would be {10 min,22 max}.)is the most possible one to be pop out,and so on . (first try,it would be seg[10;20],then left with point 21 and point 22 . sec try it would be the seg[21,30] cuz its longest by now and contains 21 and 22,and it's done.)

      so is this kind of way could work? how s it compare to the approach in the editorial?

      • »
        »
        »
        »
        13 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Hi ;) Sorry for late response ;) I don't quite understand, after removal of [10; 20] and [21; 30] point with x = 8 is still present in [1; 9] and [8; 12].

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

https://codeforces.com/contest/1249/submission/63210338

I think this is much more arranged than the previous code.

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

Still don't know why problem F is O(n^3) but not O(n^4)

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

    What does dp[u][dep] represent in problem F?

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

    I think it can be derived like this: Let $$$child_i$$$ denote the number of children of the $$$i$$$-th node.

    The total time taken at the $$$i$$$-th node is $$$O(N * (child_i)^2)$$$.

    So, the time complexity of the algorithm becomes:

    $$$\sum\limits_i (N * (child_i)^2) = N * \sum\limits_i (child_i)^2 \le N * (\sum\limits_i child_i)^2 = N * N^2 = N^3$$$

    So, the time complexity is $$$O(N^3)$$$.

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

Why this py3 solution for B1 got TLE hacked... I think it is almost same with the tutorial...


q = int(input()) for _ in range(q): n = int(input()) Q = list(map(int,input().split())) res=[1]*n for i in range(n): t = Q[i] while t!=i+1: t=Q[t-1] res[i]+=1 print(*res)
  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is for B1? how does it get hacked :thinking:

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

      yep,63138123,as you can see... Also, I noticed there are many py3 solutions like my submission hacked as well...

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

      I see the hack case now,a simply bad condition which cost 200*200*200 times....sadly for python3,it got TLE. R.I.P

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

        Yea I was just trying py3 the other day and always got TLE. So when I notice how the time execution fluctuates only from a tiny difference of input. I immediately abuse it by giving 200 queries, with 200 numbers and maximum time search.

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

I saw some people solved B2 using DFS. Can anyone explain how to solve B2 using DFS?

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

    For each position ; answer is the size of the connected component in which that element is.

    Only those elements are connected which form a cycle.

    And size of the connected component can be solved using DFS !

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

Why doesn't greedy work for problem E? I am thinking, if we use the elevator we continue to use it if we can, If we don't use it, we try to take elevator as much as possible as in next step we won't have to add 'c' if we get the elevator

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

    Maybe $$$b_i$$$ is small but $$$b_{i+1}$$$ is very very large. In that case it is better to pay $$$c$$$ cost and get off the elevator to take the smaller $$$a_{i+1}$$$ cost stairs rather than continue with the elevator. It is better to keep best case scenarios for getting to the floor by both elevator and stairs and keep comparing as you go along.

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

    say u calculate the best cost to reach ith level and to go to (i+1) level u dont always traverse the same path that u did for ith level to reach (i+1)level at ith level say cost is 50 for stair and 60 for elevator and a=100 b=1 c=50 now ur algo will choose 50 as best and try to go to (i+1) by 50+1+50 where as optimally u should choose 60+1

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

Can Anyone Explain why won't we have to check for all previous (1 to i-1) cases for particular i? In this case, it will take O(n**2).

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

    This would be because while travelling from 1 to (i-1), you would have already taken the minimum to reach there. So to reach from (i-1) to (i) , you just need to add the stairs/elevator of the cost from (i-1) to (i) and hence you can ignore all others for particular i

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

      This is not exactly correct. The DP works because of the structure of the distances, as prefix sums of increasing arrays. In general, you will not be right.

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

    This is because of the structure of the distances to the 1st floor (prefix sums of strictly increasing arrays).

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

I have submitted two solutions for B1 in Python3 and Pypy3 with the same code. Python3 got TLE but Pypy3 AC. Why there is so much difference between these two? If one logic is accepted in one language then it must be accepted in all available languages given there otherwise giving all languages during submission doesn't make sense. Vovuh can you please look into it?

AC solution(Pypy) : 63211941 WA solution(Python) : 63211890

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

    It’s somehow similar to using O3 flag when compiling cpp? I guess it’s just more optimised (most of the time)

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

    Edited. I read the code again and find that it's not the input()'s fault. Sorry for my last post.

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

What if going down was also allowed in E, what difference would it create? How'd we solve it then? Can someone please throw some light on it?

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

    Going down is currently allowed right now, it just won't create an advantage. Because you'll have to go up to the floor, cross it, and then come down, which will always be larger than going up to the floor and stopping right there. So, going down will not give any advantage because of the structure of the problem.

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

    In this problem you are allowed to go down, but this is not optimal at all.

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

Anyone else who did B2 with DSU :') ?

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

I think my subbmition more easier than in editorail. C1 — C2 Ur opinion https://pastebin.com/xu5nrwzY

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

For Problem D I created no more than k non-overlapping segments greedily based on shortest right position. This approach seems to fail for some reason, any ideas why it's failing? code

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

Regarding the C2 question test case 9.

Can someone tell me how can the input of 450283905890997363 expect itself as an answer? the ternary representation of this input is : 10000000000000000000000000000000001200 which has a 2 in it so the correct answer should be 10000000000000000000000000000000010000 convert into decimals, right?

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

    you are wrong, 450283905890997363 representation of 3^37

    upd: you can check diff between 450283905890997363 and 450283905890997444 (your answer) is 81 (fifth bit that equals to 3^4)

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

      You are right, thanks for pointing out 3^37. my conversion function was wrong I guess

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

In the contest, after long time debug on problem D1 (stupid update error), I even don't read statement E, now I solved it easily without Editorial. I'm very regret about it.

»
4 weeks ago, # |
  Vote: I like it +13 Vote: I do not like it

Here is an $$$O(n^2)$$$ solution for problem F: 63223147 , I used a greedy approach to solve it and have not find any hacks. I think it is a correct approach.

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

Can someone explain to me the code for B2?

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

    It is obvious that we have bunch of cycles that are not connected.

    I keep an array(S) for size of each cycle and an array(N) for each node that shows what cycle they belong to.

    for each node i see if it belongs to some cycle . if not i go traverse that until i reach the same node and number all nodes the same(in N) .

    like this :


    while cur != origin: cur = p[cur] size_of_cycle ++

    so after i checked all nodes for the node i its output is S[N[i]]

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

can someone help me with this?

my failed submission

my accepted submission

the only change in the both is that i commented out

ios_base::sync_with_stdio(false);

cin.tie(NULL);

how does this affect the output?????

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

    I think your vector is out of range , you can test when $$$n$$$ is $$$1e18$$$ ,the variable $$$cnt$$$ is 40 ,but the size of the vector is 39 . Obviously, it is out of range.

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

      But I don't understand why removing the fast i/o lines fixes the issue.

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

        I also don't understand. Maybe it's an undefined behavior.

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

      The cnt can't be 40..the input size is max 10^18...so cnt can be max 38

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

        You can debug ,and output the cnt.My complier is visual stutdio ,when n is 1e18,there will be an error that the vector is out of range

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

          Thanks for pointing it out..My last for loop was buggy. Fixing the loop made the solution get accepted without removing ios_base::sync_with_stdio(false)

          But i still can't see how removing ios_base::sync_with_stdio also removed the bug XD

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

            I also don't know , we need professionals to answer this strange problem.

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

In B2, I used set to check if the answer of ith element is already found ,it gave me WA, then i used array to check if element is already answered it got accepted. Doesn't 'contains' method in set in java runs in O(1)?

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

why at the end of solution C2 if (vals.back() == 1) ans = pw / 3; ? Can someone help me?

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

    Take an example and it will be clear to you

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

      why this case is not included in normal cases? I mean the last element changed from 0 to 1 ,exactly after pos2 ,the newly added 0 is pos0.It can also be caculated by the same way above.Why should this case be special?

»
4 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

Kindly explain dp[v][dep] in probelm F (Maximum weight subset)?

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

Too good contest, thanks

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

Another (easier?) implementation of D2

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

Can some one explain Editorial of D2 , i did not got clear understanding .

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

    If you've understood idea behind D1, then its the same thing done efficiently or say smartly ; observe this code, may be you'll get this from it.

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

      Yeah , i was trying to do without using the set (STL) and that's why i was facing difficulty. I upsolved it finally.

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

        Can you please explain in detail your approach now @rananjay

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

My solution for 1249B2 — Books Exchange (hard version) was giving wrong answer using disjoint set with path compression. Any idea what was wrong ?

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

I am getting WA in D1. Link to solution: https://codeforces.com/contest/1249/submission/63245610

Please help!

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

As usual, I don't understand the editorial.

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

In E. What does it mean by "Time overhead" ? Time to close the door? Time to open the door?

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

    Every time you use the elevator, you need to pay $$$c$$$ in time cost once, in addition to the $$$b$$$ time costs for the floors traveled. (You can imagine it as time spent waiting for the doors to open. Don't think too hard about the real-world logic of it though; this is CP-land) You can ascend several consecutive floors per use, but if you get off to use the stairs, then go back to the elevator, you have to pay $$$c$$$ again.

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

I got another solution for C2 which does not involve any case handling (or dealing with annoying carries!).

We can easily write a function $$$f(i)$$$ that returns the i-th good number. It can be implemented by representing $$$i$$$ in base 2, then treat the resulting digits as a base 3 number. The function runs in $$$O(\log i)$$$.

Then we use binary search to find the smallest $$$i$$$ that satisfy $$$f(i) \geq n$$$. If $$$s$$$ is the smallest $$$i$$$ that satisfy the condition, the answer would be $$$f(s)$$$. The total time complexity would be $$$O(\log^2 n)$$$.

My submission: 63244719

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

Is it ok, what a few participants 1600+ rating got it updated after contest?

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

PROBLEM -E

https://codeforces.com/contest/1249/submission/63275300

Please tell me what's wrong with my logic. i passed 10 test cases it failed on 11th.

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

    FYI

    i am calculating cost from the first floor itself. My logic is i will save the information if i used elevator in the n-1 floor if i am calculating for nth floor(to decide if i have to add 'c' or not ) and i will try to use elevators much as possible.

    • »
      »
      »
      13 days ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I had same fail try test case 4 2 4 1 20 2 2 1 but still I have fail in this test.If you solved it pls help

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

Can problem F be done for general graphs in polynomial time?

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

    Nope. Even the case with $$$k = 1$$$ and all $$$a_i = 1$$$ is NP-complete (it is maximum clique problem).

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

Can anybody explain

ok &= abs(a[p1] — a[p2]) != 1; in problem 1249A

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

In C1 problem why we're using cur%3 == 2?

Line 20: if (ok && cur % 3 == 2) ok = false;

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

what is the meaning of curSegs.erase(prev(curSegs.end()));

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

Problem E

Why the jury's answer 14th number is 23 instead of 24? The next number by stairs will have +3 and by elevator either +1 or +3 (1 + 2),

The previous value was 21 so it can be either 24 or 22..

Here is pastebin what I'm talking about: https://pastebin.com/FmZyit6Y.

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

in 1249B2 — Books Exchange (hard version) solution . vector<int> used(n) should be of bool type .

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

    C++ doesn't care about the type (int or bool) if you have for example only 1 and 0 values it will treat them like true and false.

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

Vovuh For problem F. The recurrences you mention work if computing the maximum value of a set when distance between any $$$2$$$ vertices is $$$\ge k$$$. But, the problem asks to compute the answer when the distance between any $$$2$$$ vertices is $$$> k$$$. When I read your code, I saw that you are actually incrementing $$$k$$$ beforehand. So, I think you should mention in the tutorial that you change the problem first to the $$$\ge k$$$ version or update the recurrences as:

  • If $$$dep = 0, dp_{v, 0} = a_v + \sum\limits_{to \in children(v)} dp_{to, k}$$$.

  • Otherwise, $$$dp_{v, dep} = max(dp_{v, dep}, dp_{to, dep-1} + \sum\limits_{other \in children(v) \setminus \{to\}} dp_{other, max(k-j, j-1)})$$$

Also, there's a typo: "we can notice that this is now what we wanted". I think you meant not.

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

    Yes, I forgot to mention that I increase $$$k$$$ in the beginning of the program to compute the subset with distances $$$\ge k$$$, thank you. And thanks for mentioning the typo, it will be fixed now.

»
3 weeks ago, # |
  Vote: I like it +1 Vote: I do not like it

what is life

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

please someone help me with the runtime error, the code is running fine on the sample test cases on hackerearth's codetable , but when i am submitting it on codeforces, it is failing for the first test case itself. Test Case: 7 2 11 11 9 11 7 8 8 9 7 8 9 11 7 9

#include<bits/stdc++.h>
#define ll long long int
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007 
#define N 200000
#define pii pair<int,int>
#define pb push_back
using namespace std;

vector<pii> s[N+1];
vector<int> e[N+1];
int ec[N+1];
set<pair<pii,int>> st; //<end,size>,num
int main()
{
	 
   boost
	int t=1;
	//cin>>t;
	while(t--)
	{   
	    ll n,k;
	    cin>>n>>k;
	    for(int i=1;i<=n;i++)
	    {
            int l,r;
            cin>>l>>r;
            s[l].pb({r,i});
            e[r].pb(i);
	    }

	    for(int i=1;i<=N;i++)
	    {
	       sort(s[i].begin(),s[i].end());
	    }
	    
	    
	    vector<int> ans;
	    int as=0;
	    
	    
	    for(int i=1;i<=N;i++)
	    {
	        
	        for(auto el:s[i])
	        {
	            st.insert({{el.first,el.first-i},el.second});
	        }
	        as = st.size();
	        while(as>k)
	        {
	           assert(!st.empty());
	           auto it = (--st.end());
	           ans.pb(it->second);
	           ec[(it->first).first]++;
	           st.erase(it);
	           as--;
	        }
	        for(auto it:st)
	        {
	            if((it.first).first == i)
	            st.erase(it);
	        }
	    }
	    sort(ans.begin(),ans.end());
	    cout<<ans.size()<<"\n";
	    for(auto x:ans)
	    cout<<x<<" ";
	    
	}
    return 0;
}

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

In C2 tag is given meet in the middle but it is giving TLE . Dd anyone solve this using meet in the middle and binary search , please reply ...

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

can someone explain me D1 and D2,i tried to understand but was not able to get it.

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

How to optimize this meet in the middle + binary search to C2? 63895945

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

In problem E, why we are not considering the case when the person can go down. For example, if our cost is decreasing after going down and take the elevator or stairs, Why we are not considering that case in the editorial? Any help would be appreciated.

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

What is the shortest path solution for problem E ?

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

Any accepted shortest path solution for E please ?

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

What is the Bruteforce approach for C1-Good Number problem?

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

    Problem C1-Good Number.

    You can use BitMasks for this problem, since the largest good-number will not exceed 20000 which is equivalent to (3^9).

    Minimize the answer to the difference of the sum of the BitMask and x if the condition (sum>=x) is valid.

    complexity(9*(2^9))

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

Can someone explain the recurrence of problem F in detail ?

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

in editorial of C2 why if (vals.back() == 1) ans = pw / 3; we are doing this