### BledDest's blog

By BledDest, 4 weeks ago,

1525A - Potion-making

Tutorial

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

1525F - Goblins And Gnomes

Idea: BledDest

Tutorial
Solution (BledDest)

• +136

 » 4 weeks ago, # |   +3 Thanks for this nice and clear tutorial
 » 4 weeks ago, # |   +33 The author's implementation of task C is much better than mine.
•  » » 4 weeks ago, # ^ |   +48 Thanks, that's why we didn't question it being C.
 » 4 weeks ago, # |   +5 Finally editorial is out.
•  » » 4 weeks ago, # ^ |   -26 Is this contest rated?
 » 4 weeks ago, # | ← 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 weeks ago, # ^ |   +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 weeks ago, # ^ | ← Rev. 2 →   +2 I didn't get the thing. What exactly dp[i][j] is representing? Can someone help? Thanks!
•  » » » » 4 weeks ago, # ^ |   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 weeks ago, # ^ |   -8 Not first j, in total j.
•  » » » » 4 weeks ago, # ^ | ← 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 weeks ago, # ^ |   +5 Great help, thx.
•  » » » » » 4 weeks ago, # ^ |   +5 That is really a good explanation, I have written AC code following this exact explanation if anyone is interested.D
•  » » » » » 4 weeks ago, # ^ |   +5 Yes it helped. Thanks
•  » » » » » 4 weeks ago, # ^ |   +5 greatly explained !!
•  » » » » » 4 weeks ago, # ^ |   -8 perfect explaination doesnt exi.......
•  » » » » » 4 weeks ago, # ^ |   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 weeks ago, # ^ |   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 weeks ago, # ^ |   +5 wow, this was a brilliant explanation. Thanks a ton :)
•  » » » » » 4 weeks ago, # ^ |   0 The starting-positions-mapped to-ending-positions part in the editorial was a bit confusing. occupied-chairs-mapped-to-unoccupied-ones would have been better.
 » 4 weeks ago, # |   +29 Ah, I think D is easier than C.Maybe ABDCEF is a better choice.
•  » » 4 weeks ago, # ^ |   +43 I thike E is also easier than C.If you're familiar with expectations and probabilities,you can solve it easily.
•  » » » 4 weeks ago, # ^ |   +11 Can you list some tutorials or articles?
•  » » » » 4 weeks ago, # ^ |   +3 This is a good one.
•  » » » 4 weeks ago, # ^ |   0 I think everything is easier than c in this world
 » 4 weeks ago, # |   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 weeks ago, # ^ |   +1 Check code jam qualification round 2021, you will find the exact problem with analysis.
 » 4 weeks ago, # |   +2 It took over 24 hours for the Editorial to appear :)The problems were cool, tho... I really enjoyed that round.
 » 4 weeks ago, # |   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 weeks ago, # ^ |   +3 haha, everyone who wrote greedy soln. faced that
•  » » » 4 weeks ago, # ^ |   0 Same
•  » » 4 weeks ago, # ^ |   +11 6 0 0 1 1 1 0this case is failing for greedy solution of my ans. I think you must have done same mistake
•  » » » 4 weeks ago, # ^ |   0 Thanks! I couldn't figure out much from the 8th test case tbh but yours helped
 » 4 weeks ago, # |   -20 Fast Indeed...!
 » 4 weeks ago, # |   +28 NGL soln of C is pretty neat. I hated this problem before reading the editorial.
 » 4 weeks ago, # | ← 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 weeks ago, # ^ | ← Rev. 2 →   +1 In 2nd step,the nth city is fixed.And in 3rd step the nth point and the n-1th city is fixed.
 » 4 weeks ago, # |   0 For problem A, you can figure out by the observation that the answer will always be the denominator of the fraction k/100, as that's the least sum possible. I don't know the actual working of the function, but I used the fractions library in python to Get the denominator. Solution: 116323887
 » 4 weeks ago, # |   0 in problem D , why dp is initialised by infinity ??
•  » » 4 weeks ago, # ^ |   0 because you want to minimize the final ans
 » 4 weeks ago, # |   0 The C is so cool.
 » 4 weeks ago, # |   +9 does anyone know why some problems use 10^9 + 7 as mod while other problems use 998244353 as a mod?
•  » » 4 weeks ago, # ^ | ← Rev. 2 →   0 Taking MOD to reduce a number makes sense only if done with prime number and 10^9 + 7 and 998244353 are both prime numbers of order 10^9 so typical int range. I hope it makes sense now. They are just arbitrary prime numbers.
•  » » 4 weeks ago, # ^ |   +5 It has to do with FFT, as I understand. See here.
•  » » » 4 weeks ago, # ^ |   0 thanks!
 » 4 weeks ago, # |   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 weeks ago, # |   +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 weeks ago, # ^ |   +37 Is there anything that is non-standard for chinese guys ?
