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

Автор BledDest, 4 года назад, перевод, По-русски

1525A - Зельеварение

Идея: adedalic

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

1525B - Сортировка перестановки

Идея: BledDest

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

1525C - Столкновения роботов

Идея: BledDest

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

1525D - Кресла

Идея: BledDest

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

1525E - Assimilation IV

Идея: BledDest

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

1525F - Гоблины и гномы

Идея: BledDest

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

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

Thanks for this nice and clear tutorial

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

The author's implementation of task C is much better than mine.

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

Finally editorial is out.

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

In Problem D I find this sentence very confusing:

$$$ $$$ "let $$$dp_{i, j}$$$ be the minimum time if we considered $$$i$$$ first positions and picked $$$j$$$ of them as the ending ones."

What does "pick $$$j$$$ of them (them='$$$i$$$ positions'?) as the ending ones" mean? Shouldn't it be:

$$$ $$$ " let $$$dp_{i, j}$$$ be the minimum time if we considered $$$i$$$ first starting positions and $$$j$$$ first ending positions."

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

    This state represents a situation when we've considered the armchairs $$$0, 1, 2, \dots, i - 1$$$ and exactly $$$j$$$ of them are chosen as ending positions (so, after the whole process is done, exactly $$$j$$$ people will sit on the segment $$$[0, i - 1]$$$ of the armchairs).

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

      I didn't get the thing. What exactly dp[i][j] is representing? Can someone help? Thanks!

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

        The representation of dp[i][j] is that we have considered first i seats and we have made the consideration of the first j seats on which the people were sitting initially and have given them some new position.

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

        I've managed to solve this task now. My understanding/interpretation is not exactly the same as in the tutorial, but maybe this image (which I also drew for myself while solving) will help you.

        I counted the ones and the zeros in two seperate arrays. the $$$x_i$$$ and $$$y_j$$$ save the index in the original array (so $$$x_1=0$$$ and $$$y_2=2$$$ in my picture). Then $$$i$$$ says us, how many People we chose, $$$j$$$ tells us, how many empty chairs we chose. And $$$dp_{i,j}$$$ tells us the cheapest solution to place those $$$i$$$ people on those $$$j$$$ chairs.

        Some examples:

        $$$dp_{1,1}$$$ choses only one person $$$x_1$$$ and one chair $$$y_1$$$. What is the best solution in this case? It's $$$abs(x_1-y_1)$$$ since we have only one possibility. Same with e.g. $$$dp_{5,5}$$$, we place the first 5 people on the first 5 chairs. There is only one possibility.

        $$$dp_{1,max(j)}$$$ choses only one person $$$x_1$$$ and all empty chairs. What is the best solution in this case? For each pair of $$$x_1$$$ and some chair $$$y_j$$$ we measure the distance. The smallest distance is the answer.

        $$$dp_{4,3}$$$ choses 4 people and tries to sit them on 3 chairs. This is impossible, so we assign the value $$$\infty$$$ here.

        Now the interesting part is the transition. how would we calculate $$$dp_{3,5}$$$? We want to find the cheapest solution to place 3 people on 5 chairs. We compare two steps. We could try $$$dp_{3,4}$$$ because it is also a solution for $$$dp_{3,5}$$$ (not neccessarily the optimal one) but it is a possible way to place 3 people on 4 places, so its also a valid distribution to place those 3 people on 5 places. Or we could try $$$dp_{2,4}$$$ and add person $$$x_3$$$ on place $$$y_5$$$. This has the cost $$$dp_{2,4} + abs(x_3-y_5)$$$. This way we obtain: $$$dp_{3,5}=min(dp_{3,4} \,; dp_{2,4} + abs(x_3-y_5))$$$. This relation is enough to obtain the solution, which will be $$$dp_{max(i),max(j)}$$$

        I really hope this helps and doesn't just confuse even more.

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

          Great help, thx.

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

          That is really a good explanation, I have written AC code following this exact explanation if anyone is interested.

          D

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

          Yes it helped. Thanks

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

          greatly explained !!

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

          wouldnt there be any case where suppose we shift index i+1 to i+2 and then move i to i+1. here i think we are only considering the space that are initially blank?? @OleschY

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

            That's why you have to update the $$$dp_{i,j}$$$ in a specific order (as is customary for DP-Solutions). More specifically, $$$dp_{i,j}$$$ depends on $$$dp_{i,j-1}$$$ and $$$dp_{i-1,j-1}$$$. So the two latter values have to be updated before $$$dp_{i,j}$$$. You can achieve this e.g. by iterating $$$i$$$ from $$$1$$$ to $$$max(i)$$$ in the outer loop and iterating $$$j$$$ from $$$i$$$ to $$$max(j)$$$ in the inner loop. Then it all works out!

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

          wow, this was a brilliant explanation. Thanks a ton :)

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

          Very crystal clear explanation brdr...this is how I exactly defined the states but was having a problem in writing the transition...thanks a lot !!

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

