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

Автор vovuh, история, 5 лет назад, перевод, По-русски

1118A - Покупка воды

Идея: MikeMirzayanov

Разбор
Решение

1118B - Таня и конфеты

Идея: MikeMirzayanov

Разбор
Решение

1118C - Палиндромная матрица

Идея: MikeMirzayanov

Я должен упомянуть этот блог, потому что он потрясающий! Это разбор этой задачи от MikeMirzayanov.

Разбор
Решение

1118D1 - Кофе и курсовая работа (легкая версия)

Идея: MikeMirzayanov

Разбор
Решение

1118D2 - Кофе и курсовая работа (сложная версия)

Идея: MikeMirzayanov

Разбор
Решение

1118E - Очередная задача про бал

Идея: MikeMirzayanov

Разбор
Решение

1118F1 - Разрезание дерева (легкая версия)

Идея: MikeMirzayanov

Разбор
Решение

1118F2 - Разрезание дерева (сложная версия)

Идея: MikeMirzayanov

Разбор
Решение
Разбор задач Codeforces Round 540 (Div. 3)
  • Проголосовать: нравится
  • +62
  • Проголосовать: не нравится

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

good contest!

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

nice!f2 is a interesting problem,and i learned a lot from it.

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

What's the proof of optimality for D for given coffee distribution-strategy ? Is it just intuitive or we can prove it ?

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

    Yes we can prove it.The approach is greedy. Consider the caffeine values 6 5 5 3 1 1 in order and consider the distribution for 2 days and in each day you are taking 3 cups.

    6 + 5 + (5 — 1) + (3 — 1) + max(0, 1 — 2) + max(0, 1 — 2) = 17

    If you consider this case 6 5 1 1 3 5 in order

    6 + 5 + (1 — 1) + (1 — 1) + (3 — 2) + (5 — 2) = 15

    This is because in the first case max(0, -1) + max(0, -1) hides subtracting 2 from the final output.

    Sorry for my bad example.

  • »
    »
    5 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +25 Проголосовать: не нравится

    If you can bear some algebra then you can better appreciate the proof. ;)

    So again let's sort it in decreasing order a0 ≥ a1 ≥ ... ≥ an - 1, and we consider the sum . Now there exists an m such that this sum is equal to : is strictly decreasing as i increases. In other words we can 'assume' that a0 through am - 1 are strictly greater than , and the rest at most .

    To show that this is optimally, let b0, ... , bn - 1 be a permutation of these ai's, and since we spread it over k days, at most k of the coffee cups are the first of the days (hence 0 penalty), at most k of them has penalty 1, and so on. Hence the sum is now at most . Choosing only those i's that are relevant, we see that this sum is also equal to .

    Now if p is the number of those i's satisfying , we can split the sum into two categories: the bi's contain at most m integers from a0, ... , am - 1 and the rest are not more than ; so . On the other hand, the sum (considering only those i's that are relevant) at at least so

    , as desired.

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

В задаче Е можно также выводить 1 2, 2 1, 1 3, 3 1, ..., 1 k, k 1, 2 3, 3 2, 2 4, 4 2, ....

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

.

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

Предложение по F2:

  1. Подвешиваем за вершину 1.
  2. Удаляем все листы i, для которых a[i] = 0 (Они не влияют на ответ).
  3. Для каждого цвета i ищем LCA тех вершин j, для которых a[j] = i. Обозначим как l[i];
  4. Запускаем dfs. Пусть мы в вершине i, проходимся по всем потомкам. Для потомка j. Если a[j] = 0, то ничего не делаем. Если l[a[j]] = j, то ничего не делаем. Если a[i]! = 0&&a[j]! = a[i], то сразу выбрасываем, что нельзя сделать хороший набор. Иначе в a[i] записываем a[j].
  5. После этого мы имеем следующее — все одноцветные ноды являются соседними. Тогда можно построить дерево, в котором одноцветные ноды сольются в одну вершину, а потом определить расстояние между слитыми вершинами как количество нулей плюс 1.
  6. После этого ответом будет произведение всех весов.

По коду явно будет сложнее, чем приведенное решение (попробую чуть позже реализовать), но интуитивно более понятно. И асимптотика будет так же O(nlogn).

P.S. Забудьте эту чушь, нашел контрпример:
6 3
1 0 0 2 0 3
1 2
2 3
3 4
3 5
5 6

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

E has another simple solution.
Traverse all pairs in increasing order (starting from (1, 2), (1, 3), ...) and print each pair followed by its flipped pair (not repeating any).

1 2  
2 1  
1 3  
3 1
1 4
4 1
2 3
3 2
...
  • »
    »
    5 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Yeah I also used the same strategy.

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

    i used this too, nice!

  • »
    »
    5 лет назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

    There is another solution where in we could start with say pair (1,2) and keep incrementing both the elements of the pair until the second element of the pair reaches k.

    After that, we flip the pair and keep decrementing both x and y until y becomes 1. Repeat this process and we get the correct answer.

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

    Yeah ! It is the simplest approach.

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

Can someone please explain me the tutorial of F1.

It would be of great help.

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

    I solved it in following steps:

    1. Suppose root of the tree is 1.

    2. Then by single dfs we find how many red and blue vertices are present in subtree of each vertex.

    3. Then we see each parent to child edge, if child's subtree contains 0 blue vertices and all red vertices(or 0 red vertices and all blue vertices) then this edge is good.

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

I think this contest have much tricky questions then previous div3 contest.

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

the contest was terrible and now seeing from this editorial you really didn't care about us. it's a joke, especially problem D. i'm at a loss of words trying to explain myself why you keep mixing math with programming. what connection do you see between the two? genuinely

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

In Problem B for n=1 ans should be 0, bcoz if there is only one candy, tanya will give that's one to his father, since now there is no candy she could not eat any day.(There is no candy). Shouldn't it be 0 ? (My soln got hacked just due to only this case which i made myself ans = 0 if n==1) :(

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

    No, because if dad eats the 1-th candy, sum of weights of candies Tanya eats in even days = sum of weights of candies Tanya eats in odd days = 0.

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

i've learned a lot from this contest, specially that i must read the entire problem set.

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

Can someone suggest some other approach for F2. Thanks.

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

    Hi, my solution is still similar to the editorial but maybe it can help.
    I first run a DFS to do a minimal merge of the components of the same color: I colorize the entire path between each vertex of color, and check for collisions. You only have to keep a count of number of components per color to know if the child has to give its color to his parent.
    Then instead of counting the ways of cutting, i count the number of valid colorings, where a color for a node is either "in subtree" or "not in subT". Then for the second DFS I just compute the possibilities for these two "colors". The formulas are similar to editorial.
    Solution in O(n log mod) runs in 0.5s, because I took modular inverse to obtain the product of "all but one".
    Hope it helps, my code here: 50352250

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

      Could You explain how counting works? I haven't really understood it from the tutorial. Although I understand all the rest.

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

        Either you already have made # of colors in subtree cuts in this subtree or you have made this number minus one. In my case corresponds to "is this node color in the subree?". You can think of DP0 as "am i done here? = has color of parent" so naturally you can keep a color and get from color of child to color of parent but not the converse. Then like classical tree DP you sum possibilities of disjoint events (like choosing one children color) and multiply the ones for independent events (like choosing a color for each subtree). This is not an easy DP pb because you have to keep track of both. Try to get the intuition behind the formulae before looking at tricks for computing it faster.

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

          Okey, I think I am starting to understand it now. One more question. How do we know in our dp that we cut exactly k-1 times? No more no less?

          I will try solving some easier problems with tags (trees, combinatorics) and then come back to this one. Thanks

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

            1: you checked there were no collisions during the minimal merging
            2: at each cut your separating the "top" color from the "bottom" one, the components stay connected. When you already cut it, a color disappears from the possibilities (DP1->DP0) and becomes a color not in subtree. DP0 means we are done w/ all colors of subtree, DP1 there is one left at the root of subtree.
            3: Since you want the root node to have a color from its subtree, you return DP1. Each color except this one has been cut -> K-1.

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

can anybody take a look to my post here

and help me to optimize my dp solution for the D1 to solve D2

thanks in advance

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

    Its painful to read code of other people sometimes. Let me know if you haven't understood from editorial.

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

Сдал Д2, закинул тот же код в д1 — ва :))

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

