vovuh's blog

By vovuh, history, 6 years ago, In English

1133A - Middle of the Contest

Idea: MikeMirzayanov

Tutorial
Solution

1133B - Preparation for International Women's Day

Idea: MikeMirzayanov

Tutorial
Solution

1133C - Balanced Team

Idea: MikeMirzayanov

Tutorial
Solution

1133D - Zero Quantity Maximization

Idea: BledDest

Tutorial
Solution

1133E - K Balanced Teams

Idea: MikeMirzayanov

Tutorial
Solution

1133F1 - Spanning Tree with Maximum Degree

Idea: MikeMirzayanov

Tutorial
Solution

1133F2 - Spanning Tree with One Fixed Degree

Idea: MikeMirzayanov

Tutorial
Solution
  • Vote: I like it
  • +33
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
  Vote: I like it +32 Vote: I do not like it

Explanation of f2 is copy-paste of f1, please fix it

»
6 years ago, # |
  Vote: I like it +10 Vote: I do not like it

F2 tutorial is missing (F1 has been repeated). Also, The problem F1 really should've been ahead of the problem E (if you want to sort the problems according to their difficulties).

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

vovuh , F2 tutorial is missing

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Can F2 be solved by finding articulation edges / bridges in the graph and as soon as we find bridge edge we merge them and after that we pick any random adjacent edges of vertex 1 so as to make its degree exactly d. and after make the whole tree connected using kruskal type algorithm? Here is my submission for reference of what i am talking. I am getting wrong answer on test 37. Pls tell if i am wrong. Thanks

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

    The idea for F2 is that if we remove vertex 1 then the graph will be K components and if K > D there is NO answer otherwise we should connect this K components with D edges after that we can choose any MST of the remaining graph.

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

    hope this test case will help you to understand why you are getting wa :) 7 8 4 1 2 1 3 1 4 1 5 1 6 1 7 7 6 5 4

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The problem is that bridges tell us only that if that edge is removed,the graph will become disconnected, if u remove some set of edges which are not bridges it is still possible that the graph may become disconnected, that is why the above logic doesn't work.

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

Turns out that my solution for D with maps having long double keys passed. I checked the test cases and would suggest the problem setters to do the same too. From what it appears, the cases seem weird. I've linked a screenshot regarding my concerns.

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

why does this solution gets TLE whereas this solution gets Accepted?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

MikeMirzayanov you Have Mistakenly provide E question tutorial in F question. Can you please explain 1133F2 — Spanning Tree with One Fixed Degree solution idea.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

"How to do transitions from dpi,j? The first transition is pretty intuitive: just skip the i-th (0-indexed) student. Then we can set dp[i+1][j+1]:=max(dp[i+1][j+1],dp[i,j])."

i think it should be dp[i+1][j] as by skipping ith student we are not increasing size. MikeMirzayanov

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

    I think it's a typo. It should be dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j+1])

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I also stumbled upon this, will wait for the clarification of right transition..

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

      I'll will put the way I interpreted it.

      When you are in a given state (i , j) (where the students in [1,...,i] are the possible students you can use and 'j' is the maximum quantity of teams you can use) you have to take the best between pick or not the student 'i'.

      When you dont't pick student 'i' you will have to pick the solution for the case in which you have the students in [1,...,(i — 1)] as possible students to take and 'j' as the maximum quantity of teams you can use. This describe exactly what is in the state (i — 1 , j).

      When you pick student 'i' you have to see what is the best way to pick it. So you look at the states when you allow (j — 1) teams as maximum quantity of teams and students in [1,...,z] (with z <= i) as possible students (states of the form (z , j — 1) with z <= i). When you pick a state like that you could create a new team and put the maximum quantity of possible students (which are in [(z + 1),...,i]) in it to get a candidate for the state (i , j). But remember that you want the student 'i' to be in it. So you want to see only states (z , j — 1) (with z <= i) such that you could create a new team and put all students in [(z + 1),...,i] in it.

      With a good care this can be done in O(n*k): 51128813

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I will fix F2 tutorial in a few hours. Please wait a bit.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In D, why we should ensure numerator is non-negative?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    We should treat fractions and as the same one.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

@Vovuh How is ur editorial of F2 different from this? . Aren't they same?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In div2 D, Why does use of unordered_map gives a TLE but normal map works fine ? Solution 51071398 gives TLE with unordered_map but this 51071641 gets Accepted with map

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    unordered_map has O(1) complexity on average, but input can be engineered so that it take O(n) collisions for each access. Read more about it here.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to do problem C using binary search?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Sort the array in ascending order. For every element in the array (let it be arr) find how many elements satisfy  ≥ arri and  ≤ arri + 5. Take maximum length of all such ranges. See this code for the implementation.

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