Ah, I think D is easier than C.

Maybe ABDCEF is a better choice.

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

In the B part problem if the question was changed a little bit that instead of rearranging the elements of subarray we have to choose some subarray and reverse it . Then what will be the approach for that problem.

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

    Check code jam qualification round 2021, you will find the exact problem with analysis.

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

It took over 24 hours for the Editorial to appear :)

The problems were cool, tho... I really enjoyed that round.

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

Thanks for the clean explanation and solution. But I still don't know why did I get a WA test 8 at D tho...

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

NGL soln of C is pretty neat. I hated this problem before reading the editorial.

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

For Problem E, why do we subtract 1 in the second step, 2 in the third step and so on?

At first turn, we can choose one of cnt[0] cities, at second turn we have cnt[0]+cnt[1]−1 choices, at third step — cnt[0]+cnt[1]+cnt[2]−2 choices,

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

The C is so cool.

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

does anyone know why some problems use 10^9 + 7 as mod while other problems use 998244353 as a mod?

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

In problem D if time take to move from $$$i$$$ to $$$j$$$ is some function $$$f(i,j)$$$ which is quadratic or of higher degree then this $$$dp$$$ solution will not work right?(because we can't say- "the leftmost starting position is matched with the leftmost ending" for optimal solution). How we can solve in this case?

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

I'd like to mention that it is well known in China how to solve problem D in O(n) time, and other generalisations of the problem are also studied and known. See This PDF (Problem 2 is problem D). The pdf is in Chinese but I don't have a better resource for it, maybe people interested can try google translate. Maybe you already know it, but I'm posting for those who don't.

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

    Is there anything that is non-standard for chinese guys ?

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

    I hope that they ask the same question in some other contest with bigger constraints: Expected solution O(n). That way, I will learn the method by reading blogs and editorials.

    Reading, translating, and understanding a Chinese document seems too difficult for me.

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

    Could you give me some other access to the pdf without vpn in China? I can hardly download it.

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

    Could you give me some other access to the pdf without using vpn in China? I can hardly download it.

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

    Oh, I borrow someone's VPN acount and download it, thank you for sharing this algorithm!

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

    Even though I'm Chinese I still can't understand it :(

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

Nice.

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

Can someone please explain in detail how to solve D using flows? Also, flows tag has been assigned to the problem in the Problemset.

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

      Can you elaborate on the complexity of this solution ?

      and how it can pass under those constraints !

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

        So each chair has one edge either from the source, or to the sink, that's $$$N$$$ edges. Additionally, each pair of adjacent chairs has two edges, one in each direction. So that's $$$N + 2(N - 1) = 3N - 2$$$ edges. Kactl's MCMF runs in $$$O(E^2) = O(2 * 10^8)$$$ but I guess the implementation ended up being too slow. AtCoder Library has one that runs in $$$O(F(V+E)\log(V + E))$$$, and was fast enough to pass. Here, $$$F$$$ is the max flow which we know is at most $$$\frac{N}{2}$$$.

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

          In this problem even MCMF which uses SPFA will be acceptable and it isn't hard to prove. Here can be n/2 edges from source at maximum (occupied chairs) and in worst case the flow will visit n-1 edges between chairs + n-1 edges from unoccupied chairs to sink. So the time complexity will be O( n/2 * (n-1+n-1) ) = O(n^2) = O(2*10^7).

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

    Heyy,

    I don't know much about this topic. Can Someone please share some resource or questions from where I can get to know more about it

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

Solution for C was nice :D . But can someone tell me why I am getting this runtime error on test 4 without any diagnostics for my solution? My Submission NVM. I got it. IGNORE :)

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

For Problem D is there any greedy solution?

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

In editorial of problem E, Can anyone explain the meaning of this sentence "Let's for each turn k∈[0,n) calculate the number of cities that you can build Monument in starting this turn as cnt[k]"? What exactly are we storing in cnt array?

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

    I have the same question

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

    I have added few comments in the code snippet which is necessary to understand the explanation further.

    For each point $$$j, 0 <= j < m$$$ (0 based indexing), $$$cnt[i]=$$$ number of cities in which we can build a monument on the $$$i^{th}$$$ day, such that the $$$j^{th}$$$ point won't be captured by any of the cities $$$\epsilon$$$ $$$cnt[i]$$$.

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

Can anyone help me out to find my mistake for solution of Problem D. What I am doing is that for every position already occupied, I at first find a position of the leftmost available place to that and the rightmost available place. If any of these two positions is already occupied, then I simply choose the one available, and in case if both are available, then I simply pick the position which takes the minimum time. This is my code : 116381349

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

    6

    0 0 1 1 1 0

    This case is failing for your answer and also for almost every greedy solution.

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

nocriz already mentioned $$$O(n)$$$ solution for D, but sadly not everyone can read chinese. I will explain another $$$O(n)$$$ solution from frodakcin

Let's look at the resulting solution and analyze it. I claim that resulting array can be divided into segments such that each segment is either

1) All cells in a segment are not people and are not new places for them -- useless cells. All segments of this type will have length one, and if there are $$$m$$$ people, there will be exactly $$$n-2m$$$ such segments.

