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

Автор Sulfox, 6 лет назад, По-английски

1337A - Ichihime and Triangle

Idea: Sulfox

Tutorial
Solution by Sooke

1337B - Kana and Dragon Quest game

Idea: EternalAlexander

Tutorial
Solution by EternalAlexander

1336A - Linova and Kingdom

Idea: EternalAlexander

Tutorial
Solution by EternalAlexander

1336B - Xenia and Colorful Gems

Idea: ustze & isaf27

Tutorial
Solution by Sooke

1336C - Kaavi and Magic Spell

Idea: EternalAlexander

Tutorial
Solution by ouuan

1336D - Yui and Mahjong Set

Idea: Sulfox

Tutorial
Solution by Sooke
Solution by PinkRabbit

1336E1 - Chiori and Doll Picking (easy version) and 1336E2 - Chiori and Doll Picking (hard version)

Idea: Sulfox

Tutorial
Solution by Sooke
Bonus

1336F - Journey

Idea: EternalAlexander

Tutorial
Solution by EternalAlexander
Разбор задач Codeforces Round 635 (Div. 1)
Разбор задач Codeforces Round 635 (Div. 2)
  • Проголосовать: нравится
  • +257
  • Проголосовать: не нравится

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

This round has given me confidence. Thanks to the authors and the examiners!

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

Is there Chinese solution?

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

Thanks problem-setters for the editorial. I will try to solve C next time

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

will there be any dp solution for 635 div2 'C'?If yes, Please explain.

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

I have prepared video editorials, check them out for more insight about the problems.

Div2C/Div1A

Div2D/Div1B

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

is the note of div 2 E right?

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

Div2 C and Div2 D were great problems. Thanks to the authors of the round!

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

Thank you for funny legends!!!

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

Can someone please explain to me why Div.2 C got wrong answer on test 6? https://ideone.com/EhLzvj. My idea is that I create a vector from 1 to all the leaves. I then create another copy of it and increase the sum by the difference between each of the vectors. Can anybody help me and tell me how to get it to be accepted?

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

Why in div2 D they are using upper_bound and then decrement z instead of using lower_bound while iterating on z. When I use lower_bound it give wrong answer.

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

    And that is why you should read carefully the definition of these functions.

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

    Here's an example.

    #include <iostream>
    #include <set>
    using namespace std;
    
    int main() {
        set<int> a = {2, 4, 6, 8};
        cout <<
            // greater or equal, but no equal element
            *a.lower_bound(5) << "\n" <<
            // greater or equal, finds equal element
            *a.lower_bound(6) << "\n" <<
            // strictly greater
            *a.upper_bound(5) << "\n" <<
            // strictly greater, so ignores equal element
            *a.upper_bound(6) << "\n";
    }
    

    Output:

    6
    6
    6
    8
    

    Hope this clears things up.

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

      Thanks, now i got my mistake.

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

        Could you please explain why z is being decremented?

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

          Since we want the greatest value less than or equal to (let's call it) v, we find the number just before the least value strictly greater than v. There's some number just before and just after v in the set. (Or there are no numbers before, and that has to be handled separately.) upper_bound and lower_bound both give the number just after, and only are differ in whether they include v itself. The before and after are right next to each other, though, so finding the value just after and decrementing will get the value just before.

          In the example, suppose we have the set {2, 4, 6, 8} and we want to find the value just before 5. upper_bound gives us a pointer to 6, which is just after. Decrement the pointer, and you end up just before at 4.

          Now suppose we want to find the value just before 6, where equal to 6 is still ok. upper_bound gives a pointer to 8 because it never gives back the value itself. Decrement the pointer, and you end up at 6.

          I wish there was a standard function for finding the value before, but there isn't.

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

      If r = {5} g = {5} b = {1,7} we fix g = 5 and look for upper bound from b which is 7 and according to the editorial solution z-- will indicate 1 so red(5) <= green(5) <= blue(1) which is violating the context . Isn't it ?

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

    I also had the same doubt. Actually {x,y,z} are different in editorial and implementation. I will be explain using implementation code. You can figure out the difference.
    Some Definitions-
    lower_bound() — greater or equal to the element.
    upper_bound() — strictly greater than the element.

    These are steps we are doing -
    1. We are fixing x from vector a.
    2. We need to find z from vector c such that z>=x hence we use lower_bound.
    3. We need to find y from vector b such that x>=y hence we first use upper_bound to find element strictly greater than x then decrements it which guarantees that y<=x is followed.

    So y<=x<=z relation has been followed as mentioned in editorial(note the difference in conventions).

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

can anyone please explain the editorial of problem 1336B — Xenia and Colorful Gems?

I am not able to understand it?

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

Otherwise, if we choose its parent to develop tourism instead of itself, the sum of happiness will be greater.

Here is a mistake. Should be "the sum of happiness will be smaller."

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

Can anyone please explain Div2-D problem. I understood the core idea that we iterate over any one array and find closest int rest two but why is there 6 possible cases?

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

A bit different solution for 1336D:

Spoiler
»
6 лет назад, скрыть # |
 
Проголосовать: нравится -20 Проголосовать: не нравится

I tried using calculus in Div 2 Problem D(XENIA AND COLORFUL GEMS) but was not able to achieve a good equation has anyone done it with using Maxima and Minima approach , if yes Please explain it :)

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

