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

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

1076A - Минимизация строки

Разбор
Решение (Vovuh)

1076B - Вычитание делителя

Разбор
Решение (PikMike)

1076C - Мемная задача

Разбор
Решение (adedalic)

1076D - Удаление ребер

Разбор
Решение (BledDest)

1076E - Вася и дерево

Разбор
Решение (Ajosteen)

1076F - Отчет по летней практике

Разбор
Решение (PikMike)

1076G - Игра на массиве

Разбор
Решение (BledDest)
  • Проголосовать: нравится
  • +49
  • Проголосовать: не нравится

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

My solution to problem E:

first for each depth sort all vertices by starting time and for each query and vertex U with starting time between starting time V and finishing time V, we add x to ans U (by partial sum, ask in reply for more information). then for all vertex U with starting time between starting time V and finishing time V, in depth height[v] + d + 1, we subtract ans[U] by x (by partial sum). and after all queries we run a dfs and find ans for each vertex.

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

    Fun to see someone that used the same idea (:

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

    I know it's quite old comment but still (by partial sum, ask in reply for more information) please explain the partial sum approach. Thanks

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

The problem E can be solved with complexity O(N + M) without sorting or any trees by the next way. We can remember all queries for each vertex V as pairs (X, D), which mean we have to add X to all such vertexes U in the subtree of V that the distance between U and V isn't greater than D. Then we can do DFS.

Let ANS be the current answer. Also we have to get an array CHANGE meaning that when we visit a vertex V we have to decrease ANS by CHANGE[depth[V]], where (obviously) depth[V] means depth of V. So when we visit a vertex V, for each pair (mentioned above) in V we have to increase ANS by X. Also we have to add X to CHANGE[depth[v] + D + 1].

And we must decrease ANS by CHANGE[depth[V]] once. So answer[V] = ANS. When we leave V, we must undo all changes we did in this vertex.

That's all. (I hope, I was clear).

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

    Thanks, your strategy worked perfectly for me.

    I realized this problem is basically a generalization of a problem for arrays where one begins with $$$a[\cdot]$$$ all zeros, and receives updates of the form $$$(l,r,x)$$$ meaning $$$a[k]+=x$$$ for every $$$l \leq k \leq r$$$, and after all updates, one needs to recover the array $$$a[]$$$. One could use segment trees for a $$$n \log n$$$ solution, but as in this case it is overkill because queries occur after all updates. (for future readers, the linear time solution for the array problem is to keep an extra array $$$change[\cdot]$$$ with the operation $$$change[l] += x$$$ and $$$change[r+1] -= x$$$ for every update)

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

I cant understand this implementation for D.

if(d[to] > d[k] + w)
			{
				q.erase(make_pair(d[to], to));
				d[to] = d[k] + w;
				last[to] = idx;
				q.insert(make_pair(d[to], to));
			}

How does he erases something from the set q berfore adding it?

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

Why does the following implementation for problem D is giving TLE in TC 67 (I have followed the idea given in editorial) . Here is my implementation [CODE]

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

    It's because you used Java.

    On the serious side though, it's because in dijkstra, you need to ignore the node if you've already visited it. Add a line like if (vis[p.st]) continue;.

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

sorry for my english but in problem D, according to your algorithm, asume that we need to find the shortest path from vetex 1 to all the others, so we just need n — 1 edges and from that n — 1 edges, we can get all the shortest path from 1, right ? (It just like MST, in MST we need n — 1 edges to get MST, but now, we also need n — 1 edge to get all the shortest path from 1 to another vetex)

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

sorry for my english but in problem D, according to your algorithm, asume that we need to find the shortest path from vetex 1 to all the others, so we just need n — 1 edges and from that n — 1 edges, we can get all the shortest path from 1, right ? (It just like MST, in MST we need n — 1 edges to get MST, but now, we also need n — 1 edge to get all the shortest path from 1 to another vetex)

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

    Yes. The tree we are building this way is called shortest path tree. In fact, Prim's algorithm of building MST and Dijkstra's algorithm of finding shortest paths are really similar, so we can use the tree built by Dijkstra's algorithm to store all shortest paths.

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

can anyone help me to figure out what's wrong with my code problem 1076D — Edge Deletion https://codeforces.com/contest/1076/submission/45743114

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

    Suppose there are two edges like (1, 21) and (12, 1). toString(u) + toString(v) will return a string "121" in both cases, so there is a collision. Maybe adding a delimeter or using a pair of ints instead of a string will help.

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

About G: It turns out that it can be reduced to O(m(n+q)logn) if we will use the distance to closest losing state instead of a mask of winning and losing states.

I don't know how to maintain the status by this method. Can anyone explain this?

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

    Well, in my solution I consider all possible masks of next m states of dynamic programming. As far as I understood some participants' solutions, they use the fact that only the next winning state matters, so, for example, masks like 01001 and 01000 are exactly the same and can be represented as 01xxx. So we may consider m + 1 different situations: m for every considering the distance to the next winning state, and an extra situation when the next winning state is too far away to be reached.

    Maybe Anadi can explain this idea better.

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

      In my solution I want to know for every position i — can I win if I start in this position. Let's say that if I can win then this position is good. I say that if ai  ≡  0 mod 2 then position i is good. Why? Because if there is a position j such that it is not good then I can move to it, otherwise I can stay in the same position and my opponent has to choose a winning position for me.

      Using this fact we can easily solve this task in O(nq) — we pick last position for which we don't know answer, if it's even then it's good, otherwise it's bad and we can mark previous m positions as good.

      We can simulate this process faster if we divide out array into blocks of size so final complexity is . It was possible to solve it faster in with segment tree (it's practically the same as model solution).

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

      Thanks for the help:) I think I've understood

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

What does the last array in the code for problem D do? Thanks

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

    This array maintains the index of the last edge on the shortest path from 1 to every other vertex.

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

Я чё-то не врубаюсь в последнюю часть разбора задачи G. Дерево отрезков содержит композиции на всех отрезках типа [k2l, (k + 1)2l].

Допустим на длинном отрезке прибавляем нечетное число. Отрезок может перересекается с O(n) отрезками из дерева отрезков. На каждом из них функции менять времени не хватит. Надо как-то запомнить что часть девера надо флипнуть, а потом для каждого отрезка во время вычисления запроса типа '2', быстро узнать надо флипнуть или нет.

У меня вроде тогда дополнительный log(n) фактор вылазит. Что не так?

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

    Отложенные вычисления в этой задаче выполняются абсолютно так же, как и в любой другой задаче с запросами типа "прибавить на отрезке" и "посчитать что-нибудь на отрезке": каждый раз, когда мы заходим в вершину, если в ней есть отложенный запрос, проталкиваем его вниз по дереву. Так как запрос проходит только по вершинам отрезка, то и проталкиваний в каждом запросе будет .

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

awoo There's a typo in the editorial of problem F. The following 2 lines are the correct ones.

1) ... the smallest number of separators you can have is ...

