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

Автор Gol_D, история, 2 года назад, перевод, По-русски

1611A - Make Even

Идея: MisterGu

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

1611B - Team Composition: Programmers and Mathematicians

Идея: MikeMirzayanov

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

1611C - Polycarp Recovers the Permutation

Идея: MikeMirzayanov

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

1611D - Weights Assignment For Tree Edges

Идея: MikeMirzayanov

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

1611E1 - Escape The Maze (easy version)

Идея: Vladosiya

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

1611E2 - Escape The Maze (hard version)

Идея: Vladosiya

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

1611F - ATM and Students

Идея: Gol_D, MikeMirzayanov

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

1611G - Robot and Candies

Идея: MikeMirzayanov

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

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

Автокомментарий: текст был обновлен пользователем Gol_D (предыдущая версия, новая версия, сравнить).

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

By the way, in F many of you have written a two-pointer solution and I liked it. If anyone wants to write a proof, please.

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

    Correct me if I am wrong

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

    Let us assume the max possible index(MPI) for some index L is R. Now for L+1, there are 2 possibilities:

    • The MPI for L+1 >= R, which means 2 pointers would work normally.
    • The MPI for L+1 < R. The reason 2 pointers work in this case is the fact that if MPI for L+1 is < R, it doesn't even matter to us because we need the max interval and this will definitely be less than the size of [L,R]. Thus for any useful range with start index > L, MPI must be greater than R.
  • »
    »
    2 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I didn't realize 2 pointers solution worked, so I wrote segment tree + binary search. 2 pointers is much cleaner :)

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

      how would you solve it if the problem were to find largest subsequence instead of contiguous subsequence ??

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

        Several observations:

        1. If we fix the leftmost position of the contiguous subsequence we chose to employ, we can binary search on our rightmost position. This is because choosing to help one more person on the right is always more expensive than not choosing to help that person.

        2. How do we know if we can help a contiguous subsequence $$$[a_l, a_{l + 1} \dots a_{r - 1}, a_r]$$$? We can only help if $$$a_l, a_l + a_{l + 1}, a_l + a_{l + 1} + a_{l + 2} \dots a_l + a_{l + 1} + \dots + a_r$$$ are all positive. That is, if all the prefix sums are positive, or alternatively that our minimum prefix sum is $$$\ge 0$$$.

        3. How do we quickly calculate minimum prefix sums which have some left offset? Just notice that prefix sums starting at $$$a_l$$$ are exactly the same as prefix sums starting at $$$a_0$$$, except we subtract off $$$pref[l - 1]$$$.

        4. So in short, we can find the minimum prefix from $$$a_l \dots a_r$$$ by computing prefix sums $$$a_0 \dots a_r$$$, finding the minimum on the interval $$$[l, r]$$$, and subtracting off $$$pref[l - 1]$$$.

        You can check out my submission here: 136938974 if you are still confused. Also, there's no need to cheese in this problem, but if you did need to cheese, you could be sparse table instead of segment tree for minimum, since the prefix sum array is static.

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

          yeah i understood this one, but i asked a different question..i asked if we could solve this problem in O(NlogN) when we are asked to find longest subsequence instead of longest contiguous subsequence

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

            I assume what you meant is creating the longest subsequence by removing some elements in the original sequence.

            In this case, we can try to solve the problem greedily.

            First we construct a minheap.

            Then we will iterate through the original sequence using a pointer i.

            • If the ith element is negative, put it in the heap.

            • When the sum of all elements from 1 to i becomes negative, we will remove the element with the smallest value using the minheap we just constructed. Since the removed elements will have the smallest possible value, I think the resulting sequence will be the longest possible sequence.

            I couldn't prove/disprove this idea, but it seems to work.

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

    In China we call this solution "Monotone queue", maybe you can get some information by searching this word.

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

    let's say p[i] is a prefix sum from L to i, by definition every i for p[i] must be greater or equal to -s

    suppose we want to add a[R+1] to our subsequence

    1.if p[R] + a[R+1] < -s, we must move our L pointer, it means we will remove some p from L to R let's say it p[j], then (p[R] + a[R+1]) — p[j] must be greater or equal to -s.

    (p[R] + a[R+1]) — p[j] >= -s

    (p[R] + a[R+1]) + s >= p[j] -> means p[j] < 0

    what will happen with other p where j < i <= R when we removing p[j], by definition every p >= -s, since we are removing p[j] and p[j] < 0 and p[j] >= -s, then every p where j < i <= R will lose p[j],so it means any p from our new subsequence won't be less than -k

    2.if p[R] + a[R+1] >= -s we can just move our R pointer to R + 1

    solution: here

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