This code is of div 2 problem A. Code

why it didn't got TLE as the range of the numbers is 10^9 and if the variable d=10^9 and and b+c=2 then the loop this code should give TLE

I think if loop goes beyond 10^7 or 10^8 it should give TLE

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

Is there a better explanation for Div1 C ?

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

    Let me try. I will explain the same dp but with another indexation and less cases to handle.

    Informal: Let dp[i][j] ($$$i\leq j$$$) be the number of ways to perform $$$j - i$$$ operations in such a way that the string we've built so far will be in the position [l..r) of the final string, provided that we did not perform invalid actions. For example, if $$$T$$$ is abac then dp[2][5] is the number of ways to build ac* where * is any symbol, counting that if we'll ever finish this as a spell then we add exactly 2 more letters to the beginning. This dp is easy to calculate using dp[i][j - 1], dp[i + 1][j], s[j - i] and probably t[i] and t[j - 1].

    More formal: Let's change the rules for our game. The new rules follow.

    We have an infinite sequence of free cells, numbered by $$$0$$$, $$$1$$$, $$$2$$$ etc. Before we start, we put a separator before any of the cells (either before the 0-th cell or between any two consecutive cells). In each turn we delete the first character of $$$S$$$ and put it either into the left nearest free cell to the separator (if any), or into the right nearest free cell. We cannot put a character $$$c$$$ into the sell $$$i$$$ if $$$i \lt m$$$ and $$$c\neq T_i$$$. After each turn we can claim our victory if the first $$$m$$$ cells are filled (by the letters of $$$T$$$, as follows from the rules).

    I state that the new game is equivalent to the old one. Indeed, putting the separator before the $$$i$$$-th cell in the new game corresponds to promising that we will add the character to the front of $$$A$$$ exactly $$$i$$$ times in the old game. I leave understanding of the relation between victories in these two games as an exercise to the reader.

    Now it's easy to calculate dp[i][j] which is the number of ways to eventually fill exactly the cells [i, j). The answer to the problem is the sum of dp[0][j] for j from $$$m$$$ to $$$n$$$.

    My dp calcuation:

    It's not necessary to calculate it in this order; one could also do for i := n..0; for j := i + 1..n.

  • »
    »
    6 лет назад, скрыть # ^ |
    ← Rev. 5  
    Проголосовать: нравится +78 Проголосовать: не нравится

    I myself failed to understand the tutorial too. I've read some comments under the announcement blog and have looked through some implementations.

    Assume that both strings $$$S$$$ and $$$T$$$ are of equal length, that is $$$m = n$$$. If it is not the case, append some special characters to $$$T$$$ that say that any character may be there. You have to count number of ways to build string $$$T$$$ using the two operations. Let $$$dp(l, len)$$$ be the number of ways to build string $$$T[l, l + len - 1]$$$ having used first $$$len$$$ characters of $$$S$$$. When you have a string $$$T[l, r]$$$ and you consider the next character, that is $$$S[r - l + 2]$$$, you can proceed to states $$$T[l, r + 1]$$$ and $$$T[l - 1, r]$$$, but only when $$$T[l - 1] = S[r - l + 2]$$$ or $$$T[r + 1] = S[r - l + 2]$$$ respectively. If $$$T[i] = ?$$$, a special character, then it is always true that $$$T[i] = S[j]$$$, for any $$$j$$$. To get the answer remember that you don't have to use all $$$\lvert S \rvert$$$ characters, you may stop after some steps, so you need to sum $$$dp(1, len)$$$, for $$$len \ge \lvert T \rvert$$$. I assumed that $$$\lvert S \rvert = \lvert T \rvert$$$, but you still need to store the initial length of $$$T$$$ before appending special characters to calculate the answer.

    Have a look at my submission 76955112. The hack I use when filling initial $$$dp$$$ state is proceeding up to $$$n + 1$$$ as without it I don't get correct $$$dp(n, 1)$$$ because $$$dp(n + 1, 0)$$$ is used.

    I would like to see the tutorial to be presented in a clearer way so I can understand how exactly our solutions are different.

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