For problem D we can prevent precision error by using BigDecimal for example in java, the largest fraction is 1000000000 / 999999999 = 1.000000001000000001000000001, the numbers after the 1. well represent the rounding we will use inside the BigDecimal class, 51105537

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In E. Why can't we just simply use two pointers. My idea is to sort this array then use two pointers as below a[n+1] = 2e9; for(int l = 1, r = 1; l <= r && r <= n+1;) { if(a[r] — a[l] <= 5) r++; else{ len[m++] = r-l; l = r; } } sort(len, len+m, greater()); for(int i = 0; i < k; ++i) ans += len[i]; But I get wrong answer on test 25. Can some one help me?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This example can help you: 10 1 11 9 8 7 6 5 5 5 5 5

»
6 years ago, # |
Rev. 7   Vote: I like it 0 Vote: I do not like it

DP in editorial for E is something different from its definition.

In that solution we just skip intermediate students (i, i + cnt_i), but in this case dp[i + z][j + 1] (z = 1 ... cnt_i — 1) is not increased as it should be.

Consider following example: n=3, k=1 a[] = 1 1 1

Then editorial code outputs: dp[i][j]=

0 0 
0 0 
0 0 
0 3

But by definition dp[1][1] = dp[2][1] = 1, because we form one team from these students.

Can someone please explain what dp[i][j] really stands for?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can E be solved using divide and conquer after sorting?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anybody tell me why this solution got TLE my submission

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain why the maximum number of students is possible only when there are exactly k teams as written in the solution for E?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

f2 is missing!

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

my solution for F1 https://codeforces.net/contest/1133/submission/51458164 is giving correct output on my computer but I am getting wrong answer on codeforces. I am not able to understand the reason. Can anyone help me out ?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I saw your code and ran it on ideone. It works fine on ideone. So I submitted it just to be sure and got WA, just as you got. Then I noticed the size of the vector you allocated in the bfs function. It should be n+1. I got AC after changing it. https://codeforces.net/contest/1133/submission/51472660 This is the AC code. Only the size is changed, nothing else. It worked on your machine and ideone probably because it allowed out of bound call, and luckily there was no garbage value in that memory.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem E, why can't we just loop k times, in each loop get the team with max members under the given condition, take the remaining students and continue till k teams are formed. My submission

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

    I was doing the same thing and got wrong on 52 as well, did you figure it out? Please tell me if you did.

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

      $$$k = 2$$$

      $$$arr = [1, 6, 6, 7, 7, 12]$$$

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can any one help why my binary search solution isn't working? https://codeforces.net/contest/1133/submission/55474924

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    When passing a vector to a function, you should pass it by reference, otherwise it may lead to TLE, just like it did for you (read more here). Also, in your binary search function, you do not define your variable "ans" when you create it, which can be problematic if you never set the value during your binary search.

    I would also suggest using standard C++ lower_bound or upper_bound functions when doing a binary search on a sorted array (or in your case, a vector). It might take some time and practice to learn how to use them but it will be really useful and you won't have to write your own binary searches when it is unnecessary.

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

      OMG, this silly mistake I was doing it whole time in many questions, thanks a lot for pointing out, just checked lower_bound function it's really helpful. all the three tips were helpful, BTW, can you help me with this dynamic programming question https://codeforces.net/contest/158/problem/E I am really stuck, not able to understand how to form the base cases (i,e. initial values of 2d array dp[][]). Thanks Again

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I can see that the editorial of that contest (link) has a few comments regarding this problem and you might find them helpful.

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Also, when you need help with a problem it is a good idea to ask for help by creating a separate blog for it, I have noticed that people tend to answer these more often than comments under blogs like this.

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

For 1133E — K Balanced Teams, I've used a backward DP that's my best friend, unlike the forward DP given in the editorial.

Here is my submission.

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

    Can you explain your solution?

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

      It's pretty much like in the editorial. The $$$cnt_i$$$ in my solution represents the number of students in a team, if the $$$i^{th}$$$ student is the last one in it.

      And the dp goes in the similar manner. The $$$dp_{i,j}$$$ represents the maximum total number of students, in upto j teams, while considering the first $$$i$$$ students.

      You can decide to take or not take a student, and the dp simply states that, in the for loops.

      It's been 3 months so I had to look into the solution itself again to understand what I'd done. Do let me know if you need more clarification.

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

Can someone help me figure out why am I getting TLE. I have written almost the same code as in the solution provided.84302770

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

    You can solve that problem C by map easily.

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

vovuh, editorials are not linked to contest materials of problems. can u please look into it?

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

Why in solution he shuffles the output ?

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

My idea forD, we can simply store -bi/ai in long long double in a map and then iterate to find the value for which maximum indices have the same value and it shall work https://codeforces.net/contest/1133/submission/264512885

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

Vovuh can stay on top of the contributors table just by organizing 1/2 Div. 3 rounds per month xD