2) ... The smallest number of separators is ...

The expressions used in code, however, is correct.

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

How to solve problem E(Vasya and a Tree) for online queries?

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

Why this: 1+(n−d)/2 for Problem B?

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

    Let d be the initial smallest prime divisor for n.

    After finding d, we subtract it, so we have done 1 operation total.

    Now, n-d (n after subtracting d) is guaranteed to be even, so for each subsequent operation we will always find 2 and subtract it. We will do (n-d)/2 operations like this.

    So we do 1 + (n-d)/2 operations total.

    Why is n-d always guaranteed to be even? There are two cases:

    1) n is even. So d is even. So n-d is even because even-even=even.

    2) n is odd. So d cannot be 2 and 2 is the only even prime number. So d is odd. So n-d is even because odd-odd=even.

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

In problem C, how do we get a, b = (d±√D) / 2?

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

For Problem C I tried using a binary search solution. Link: https://codeforces.com/contest/1076/submission/46646184

I'm doing binary search from 0 to d. I take the mid element as a and find b = d — a. To break out of the binary search I check if a*b == d and return a or if mid becomes equal to either a or b (I figured they might after one point because of precision) in which case I return -1 indicating it is not possible.

This is not working for all test cases and I think it's off by some precision cause it isn't passing the sample. What is the correct way to do this if it were to be done using binary search?

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

Help required in problem 1076D-Edge Deletion. http://codeforces.com/contest/1076/submission/59660020

What I am doing is over here is,first find the MST of the given graph. And then run a dfs on this MST and keep adding edges until the count is less than k. What is wrong with this approach??

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

    test case (format as of problem first number is vertices in graph , 2nd is edges , k , then edges description) 3 3 2 1 2 2 2 3 1 1 3 2 Draw MST and see the difference for vertex 3 , shortest distance via mst approach will be 3 unit , but if you just considered simple edge of 1-2 and 1-3 , shortest path to vertex 3 will be 2 unit. Hence mst may cause problems , better to use dijkstra from single source.

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

I learnt a lot from Problem E. Thanks.