Alternative solution for G:

We have to cover the candies with chains. According to Dilworth's theorem it is enough to find the longest antichain. (set of candies, where it is impossible to collect two of them in the same turn).

Two candy $$$a$$$, $$$b$$$, (with the same parity) can be in the antichain if their width differnce is strictly larger than their height difference ($$$|y[a]-y[b]|>|x[a]-x[b]|$$$)

This motivates a dp solution $$$dp[i][j]$$$ — the longest anticahin from the first column ends $$$(i, j)$$$. The transitions are easy: $$$dp[i][j]$$$=max($$$dp[i-1][j-1]$$$, $$$dp[i+1][j-1]$$$) and $$$dp[i][j]$$$=$$$dp[i][j-2]+1$$$ (if there is a candy in (i, j).

The time and memory complexity is $$$O(n*m)$$$.

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

    That was what I did in the contest. I was just a bit dumb and didn't realize that I could've just done a bit of dp. Instead, I solved with LIS, so the code got much longer than needed. Well, the final code was really similar to Baltic OI — 2009 — Candy.

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

Did anyone else solve E1 using LCA?

I solved E1 using LCA and a bruteforce-ish approach. For every leaves, I brute through all the enemy vertices in the list and then used LCA to calculate the distance from the leaf to the enemy and from the leaf to the root in $$$O(logN)$$$. I did a little optimization that if the leaf is an enemy vertex, I skip it.

In the worst case the complexity should be something around $$$O(N * k * logN)$$$ with $$$k$$$ is the number of enemies, since it is possible to build a tree with $$$N - 1$$$ leaves. Still not sure why my code didn't got FST, probabilistic magic maybe?

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

my simple two pointer solution for problem F:

solution for F

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

    this test

    1
    8
    1 1 2 3 4 5 1 7
    1 7 2 3 4 5 6 8
    

    get wrong answer distant to 8 is 7, distant to 5 is 8, but 5 earlier then 8 it's solution don't work because you don't check distant from root of tree.

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

Is this a bug? The editorials to the problems aren't linked in "Contest Materials" yet, only the Announcement is linked.

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

I think, my solution for F is easier than editorial's.

Time complexity: O (n * log ^ 2 (n))

Link: 137119559

P.S. you can use sparse table instead of segment tree.

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

    You can use sliding window which is O(N)

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

      how do we decide which one to slide?

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

        use a prefix sum (or just a sum), try taking next value at j. if S + range_sum(i, j) < 0, then its not possible to take elements up to j starting at i, then move i until it is possible to do so. At this point, you then increment j and repeat the process.

        something lile this:

        while(j < N) {  
           while(i <= j && S + range_sum(i, j) < 0) i++;  
           ans = max(ans, j-i+1);  
           j++;  
        }
        

        using this idea, you know that every i already tested cannot excede the current j, (because at some point S + range_sum was less then 0). At the meantime, we are removing all i that is not usefull (doesn't get to j).

        I think that the last question is: "Given an invalid window (i, j), what if I test an new_i (new_i > i) that can get to j, but there is some point x (new_i <= x < j) such that S + range_sum(new_i, x) < 0??", that is not possible because, initially our current window was invalid and:

        1. if we remove a positive prefix of our window (i, j), then S + range_sum(new_i, j) is also invalid (less then an invalid range). That is the case explained before.

        2. if we remove a negative prefix of our window (i, j), as S + range_sum(new_i, x) < 0, adding the removed negative prefix + S + range_sum(new_i, x) is also less then 0. Which means that S + range(i, x) is negative, therefore, j shoud never be greater then x, as j is the first invalid point. This is a contradiction.

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

I think it should come like "Consider a vertex p[i], (2≤i≤n)" instead of "(2≤p[i]≤n)" in the editorial for D.

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

I did same as it written in the editorial.

By running a breadth-first search at the same time from each vertex with a friend, we can find the shortest distance to any friend from each vertex and by running from the root — the distance to the root. Now let's just go through all the leaves and check if there is one among them that the distance to the root is less.

Here is my implementation

What I did in my implementation is that

First I found the distance of every vertex from the root and stored it in the vector distance_from_root.

second, I found the minimum distance of every vertex from every friend, What I mean is that if the distance of a vertex x is 1,2,3 from friend x1,x2,x3 respectively then I stored the minimum of it i.e., 1 for vertex x in the vector distance_from_friend and then I checked for this condition as it is given int the editorial. Thus, Vlad can win if there is such a leaf (which, by condition, exits) for which the distance to the root is less than the distance to any of the friends.

But it is showing TLE. Please Help!

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

    You run bfs from each friend, it's O(n*k). In the editorial, It was meant to run one bfs which uses all friends as start vertices like it is in solution.

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

I tried to solve "1611E1 — Escape The Maze (easy version)" using approach of traversing from friends up (parents) and from root (Vlad) down (to leaves) and at the same time. When friends visit some node I mark it as visited and that prevents Vlad to traverse through that node down. This approach gives me incorrect solution and I don't know why. Any ideas? Submission: https://codeforces.com/contest/1611/submission/136924541

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

Can someone explain to me my own approach for C xD? During the contest this seemed natural to me, but now I cannot convince myself why this actually works. In short, I compare leftmost and rightmost elements of a and whichever is smaller goes to the corresponding side in p (if a[left] is smaller, a[left] goes to beginning of p, otherwise a[right] goes to end of p)

Link

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

Can someone explain solution of E1?

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

    Here is my solution for E1: https://codeforces.com/contest/1611/submission/137730474 I did not use the color thing, even though I can understand it now.

    For my solution, I used a queue, a distance array, and simply made 2 BFS. The first BFS helps to know the distance between any vertex and Vlad's friends. This is feasible by putting all the friends' nodes as the sources (you put them in the queue and put the distance as 0). Why is that useful? Because we want to know whether a friend will be able to catch Vlad on a certain node. This is the case if the distance from the node to Vlad is bigger or equal to the distance from the node to the closest friend. Now that we have this distance array, we simply make a BFS with Vlad as the source, and verify that the distance from Vlad to each node is strictly smaller than the distances previously computed. If we end up on a node verifying this condition and this node appears to be a leaf, we know Vlad can escape without being caught!

    I hope my explanation is clear, I had also put some comments in the code to make it understandable.

    P.S.: for the color solution, it is basically the same but more optimized: we simulate both DFS at the same time. Vlad and his friends are the sources. Every node visited is colored with Vlad's color or his friends' color, IF it was not already colored. An important detail here is that we do not want Vlad to end up on a node that is at the same distance from one of his friends. To take this into account, we put Vlad's friends in the queue before him, so that they get to move before he does (color of friends has a higher priority than Vlad's color).

    Let me know if something is not clear!

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

Can anyone spot the mistake in my code .. I am getting a runtime verdict on this code https://codeforces.com/contest/1611/submission/137548112

Thanx in advance

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

Can someone explain me the editorialist's Solution for G or anyother solution . It's been 1 day me trying to understand that . I did understood the solution provided by peti1234 in comments section .

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

I can solve problem G in O(n*m) by find path for each robot.

I divide robot into 2 parts, the same as MikeMirzayanov's solution.

With each part, while cell 1 exist, i go from top to bottom, with each row:

  • Get the first cell is 1, let's say p[i].

  • If robot can go to this cell, add i to list Q.

  • Else if column that robot exist smaller than this cell, skip this row, that mean p[i] < p[Q.back()].

  • Else, remove Q.back() from list, until robot can go to this cell.

After, make all cell in list to 0.

If we save p[i], p[i] always increase, and the loop is not overate m times, so complexity is O(n*m).

P/s: Sorry for my bad english.

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

Multi-Source BFS is amazing

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

In problem F,as per the equation in editorial,for each l in prefix array there would be value for right index in prefix array, given by p[r]<p[l-1]-s,so cant this this be implemented with a method of finding next greater element,with a little modification?

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

why my solution is not correct... WA at TC-2

210089349