BledDest's blog

By BledDest, 4 years ago, In English

1525A - Potion-making

Idea: adedalic

Tutorial
Solution (adedalic)

1525B - Permutation Sort

Idea: BledDest

Tutorial
Solution (Neon)

1525C - Robot Collisions

Idea: BledDest

Tutorial
Solution (awoo)

1525D - Armchairs

Idea: BledDest

Tutorial
Solution (BledDest)

1525E - Assimilation IV

Idea: BledDest

Tutorial
Solution (adedalic)

1525F - Goblins And Gnomes

Idea: BledDest

Tutorial
Solution (BledDest)
  • Vote: I like it
  • +136
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Thanks for this nice and clear tutorial

»
4 years ago, # |
  Vote: I like it +33 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +48 Vote: I do not like it

    Thanks, that's why we didn't question it being C.

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Finally editorial is out.

»
4 years ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    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 years ago, # ^ |
      Rev. 2   Vote: I like it +2 Vote: I do not like it

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

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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 years ago, # ^ |
        Rev. 3   Vote: I like it +15 Vote: I do not like it

        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 years ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

          Great help, thx.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

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

          D

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

          Yes it helped. Thanks

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

          greatly explained !!

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            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 years ago, # ^ |
            Vote: I like it +5 Vote: I do not like it

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

        • »
          »
          »
          »
          »
          10 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          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 years ago, # |
  Vote: I like it +29 Vote: I do not like it

Ah, I think D is easier than C.

Maybe ABDCEF is a better choice.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +43 Vote: I do not like it

    I thike E is also easier than C.If you're familiar with expectations and probabilities,you can solve it easily.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

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

»
4 years ago, # |
  Vote: I like it +2 Vote: I do not like it

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

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

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    haha, everyone who wrote greedy soln. faced that

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    6

    0 0 1 1 1 0

    this case is failing for greedy solution of my ans. I think you must have done same mistake

»
4 years ago, # |
  Vote: I like it +28 Vote: I do not like it

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

»
4 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

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 years ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    In 2nd step,the nth city is fixed.And in 3rd step the nth point and the n-1th city is fixed.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The C is so cool.

»
4 years ago, # |
  Vote: I like it +9 Vote: I do not like it

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

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it +68 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +37 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # ^ |
    Rev. 2   Vote: I like it -34 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it -34 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it -34 Vote: I do not like it

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

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

»
4 years ago, # |
Rev. 5   Vote: I like it 0 Vote: I do not like it

Nice.

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +7 Vote: I do not like it
    Spoiler
    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      Can you elaborate on the complexity of this solution ?

      and how it can pass under those constraints !

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        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 years ago, # ^ |
          Rev. 2   Vote: I like it 0 Vote: I do not like it

          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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it +3 Vote: I do not like it

For Problem D is there any greedy solution?

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have the same question

  • »
    »
    4 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it
    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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    6

    0 0 1 1 1 0

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

»
4 years ago, # |
Rev. 2   Vote: I like it +98 Vote: I do not like it

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 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

How on earth is E rated just 2100?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

[moving cost being L1]

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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 years ago, # |
Rev. 8   Vote: I like it 0 Vote: I do not like it

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 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

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

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

Figured out.Forgot to sort the positions. :(