2) Segment of cells such that it has even length at it has same amount of people as new seats for them inside and people match places in this segment (for example, segment has length 4, and there are 2 people who moved to 2 places)

To prove, let's find first not useless cell and go to the right, maintaining balance. Each person gives $$$+1$$$, and each taken place gives $$$-1$$$. If we arrive at balance 0, we got segment of type 2 and can start new one. If we didn't arrive at balance 0, there are two cases

1) we reached the end of array. That is just impossible, because number of taken places is equal to the number of people

2) we reached useless cell with non-zero current balance. Again, that is impossible because that would mean that we have non-zero balance on a segment, so someone (person or place) from this segment is matched to something to the right of empty cell and this is just not an optimal answer (we can match with empty cell instead)

Now you can notice that there are only $$$O(n)$$$ interesting segments. All segments of type 1 are just single cells, and all segments of type 2 are $$$[l, r]$$$ such that for each $$$l$$$, $$$r$$$ is minimal such that balance([l, r]) = 0. And we have to "construct" our array from these segments. In other words, we can replace segments $$$[l, r]$$$ with cost $$$c$$$ with edge from $$$l$$$ to $$$r+1$$$ with cost $$$c$$$ and find shortest path from $$$1$$$ to $$$n+1$$$. Obviously you can find shortest path in $$$O(n)$$$ if you know costs of all edges.

Cost of type-1 edge is $$$0$$$. To calculate cost of type-2 edge, let's write it: it is sum of $$$abs (person\_position - place\_position)$$$. Since this is minimal segment with balance $$$0$$$, you can see that all values $$$(person\_position - place\_position)$$$ will have the same sign, so you just need $$$abs(sum(person\_positions) - sum(empty\_places\_positions))$$$, and sum of such positions you can easily calculate with prefix sums.

You can look at my submission with some comments here

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

.

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

I found problem E very interesting. This was the first time I came across a probabilty related problem in competitive programming. [ still a noob:) ]

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