1118D1 — Coffee and Coursework (Easy version)

Let the current number of days be k. The best way to distribute first cups of coffee for each day is to take k maximums in the array

How do you know that it's the best way to distribute? I don't get that

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

    Checking on samples and doing some casework and observations, It is better to use maximum values initially because they may get depleted later on.

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

Need help with the problem F2. What I've done is-

  1. For every color, tried to build $$$k$$$ same-colored subtrees. If not possible (collision occurs), the answer is trivially $$$0$$$.

  2. For simplicity, let's call uncolored vertices as black vertices. When an answer exists, there will be many (possibly $$$0$$$) subtrees induced by black vertices, whose leaves will have many colored components(sub-trees) adjacent to them. Let's call the set of these colored component/sub-trees $$$S_T$$$ and the black components $$$B_T$$$. Instead of cutting $$$k-1$$$ edges, I tried to color the black vertices in a way such that:

a. only the trees in $$$S_T$$$ get larger

b. no new subtrees form.

c. all the vertices get colored.

Clearly, doing step 2 for all the black sub-trees in $$$B_T$$$ will lead us to the result. The answer will be the product of $$$count(coloring_.a_.black_.component_.in_.B_T)$$$. So, all I need is to find a way to count the colorings of $$$1$$$ black component. It should be an easy DP but somehow I'm failing to grasp it. The tutorial did not help (sorry). Any help is welcome. Thanks.

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

The way Mike has explained F2 is remarkable. Thanks Mike and Vovuh.

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

For problem F2 I have an approach. However I am stuck at some part Please read this blog for my approach. Note: For good coders you will find one more interesting problem to solve.