Here is a solution for div2 C (detail explanation with code ) Check Here

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

    Is this a sort of blog of yours?

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

    You say that for a node with c children and at level x from root, if we choose as tourism city then it will contribute (c)*(x). But that presumes that all the ancestors are tourism cities as well. What if not all ancestors are tourism cities?

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

      There is no meaning of building industry on the ancestors (it wont maximize) Because if a node has c*x happiness (assuming all the ancestor is tourism spot) Then we will first build industry on that not on its ancestor

      And thats why if a node give c*x additional happiness its ancestor will give less for sure

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

    In step 4 you said that if a node has c children and it is at a depth of x,then it will give c*x happiness if we select it...select it as what?? Industry or tourism?? if we select it as a tourism city, then i think happiness will be c*(x+1).As depth of a node u means the number of nodes in the path [1,u).u is excluded. So if we include u as a tourism city then the number of tourism city in front of the c children becomes (x+1). So happiness becomes c*(x+1).

    I am stuck here. Please Help.

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

      Suppose Node A has depth x [1,x] x included A has c children and as its tree if we are going to select A we must have build industry in all of its children (remember A is still tourism we have not build anything) A as a tourism spot — c*x ( as all children will pas through x nodes) A as a industry — we have now (c+1) industry and (x-1) tourism spot so (c+1)*(x-1)

      Remember its tree so we are just thinking about that particular branch and then comparing it with others

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

    There are a few places where you have mixed tourism and industry and the total contribution by each node towards the total happiness will be (c-x).

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