can anybody please give a more detailed explanation for D as I am not good with DP , after reading this tutorial for many times still I am stuck , Thanks in advance.

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

How on earth is E rated just 2100?

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

Problem 1525C - Robot Collisions is very interesting and it's tutorial is more interesting. Thanks a lot BledDest

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

I am wondering what would the solution be for problem D if the problem is extended to 2d array.

[moving cost being L1]

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

If problem D is modeled as a linear sum assignment problem (a bipartite graph with 2n vertices and connection between vertices i and j+n if a[i] = 1 and a[j] = 0), there is some general linear sum assignment algorithm that doesn't give TLE? I tryed successive shortest path (with O(E*logV) Dijkstra as intermediate step). I think it solves the general linear sum assignment problem in O(n²*logn) but it gives TLE on testcase 31 for this problem (code). So I wondered if it's some problem in my implementation or just the fact that the time limit for this problem is tight enough to avoid solutions using a general linear sum assignment algorithm.

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

In C, how did this formula come?

ans[i] = ans[j] = (2 * m - a[i].x - (a[j].d == 1 ? a[j].x : -a[j].x)) / 2;
»
3 года назад, # |
Rev. 8   Проголосовать: нравится 0 Проголосовать: не нравится

Problem F is so goooood ;w;

Here is my thought process on deriving the solution:

At first, I thought of the problem Skiers, where the minimum number of traversal is "the maximum anti-chains / minimum path-cover", which there is O(N^4) algorithm to solve it on DAG graph (and O(N) algorithm for directed acyclic planar graph). However, I realized after that this problem is quite different because each traversal "can't cross" with each other on any vertex. Then, it should be something else.

Then I came up with another approach that based on the observation that "each node can only pick at most one out going edge". Then, each time we pick one outgoing edge on a node, the number of "chains" decrease by one. Then, we just count the number of nodes having at least one going edge and subtract that from n and we will get the number of minimum goblins needed to overrun the town.

However, I realize not long after that the approach is wrong, as each node can pick at most one incoming edge. There might conflicts arise if two nodes pick two edges that go into the same node.

Hmmm, Wait. "Conflict" arising from "two nodes pick the edges that going into the same node" ? Isn't that sound familiar? OH yeah, it's maxflow problem.

And then I think splitting the graph into layers and make maxflow between two layers at a time, which I thought it was too complicate, and maybe it's better save this approach for later, because this approach makes me realize the key observation of the problem : "For each edges that goblin pass through, the number of chains decreases by one".

Suddenly, every pieces of puzzle fit together. We want to find the maximum number of edges we can pick, such that each node can only pick at most one outgoing edge, and each node can have at most one incoming edge. Isn't this also sound familiar? YES, it's a bipartite matching problem.

One last thing to concern: now that we can determine the minimum number of goblins need to make a graph loses. How do we know which node to turn off (incoming/outgoing) ? I then look closely to our bipartite matching graph, the "turn off" operation is the same thing as "cutting the edge from the souce to an interval node", or "cutting the edge from an internal node to the sink".

Which edge to cut inorder to guarantee decrease the maximum flow by 1 (is there even such edge at all?)? Hmm, I haven't solve this problem before. Let's try consider a min-cut. And bravo, I then discover that "cutting an edge of min-cut is guarantee to decrease the flow by one" (from the observation that "if we take out all the edges of MinCut, then the source won't be able to reach the sink." and some other observation like "cutting one edge can decreases at most one flow".) And it's guarantee that the min-cut will have edges only from "source to internal node" and "internal node to sink" only.

That just means every time I want to decrease the flow, I just pick one edge from the min-cut (can be any one of them), and just cut it. And this change is also cumulative (the remaining edge of min-cut will also be the min-cut of the new graph.)

The problem then reduce to [find min-cut] -> [do dp O(n^3) and memorize the transition] -> [Use the transition to the final maximum answer to determine when to cut an edge].

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

Can anyone help me figure out what mistake am I doing ??

https://codeforces.me/contest/1525/submission/209717072

Figured out.Forgot to sort the positions. :(