•  » » 4 weeks ago, # ^ |   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 weeks ago, # ^ | ← Rev. 2 →   -34 Could you give me some other access to the pdf without vpn in China? I can hardly download it.
•  » » 4 weeks ago, # ^ |   -34 Could you give me some other access to the pdf without using vpn in China? I can hardly download it.
•  » » 4 weeks ago, # ^ |   -34 Oh, I borrow someone's VPN acount and download it, thank you for sharing this algorithm!
•  » » 4 weeks ago, # ^ |   0 at nocriz if you can elaborate a bit on this (if you know and wont take much of your time), would be helpful.
•  » » 4 weeks ago, # ^ |   0 Even though I'm Chinese I still can't understand it :(
 » 4 weeks ago, # | ← Rev. 5 →   0 Nice.
 » 4 weeks ago, # |   0 Hey, I'm trying to understand but not getting the intuition behind A, can someone help me ?
•  » » 4 weeks ago, # ^ |   0 Every percent can be written as a fraction.25% -> 25/10040% -> 40/100Remember u can always simplify the fraction. So 25/100 becomes 1/4, 40/100 becomes 2/5 etc. Amount of moves you need to do is in denominator (w + e).So all you need to do is simplify fraction to the simplest form and you use GCD for it. 25/100 -> GCD is equal to 25. Because all you need is the denominator u divide 100 by GCD and that's your answer.
•  » » » 4 weeks ago, # ^ |   0 Thank You So Much, I have now understood the idea behind the gcd.
 » 4 weeks ago, # |   +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 weeks ago, # ^ |   +7 SpoilerMake a graph with n+2 nodes, one for each armchair, a source and a sink. Add an edge between adjacent armchairs with cost 1 and infinite capacity. For occupied chairs, add an edge from the source to that chair with cost 0 and capacity 1, same for unoccupied chairs to the sink. Now you can run min-cost max-flow on this.
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 Can you elaborate on the complexity of this solution ?and how it can pass under those constraints !
•  » » » » 4 weeks ago, # ^ |   +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}$.
•  » » » » » 2 days ago, # ^ | ← 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 weeks ago, # ^ |   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 weeks ago, # | ← 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 weeks ago, # |   +3 For Problem D is there any greedy solution?
 » 4 weeks ago, # |   0 Can anyone tell me how to prepare for problems like C?
 » 4 weeks ago, # |   +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 weeks ago, # ^ |   0 I have the same question
•  » » 4 weeks ago, # ^ | ← Rev. 3 →   0 Editorial Code Snippetfore (j, 0, m) { // We are looking at all the "points" one by one. vector cnt(n + 1, 0); // Notice here that cnt vector is intialized to 0 // every time for all 0 <= j < m (0 based indexing) // For the point j to NOT be captured by city i, the monument should be built on // step >= n + 1 - x (These variables are introduced in the editorial.) // Here x = d[i][j] fore (i, 0, n) cnt[n + 1 - d[i][j]]++; vector d(n + 1, 0); d[0] = 1; int rem = 0; fore (i, 0, n) { rem += cnt[i]; d[i + 1] = norm(d[i + 1] + mul(d[i], rem)); rem = max(0, rem - 1); } // cerr << d[n] << " - " << norm(1 - mul(d[n], invFact)) << endl; E = norm(E + 1 - mul(d[n], invFact)); } 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 weeks ago, # |   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 weeks ago, # ^ |   0 60 0 1 1 1 0This case is failing for your answer and also for almost every greedy solution.
•  » » » 4 weeks ago, # ^ |   0 Thanks for the reply.I have found my mistake
 » 4 weeks ago, # | ← 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 frodakcinLet's look at the resulting solution and analyze it. I claim that resulting array can be divided into segments such that each segment is either1) 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 cases1) we reached the end of array. That is just impossible, because number of taken places is equal to the number of people2) 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 weeks ago, # | ← Rev. 3 →   0 .
 » 4 weeks ago, # |   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 weeks ago, # |   0 Problem C is pretty amazing actually. Kudos to the author!!
 » 4 weeks ago, # |   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 weeks ago, # ^ | ← Rev. 2 →   0 This is my LogicAnd this is my Submission:116702763
•  » » » 4 weeks ago, # ^ |   0 Thanks, it really helped.
 » 4 weeks ago, # |   0 why problem D follows dp and not greedy ? like how we identify the property that tells us that this problem exhibits dp property ?
 » 4 weeks ago, # | ← Rev. 2 →   0 How on earth is E rated just 2100?
 » 4 weeks ago, # |   0 is there anywhere I can find an explanation for C
 » 3 weeks ago, # |   0 Problem 1525C - Robot Collisions is very interesting and it's tutorial is more interesting. Thanks a lot BledDest
 » 3 weeks ago, # | ← Rev. 3 →   0 -
 » 12 days ago, # |   0 I am wondering what would the solution be for problem D if the problem is extended to 2d array.[moving cost being L1]
 » 9 days ago, # | ← Rev. 3 →   0 can anyone help me find the problem with my implementation of problem E?119103848
 » 8 days ago, # |   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.
 » 6 days ago, # | ← 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;