Is there any specific reason for vector/set giving TLE in div1B ? I was trying to optimise my logic but couldn't come up with anything, so randomly changed vectors to arrays and it passed in around (1/15)th of the time limit. Got around 6-7 wrong submissions simply because of using vectors :/ I don't think I have ever gotten TLE before with n being 1e5 and the code running in O(n.logn) with vectors :(

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

Is std::nth_element faster than std::sort?

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

    Yes, it has Linear complexity since it doesn't sort the entire sequence. Rather, places in the nth position the number which should have been in that position if the sequence was sorted. The rest of the elements won't be in any particular order except for the fact that all elements before the nth element won't be greater than the nth element and all the elements after it won't be smaller.

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

I solved the Div2D 1337D - Xenia и разноцветные камушки a bit differently. I created an array $$$v = r \cup g \cup b$$$ and sort it with keeping track of from which set they come. Whenever I have found $$$3$$$ numbers, I calculate an answer and keep the best one. First, notice some observations.

  1. For an $$$rrrggggbbbb$$$ portion, it is better to take the latest $$$1st$$$ number and the earliest $$$3rd$$$ number in a sorted triplet to find the best result.
  2. For an $$$rggggb$$$ portion, the best $$$g$$$ would be the one which is the closest to $$$(r+b)/2$$$. One can easily prove this using some mathematical expression.

Once we read the first number (and know which set it's coming from), the $$$5$$$ cases are possible while iterating over $$$v$$$. Consider the array $$$v$$$ looks like this in the beginning.

$$$r_0 r_1 r_2 g_0 g_1 g_2 g_3 b_0 g_4 r_3 ...$$$

Cases:

  1. We are still reading the $$$1st$$$ type of number. If so, just take the latest one ($$$r_2 g_0 b_0$$$ is better than $$$r_0 g_0 b_0$$$, using observation $$$1$$$).
  2. A new type of number has been read. This is our $$$2nd$$$ number.
  3. Once we have found some $$$2nd$$$ number, we are looking for a $$$3rd$$$ one. If the $$$2nd$$$ number keeps repeating, we just take all of them to a temporary array.
  4. If the $$$1st$$$ type of number comes back instead of $$$3rd$$$ or $$$2nd$$$, make it the new $$$2nd$$$, and turn the old $$$2nd$$$ into new $$$1st$$$. ($$$b_0g_4r_3$$$ is better than $$$g_3b_0r_3$$$)
  5. Lastly, if we indeed reach a $$$3rd$$$ number ($$$r_2 g_0 g_1 g_2 g_3 b_0$$$ or $$$b_0 g_4 r_3$$$), we need to find the best $$$2nd$$$ number from the temporary array using binary search. Say, $$$r_2 g_1 b_0$$$ is the best we have found. After finding the result, make the current $$$2nd (g_1)$$$ into $$$1st$$$ and the $$$3rd (b_0)$$$ into $$$2nd$$$, waiting for an $$$r$$$ to occur and $$$gbr$$$ to happen. But wait, is that right? Not really. We indeed want a $$$gbr$$$ to occur, but the best $$$g$$$, in this case, is not $$$g_1$$$, it's $$$g_3$$$, the one that's right before the $$$b_0$$$ which we declared as the new $$$2nd$$$ (or the old $$$3rd$$$). It's similar to the Case $$$1$$$ described above. Be careful at this part.

This eventually goes over all possible triplets, and every time we find a triplet, we find the best middle ($$$2nd$$$) number using binary search. My submission: 76948887.

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

Why does finding 3 closest numbers using 3 pointers in the 3 sorted array(moving ahead the pointer with min value) fail in DIV2-D.Can someone please point out a case?

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

For Div2(C), my logic was to sort nodes ascending according to their degree(no of other nodes they are connected to) and then descending according to their depth. Then choose first k nodes as industry. Why is this approach incorrect ? Can anyone suggest a test case please?

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

EternalAlexander could you please simplify the notations of the problem div1 C?

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

Can Anyone explain the Top-Down Approach for Div.1 (C) 1336C — Kaavi and Magic Spell ???

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

Everyone seems to be solving 2E/1C in $$$O$$$$$$($$$$$$n^2$$$$$$)$$$ memory while tourist did in $$$O(n)$$$ — 76845451. Can someone who understands his approach please explain how it is different from the one discussed in the editorial?

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

For Div2 D. I used a simple 3 pointer search similar to this gfg link. My submission - 76926016

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

I dont understand the meaning of size(u) in problem C. Please explain me the reason why we subtracted it from the depth of every node??

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

Can anyone help me understand the approach div2 E Kaavi and magic spell? What is j represent here? Dp[i] [j] represents what?

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

Can anybody explain why my submission 76966044 for Div 2C is getting Runtime error on Testcase 9? My approach is the same, i.e calculate depth — (no. of nodes in it's subtree) for every node and then print the sum of the k largest elements.

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

Why the ans is b,c,c in the first question instead of b,c,d in the first question?

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

On 1337D i tried to sort all the three arrays, and for each fixed x, i would ternary search for y, but to make the decisions on this ternary search i would make another ternary search for z. Code . It did pass 22 test cases. It is not easy to find cases to make it fail.

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

Why once upper bound and lower bound is used? Why I can't use lower bound/upper bound twice? can squares return value zero? Can someone please explain?

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

    First lower bound used because you want to have a value which is not less than x(i.e greater than or equal to x).Second,upper_bound (with z--) to have the greatest value which is smaller than x in c,now we can't use lower_bound here because lower_bound will give a value not less than x(the value may be equal to x we don't want that we need strictly greater) for eg. say we have c= 2,3,6,6,6,7 and x=6 now when we use z=lower_bound(....)(which is wrong) z will be equal to 6 and z-- =3 but what we need is z to be 7 and z-- =6 which will minimise the difference.

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

Cant find Tutorial of 1337D Problem (Ps: I am new here can anyone help)

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

For div.1 E2 Lemma 3

How to proof that the linear space B has exactly $$$m-k$$$ bases ?

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

    First, it is clear that the length of $$$B$$$ is $$$m$$$ when $$$k=0$$$.

    Every time we add a new base to $$$A$$$ (also, $$$k$$$ is increased by $$$1$$$). $$$B$$$ is impossible to be $$$B$$$ before, we must remove some bases in $$$B$$$. If we remove more than $$$2$$$ bases in $$$B$$$, the length of $$$B$$$ is minus when $$$k$$$ is increased to $$$m$$$!

    So the length of $$$B$$$ has to be $$$m-k$$$.

    UPD: I found a much easier way to prove it. See it in the new tutorial.

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

I am trying to implement div2C, my code clearly runs in O(nlogn) time, but I keep getting TLE.

one of my subbmissions. I have checked many times but couldn't find what is wrong.

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

Another solution for 1336C:

Spoiler

This solution is easier to think,understand and implement than the editorial.

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

Can anyone re-explain E1 to me ? :'(

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

My observations and solution for Div2 C / Div1 A and Div2 D / Div1 B in the Announcement thread:

Div2 C

Div2 D

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

There is another straightforward to think greedy solution using hld for Div1A:

Let, $$$t(u) = \text{number of chosen vertices in subtree of }u$$$, $$$l(u) = \text{level of }u\text{ if we root the tree at }1\text{ (in particular }l(1)=0)$$$.

Maintain $$$f(u) = l(u) - t(u)$$$ for all free (not chosen) vertices.

Initially, $$$\text{ans}=0$$$. Then, do this $$$k$$$ times:

  • Choose $$$v = \text{arg}\,\max\limits_{u\text{ is free}}\,f(u)$$$.
  • $$$\text{ans} := \text{ans}+f(v)$$$.
  • To maintain $$$f(u)$$$, you need to decrease $$$f(u)$$$ by $$$1\, \forall u \mid u \in \text{path}(1 \rightarrow v)$$$. (Do this using hld)

Complexity: $$$\mathcal{O}(n + k \log^2 n)$$$

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

A question regarding 1336A - Linova и королевство tutorial: why are you saying that std::nth_element in STL implies an $$$O(n)$$$ solution? As far as I understand, std::nth_element works in $$$O(nlogn)$$$ time

Ref: https://en.cppreference.com/w/cpp/algorithm/nth_element

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

    In the page you mentioned, are you reading the correct footnote? Overloads 2 and 4 take an execution policy as an additional parameter, while overloads 1 and 3 are "Linear in std::distance(first, last) on average" according to your page.

    For more details, see https://en.wikipedia.org/wiki/Quickselect.

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

      Thank you, indeed, I misread the footnotes :)

      Then it's $$$O(n)$$$ on average and $$$O(nlogn)$$$ in the worst case, right?

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

      I reacted to the claim that the solution is $$$O(n)$$$, which is not the case, since big-O notation marks the upper bound of the function (time complexity), and not the average.

      With $$$1 \lt = k \lt n$$$ the solution complexity is still bound by $$$O(nlogn)$$$

      Similarly, you can't say that quicksort is bound by $$$O(n)$$$ — that's its average performance, but in the worst case it's bound by $$$O(nlogn)$$$

      Sorry for being pedantic, just wanted to point it out.

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

        You correctly state the average and worst-case complexities, and I agree with you that when someone says "a solution runs in O(something)", the implicit assumption is that they're talking about worst-case complexity. So the post should have said "O(n) average time" to avoid this confusion. Your concern is perfectly justified, in my opinion.

        I also want to point out your previous comment is absolutely correct too. Big-Oh notation can be used to denote an asymptotic upper bound to any function, and in particular you can use it to bound the average-case complexity as well as the worst-case complexity, and so "O(n) on average" is a technically correct use of big-Oh notation.

        Either way, nth_element could have been implemented with the Median of medians algorithm to guarantee worst-case O(n) complexity. In practice, the STL implementations we use rely on a variant of Introselect that combines Quickselect and Heapselect. Wikipedia seems to claim that the original Introselect combined Quickselect and the Median of medians algorithm for optimal worst-case complexity, but I can't confirm that.

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

i did not understood the editorial for problem C, it is not written well,to clarify my doubts sizu is the size of the subtree of u and depu is its depth ,consider for the last node of the tree with 5 nodes its sizu will be 1 and depu will be greater than 1 , now the author is saying the total happiness will be increased by sizu−depu: here sizu−depu=1-x ,x>1 (sizu−depu) will be a negative value,now how can negative value increase the happiness ???

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

In div1B my code is giving correct answer for test case 1. but on submitting it is sowing some different answer(instead of 1999999996000000002 some other answer is printing)77017823. please help.

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

The solution to E2 is very interesting, but the tutorial is a little bit hard to understand for me at the beginning. Here are some suggestions:

(1) It would be better to use "Walsh-Hadamard transform" instead of "FWT" here, because fast or not does not matter here. (It's not affordable to explicitly compute it anyway.)

(2) Similar to (1), I feel that "XOR convolution" is a little bit distracting here, because we only care about one term anyway. It made me feel that "we do Fast Walsh-Hadamard transform because we want to do XOR convolution fast", but this is not true.

(3) The proof of lemma 4 doesn't really make sense. "The $$$i$$$-th number of $$$F^c$$$ only depends on $$$popcount(i)$$$, it certainly still holds after Fast Walsh-Hadamard Transform." is true because the the property here is "popcount". If you replace "popcount" with something else it will be false. The formula you listed below should be the actual proof for this lemma.

If I'm going to explain this solution I will try to rephrase it as follows. The main purpose of doing Hadamard transform is to move the analysis from linear subspace $$$A$$$, which has high dimension ($$$k$$$), to another linear subspace $$$B=\{y:\forall x\in A, \langle x,y \rangle=0 \pmod 2\},$$$ which has low dimension ($$$m-k$$$). Hadamard transform provides a linear transformation between $$$[x\in A]$$$ and $$$[y\in B]$$$: $$$[x\in A]=\frac{1}{|B|}\sum_y (-1)^{\langle x,y\rangle}[y\in B]$$$. Therefore if we want to count how many vectors in $$$A$$$ satisfy some properties, we can enumerate every $$$y\in B$$$ and calculate its "contribution" instead. Formally, let $$$F_c$$$ be the set of all vectors which have $$$c$$$ 1s. Then we have $$$\sum_{x\in F_c} [x\in A]=\frac{1}{|B|}\sum_{y} (\sum_{x\in F_c}(-1)^{\langle x,y\rangle })[y\in B]$$$. If we simply enumerate every $$$c\in[m],y\in B$$$ and calculate the contribution of $$$y$$$ in $$$F_c$$$ by some combinatorial methods, we have a $$$O(m^2\cdot 2^{m-k})$$$ algorithm already. By observing that the coefficient only depends on the number of $$$1$$$s in $$$y$$$, this can be squeezed to $$$O(2^{m-k}+m^3)$$$ as in the tutorial.

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

Elegant problems and short, concise statements. Thank you for this fine contest!

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

Someone please explain Div 2 C briefly.

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

    since the capital city is at node 1 we have to place industries in such a manner that total sum of tourism cities is maximum for each of the industry

    greedy thought
    proof
    problematic situation which may occur to disturb our greedy solution
    solution to above situation

    hope it's clear now

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

solution for div 2 d is very elegant

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

I am trying to write an easier explanation for Div2E/Div1C.
Our target is to build a string of length $$$n$$$, which is initially empty.

1 2 3 ..... n

$$$dp_{i,j}$$$ is a state where we have already filled up all the positions in the range $$$[i+1,j-1]$$$.

1 2 3 ..... i i+1 ..... j-1 j ..... n
* ***** *

Now we will either add the next character at the beginning (at index $$$i$$$) if it is equal to $$$T[i]$$$ or we will add the next character at the end (at index $$$j$$$) if it is equal to $$$T[j]$$$. The first option will take us to the state $$$dp_{i-1,j}$$$ and the second option will take us to the state $$$dp_{i,j+1}$$$.
Our answer is $$$dp_{1,1}+dp_{2,2}+dp_{3,3}+.....+dp_{n,n}$$$.
Here is my submission.

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

For div2 C. When I submitted this solution. I got the Wrong answer at test case 7.

But when I submitted this solution it got accepted.

The only difference between these two starts from line number 34 in the wrong answer I declared the array res as vector int and for the right one, I declared it using pair of vector int.

So Why one is got accepted and the other one got rejected ?

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

someone please tell me that why we have added z==c.begin() condition in solve method in Div2. D in editorial I mean upper_bound is returning the smallest index which has greater value than the key searched so suppose if the min value of vector c is greater than the key the result will be c.begin() why would we do continue in this case (can't we form a triplet with value from vector c as c.begin() ) what's wrong with doing that

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

Why is this solution in Python giving Runtime Error on Case 9

76970117

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

Can anyone please explain { DIV2 : E. Kaavi and Magic Spell }. I am not getting it from the editorial. Not even the intuition.

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

Nice round :) Thank you ^^

btw, you don't have to actually hold the dp matrix in div1C, as the update only use the next location for each update, you can update it from left to right.

Here is a very short solution:

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

    It's really short. I didn't fully understand the solution from the editorial though.
    I was able to solve it using O(n) space too. But I had to store two rows since my current state calculates answer using the values from the previous row.

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

How to solve the bonus part of E2? :(

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

In Div1 E in this part "build linear basis A with given numbers", what do you mean exactly? Build a set with minimum number of elements of the input such that all the elements are linear combination of those?

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

    consider each number as a zero-one vector in $$$\mathbb{F}_2^m$$$, then find a basis of this vector space. Then, each given number can be written as the xor of a subset of this basis.

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

      In that case a base could be { (1,0,...,0,0) , (0,1,...,0,0) , ... , (0,0,...,0,1) }, right?

      I don't understand why $$$ ans_i = p_i \cdot 2^{n-k} $$$.

      Do you know a post where i can learn this?

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

        "In that case a base could be { (1,0,...,0,0) , (0,1,...,0,0) , ... , (0,0,...,0,1) }, right?"

        No, that's a basis of $$$F_2^m$$$, but we want a basis of the subspaced spanned by the given vectors.

        As for why $$$ans_i=p_i\cdot2^{n-k}$$$: let $$$V$$$ be the set of input vectors, and let $$$A\subset V$$$ be a basis of the subspace spanned by $$$V$$$. First we look at linear combinations of $$$V\setminus A$$$, then we consider linear combinations of $$$A$$$ to get a desired popcount. Choose any linear combination of $$$V\setminus A$$$, there are $$$2^{n-k}$$$ of these, and suppose the xor sum is $$$x\in span(V)$$$. Each $$$x$$$ that we get this way can be expressed uniquely as a linear combination of vectors in the basis $$$A$$$. Now consider one of the $$$p_i$$$ linear combinations of vectors in $$$A$$$ such that the resulting popcount is $$$i$$$, and suppose the xor sum of this linear combination is $$$y$$$. There is a unique way to use the basis vectors to go from $$$x$$$ to $$$y$$$, ie. writing $$$y-x$$$ as a linear combination of the basis. This gives a bijection that shows $$$ans_i=p_i\cdot2^{n-k}$$$.

        "Do you know a post where i can learn this?"

        This is linear algebra with a bit of combinatorics.

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

          I don't get one moment.. Suppose we get any linear combination from $$$V \setminus A$$$ which have xor sum $$$y$$$. Then we get any subset $$$X$$$ from $$$A$$$ that have popcount equal to $$$i$$$ and xor sum -- to $$$x$$$. How come that we're always able to express $$$y - x$$$ as linear combination of vectors from $$$A \setminus X$$$?

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

          Can you elaborate more on the part where you say "This gives a bijection that shows ans_i = p_i * 2^(n-k)."?

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

            Every linear combination of $$$V$$$ is the sum of a linear combination of $$$A$$$ and a linear combination of $$$V\setminus A$$$. One way to get a linear combination of $$$V$$$ with popcount $$$i$$$ is to choose a linear combination $$$x$$$ of $$$V\setminus A$$$ and a linear combination $$$y$$$ of $$$A$$$ with popcount $$$i$$$. Once we fix $$$x$$$ and $$$y$$$, we can put $$$x$$$ as the linear combination of $$$V\setminus A$$$, and $$$y-x$$$ as the linear combination of $$$A$$$. The number of ways to choose $$$x$$$ and $$$y$$$ are $$$2^{n-k}$$$ and $$$p_i$$$ respectively.

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

Can anyone please help?

Problem Div2 D : I have coded solution as per editorial, verified the data types of ans, vector. I am getting correct answer for the sample test on my machine, but on submitting I'm getting a different output.

wrong answer 2nd numbers differ - expected: '1999999996000000002', found: '2147483647'

submission 77118100

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

Can someone please tell me why am I getting WA on test case 6 on div2 C . My logic is to find the depth and the no. of children of each and every node. Find depth[i]-children[i] for all nodes and return the sum of the top k values Here's the code https://codeforces.me/contest/1337/submission/77153692

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

Can anyone suggest more good problems like DIV2 'C'(1336A)

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

I don't Understand Div 2 problem D can someone explain to me what's wrong in my idea Idea: First, assume the solution is of the form x<=y<=z fix x and found y by lower_bound(b.begin(),b.end(),x) then found z by lower_bound(c.begin(),c.end(),*y)

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

How to approach problem C. sizu−depu, how do we get to know about this? observation & intuition?

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

Hey Guys, If you want a solution for Div.2 B — Kana and Dragon Quest Game

Please check out this video!

https://youtu.be/DGo4IU143nQ

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

Can someone please explain Magic Spells ? Ashishgup I liked your code on this. Can you just tell, what is dp[i][j] in your solution ?

Thanks

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

For 1337C- Linova and Kingdom,

I did same but I was getting Runtime Error which must be due to recursion limit as it will be exceeded if we have all 10^6 nodes connected in a sequence.. So I tried to increase the limit to 10^6 but for that, I got memory limit exceeded error.

Then I tried to set it to 10^5, but for this one also I got Runtime error.

Can anyone suggest what should I have done?

»
6 лет назад, скрыть # |
← Rev. 5  
Проголосовать: нравится +18 Проголосовать: не нравится

I think I am missing something for Division 2 Problem C: Linova and Kingdom. I have the following problem with the author's solution:

When calculating increase in happiness, when we choose a certain node $$$u$$$ to develop into a tourism city, then the loss is $$$dep_u$$$, this is correct: since all the parent nodes are already taken by the lemma that the author proved at the beginning. But when we calculate the gain, it is, at that moment, equal to $$$size_u$$$. But when we color some predecessor of $$$u$$$ in subsequent (later) steps, then this gain value changes, because some nodes in the sub-tree of $$$u$$$ no longer remain as an industry city.

EternalAlexander, or anyone, please point out where am I wrong. Thanks!

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

    You can see that, when you colour node $$$u$$$ all its predecessors are already coloured in previous steps. Because for all predecessors of $$$u$$$ the $$$size-dep$$$ is greater than $$$u$$$.

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

      OK, sorting takes the predecessors first in a branch. But out of all the possibilities over all branches, how does sorting ensure that it takes the best possible node? Because the number $$$size_u - depth_u$$$ doesn't really make sense to me, because it loses its significance when we take a child of $$$u$$$, it's value completely changes. So why do we store these numbers in an array and sort them. I think this needs some kind of proof??

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

        We have proved that, in the optimal way, when you take node $$$u$$$, all its predecessors are already taken, otherwise you should take the predecessor first.

        So before we take node $$$u$$$, all the nodes in its subtree are definately not taken. So in the optimal way, when you take node $$$u$$$ the change to the answer is $$$size_u - depth_u$$$.

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

          Yes you have proven that, but how do you prove that out of all possible nodes in a given depth (all the nodes in previous depths are already taken), the optimal node is the node with the maximum value of $$$size_u - depth_u$$$? Because this number doesn't mean anything after a few steps. So why do we store it? And sort them?

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

            Ok, I'll try another way to explain.

            Here $$$k$$$ denotes to $$$n-k$$$, i.e. the number of tourism cities.

            Let $$$f_u$$$ be $$$size_u - depth_u$$$.

            First, the optimal answer is no less than the greatest $$$k$$$ of $$$f_u$$$. Because that's the answer when you take the first $$$k$$$ nodes.

            Second, the answer can not be greater than that:

            If there exists a way to make the answer greater, assume that the nodes you've taken are $$$u_1, u_2 \cdots u_k$$$, the sum in this way will be $$$f_{u_1} + f_{u_2} + \cdots + f_{u_k}$$$, because for any node taken, all its predecessors are taken, and you can calculate the answer in this way. And you can see this is no greater than the sum of the $$$k$$$ greatest $$$f_u$$$.

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

            The main idea is:

            1. In the optimal way, when you take node $$$u$$$, all its predecessors are already taken.

            2. If the nodes you take satisfy the condition above, assume the nodes you take are $$$u_1,u_2 \cdots u_k$$$, the answer is $$$size_{u_1} - depth_{u_1} + \cdots + size_{u_k} - depth_{u_k}$$$.

            3. If you take the greatest $$$k$$$ of $$$size_u - depth_u$$$, the nodes you take satisfy the condition above.

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

can someone please give solution for 1336C - Kaavi и магическое заклинание with proper explaination. I can't understand the editorial.

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

Does anyone know more problems like 1336C — Kaavi and Magic Spell ?

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

For Div2D/Div1B My Approach is very similar to Editorial's. Its failing System Tests.

We assume the solution is of the form x<=y<=z. Then Fix x and find y by lower_bound(b.begin(),b.end(),x). Then find z by lower_bound(c.begin(),c.end(),*y)

We do this for all 6 Possible Combinations of Arrays r,g,b.

Code

Please Help!

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

1336B — Xenia and Colorful Gems auto y = lower_bound(b.begin(), b.end(), x); auto z = upper_bound(c.begin(), c.end(), x);

why is z-- but not z = lower_bound(c.begin(), c.end(), x)?

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

There was a huge mistake in the tutorial of 1336F - Путешествия. I have fixed it now. Thanks sys. for pointing out!

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

In problem e1 the soluution has this written on it Build linear basis A what does this mean ?

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

Can anyone help me finding the error as my code is getting wrong ans on test case 6 problem A

My Code

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

nice problem div1A problem

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

I want to take some time to explain the Div2C/Div1A's solution (Linova and Kingdom).

So the tutorial already mentions that we need to get the $$$val_u = size_{u} - depth_{u}$$$ for each node, and then sort the values, such that the bigger ones are first, and select the first $$$n-k$$$ values, and those are the ones that are selected as the tourist spots. Also, the summation of the $$$val_u$$$ for these $$$n-k$$$ values would give us the answer, But why?

So why are we taking $$$size_u$$$?

This is taken, while assuming that all the nodes under the current node will be industries. Now, if our current node is a tourist node, then in that case, each of the industry in the subtree will count this tourist node as one. Therefore we take the $$$size_u-1$$$ value. (The -1 is cancelled out later)

Why are we decreasing $$$depth_u$$$?

All the parent nodes of the current node would have taken the current node as an industry (as per the $$$size_u$$$ calculation). Now, since we are considering the current node as a tourist spot, we need to remove the "industry assumption" values from the total. This can simply be done by counting the number of nodes from 1 to the current node. That will be $$$depth_u-1$$$

The -1s cancel out, so we get $$$size_u - depth_u$$$.

Now this works because the sorted order forces us to go from parents to children. I hope this helps.

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

    can you please explain why this assumption is correct? This is taken, while assuming that all the nodes under the current node will be industries.

    Because it may happen that we assumed that all the subtree will be industries but we did not take all the subtrees

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

      Yes it can be false definitely. That's what the $$$-depth_u$$$ part is for. So you add $$$size_u$$$ assuming that all the nodes under the current node will be industries. Let's say this node as N, whose children we have assumed to be industries.

      Now let's say, when you reach some node A, in the subtree of N, which is to be taken as a tourist spot. In that case, the assumption for N becomes wrong, and you remove need to remove A's contribution from all it's parents. That's just $$$depth_u - 1$$$.

      So this handles the scenario when the node in the subtree is not an industry.

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

An interesting fact: The crucial step in the solution of E2 is known as the MacWillams identity in coding theory: Let the generating function of the words in a linear code $$$C \subseteq \mathbb F_2^n$$$ be $$$F_C(x, y) = \sum_{w\in C} x^{n-|w|_1} y^{|w|_1}$$$, then

$$$ F_{C^\perp}(x, y) = \frac 1{|C|}F_C(x+y, x-y). $$$

Where $$$C^\perp$$$ is the dual code $$$C^\perp = \{ v : \langle v, w\rangle = 0, \forall w\in C \}$$$.