### ch_egor's blog

By ch_egor, 20 months ago, translation,

(Developing and idea — V--gLaSsH0ldEr593--V)

(Developing and idea — MikeMirzayanov)

(Developing — _kun_, idea — jury)

(Developing — Sehnsucht, idea — Helen Andreeva)

(Developing and idea — VFeafanov)

(Developing and idea — Sender)

(Developing and idea — voidmax)

Tutorial of Codealittle Contest-1
Tutorial of Practice Round
Tutorial of Practice

• +60

 » 20 months ago, # |   0 Auto comment: topic has been translated by ch_egor (original revision, translated revision, compare)
 » 20 months ago, # |   +11 Thx for fast judging and editorial!
 » 20 months ago, # |   +25 Good problems, interesting solutions
 » 20 months ago, # |   0 Could D be solved using Topological Sorting?
•  » » 20 months ago, # ^ | ← Rev. 3 →   +11 Yes. I did it by Topological Sorting. My code is here 50382490Actually I think Topological Sorting is just implementing the same method given by the editorial.
•  » » » 20 months ago, # ^ | ← Rev. 2 →   0 Hi! Could you please explain your code a little bit. I am interested in knowing what addEdge() function is doing and how is the while(head <= tail) loop working ? What is fe[] array for ?
•  » » » » 20 months ago, # ^ | ← Rev. 2 →   0 I am sorry to reply so late.For the first question, do you know what Adjacency List is? Function addedge() just adds an edge to the adjacent list of the graph, and fe[i] just denotes the index of the head of the list which contains all edges which start from vertex i.For the second question, do you know what BFS is? In this step I am just implementing BFS algorithm to get the topological order of this graph.
 » 20 months ago, # |   +8 C is very nice!
•  » » 20 months ago, # ^ |   0 Please explain the proof of C.
•  » » » 20 months ago, # ^ |   0 There are already few comprehensive explanations, I don't think I can do it better.
•  » » » » 18 months ago, # ^ |   0 Given editorial for c problem is grt..!!..but what I wanna say is I got AC in different way(sadly not during the live contest)...initially I sort the array...pick the largest element & put it into the dequeue.. then in the same fashion I traversed back from (n-2) to (0) and at one move I pick the element & put at the leftmost position in dequeue( O(1) operation..) and in other operation I pick element & put at the rightmost element in dequeue... Through this I approach I got AC...but I m also surprised with editorialist's solution bcoz it is O(n) where my solution is O(nlogn)...but it works well in the given constraints
 » 20 months ago, # |   0 Can somebody explain how to do the Concatenation of lists in O(1) for problem F?
•  » » 20 months ago, # ^ |   0 Roll your own linked lists, and create a map from the DSU to the first and last pointer in the list. Then, to join two lists it's just a single pointer change.
•  » » 20 months ago, # ^ |   +17 You can use std::list and the splice function. For a reference, take a look at this submission: https://codeforces.com/contest/1131/submission/50373670
 » 20 months ago, # |   0 How to solve problem F using dfs? Many of the people have solved it using DFS.
•  » » 20 months ago, # ^ |   +1 You may want to see my comment below.
 » 20 months ago, # |   +1 Wait. On F, I don't understand why it's enough to simply unite the sets. We are trying to put the numbers on an ordered line, so when we unite a and b does it not matter if a is to the left of b or b is to the left of a? Can we prove that it doesn't matter? The editorial doesn't actually explain the problem, it explains how to do DSU.
•  » » 20 months ago, # ^ |   0 Every time when we have to merge sets, all of kittens from one set are in one cell. If you have some order for set a and set b, you just have to ensure that this two sets are adjecent, because no matter you place them ab or ba, after i-th operation they will become one component.
•  » » 20 months ago, # ^ | ← Rev. 2 →   0 Assume that putting a to the right of b is a valid solution Then we split the sequence into four partsc — b — a — dWhere c is the subsequence in front of b and d is subsequence behind aIt is fairly obvious that d’ — a — b — c’ is also a valid solution (d' and c' are the reversed sequence for c and d respectively）So the order doesn’t matter
•  » » » 4 weeks ago, # ^ | ← Rev. 2 →   0 lckcode can you please explain the time complexity part. I now that in DSU with union by size each query takes O(logn) time. And we will make (n-1) such queries. Also in each query we have to concatenate two list A, B in O(min(A, B)) time. How all of this combined give us O(nlogn) time?
•  » » » » 4 weeks ago, # ^ | ← Rev. 3 →   +1 We don't actually concatenate the two lists, in practice, you may use multiple linked lists. To merge two lists, you may simply point the end of one linked list to another. This is O(1)
 » 20 months ago, # |   0 Is there any way to solve F using DFS
•  » » 20 months ago, # ^ | ← Rev. 4 →   +5 I did that, but not sure if the way is what you want, since my DFS is used to travel a tree. My Submission is here, notice that array sz[] is not used and was forgotten to remove.Simple explanation: when we merge 2 sets, we create a new node stands for the whole set and set its two sons to that 2 sets. After all operations it will become a tree because answer always exists. Use DFS to travel this tree, and every time when we meet a leaf node just print it. It's correct because for every merge operation the 2 operands are sons of a new node and that guarantees the 2 part must be neighbors in the final output.Hope it helps.
•  » » » 20 months ago, # ^ |   0 i did that and i got a wrong answer can you check my solution? https://codeforces.com/contest/1131/submission/50388700
•  » » » » 20 months ago, # ^ | ← Rev. 4 →   0 try this: ~~~~~ 5 1 2 2 3 2 4 3 5 ~~~~~
•  » » » » » 20 months ago, # ^ |   0 Each node would have only 2 edges , except the leaf nodes. Am i correct? And for that i just stored everything in the adjacency list and applied DFS on one of the leaf node.I am getting wrong answer.
•  » » » » » » 20 months ago, # ^ |   0 I think starting from a leaf node will invalidate the correctness proof.
•  » » » » 13 months ago, # ^ |   0 o_EDid you find the faults in your approach I did the same and got WA in test case 8 like you. Can you tell me if you got it?61853937
•  » » » » » 13 months ago, # ^ |   0 actually i learned dsu and came back to solve it , it's really easy using it but i didn't understand the dfs approach ,sorry
 » 20 months ago, # |   +15 How to solve G? I think I need more details.
•  » » 20 months ago, # ^ |   +17 Let’s look on stack of increasing values of dp. Then we count the new value, we need to get the minimum element on the segment, that element should be in the stack, we can go from top and search for it. But this solution is O(m**2). Let’s notice that we can erase elements if we find reachable element earlier. So we can keep in stack only optimums. That gives us O(m).
•  » » » 20 months ago, # ^ |   +19 Thank you!
 » 20 months ago, # |   0 didn't understand the solution of problem C, can anyone explain it to me or suggest other one ?and what is the kind of topic he used to solve this problem ?
•  » » 20 months ago, # ^ |   0 The solution is a constructive one. Sort the sequence, and append all even-indexed elements reversely to the end of the sequence. Correctness proof is given in the editorial.
•  » » 20 months ago, # ^ |   +14 I'll do my best to describe the proof in more detail:We have a problem that requires us to find a configuration that minimizes a value. The general approach used (which can sometimes be applied to other problems too) is the following: We prove that that there's a lower bound on how small the answer can be for a given input and then we figure out a configuration that achieves that exact lower bound.For this task: First, let's consider the numbers ordered such that a[i] ≤ a[i + 1]. We can then prove that the answer can't be better than ai + 2 - ai,  for any i = 1,2,3,...,n-2. ( statement 1 )If we manage to do that, then we can say that a lower bound for the answer is the maximum of all of those, i.e. max(ai + 2 - ai), where . The final step would be to see that the solution provided in the editorial gives that exact lower bound ( statement 2 ).Proof for statement 1: First imagine that you have a complete graph, where the vertices are the given heights and the edges represent the absolute differences between the two endings. In order to give a solution, we must give for every vertex a "before" neighbour and an "after" neighbour, such that we get a cycle going through all nodes exactly once (a hamiltonian cycle). Now let's prove by contradiction that the solution can't be better than ai + 1 - ai for a fixed i. Suppose that it is smaller. Since we want the maximum value of an edge to be stricly less than ai + 1 - ai, then it means we can't use any edge between a[m] and a[n], where (let this be the first set) and (let this be the second set), since a[n] - a[m] would be greater or equal to a[i + 2] - a[i]. From this, we can see that if we want to start a path from the first set that goes to the second set and then comes back, then we must use a[i + 1] at least twice. Thus, any cycle will contain a[i+1] more than once, so it can't be hamiltonian, so we can't build a correct configuration => contradiction with our initial hypothesis that there exists a solution. Therefore any solution can't be better than the mentioned lower bound.Proof for statement 2: I'll just mention the logic for the example solution given in the editorial: a1, a3, a5|a6, a4, a2. We can see that the difference between two heights in the first part or in the second is one of the those that give the lower bound. The other two are the difference in the middle and the one at the end. But we have that a[6] - a[5] < a[6] - a[4] < lowerBound and a[2] - a[1] < a[3] - a[1] < lowerBound. So the maximum of all values is less or equal to the lower bound, but since the lower bound is the lowest value, it means we get exactly that.Hope it helped.
•  » » » 20 months ago, # ^ |   0 Dude, you are masterpiece ! thanks <3
•  » » » 20 months ago, # ^ |   +17 Honestly, the proof of statement 1 seems pretty nontrivial, which is why I'm surprised the problem was a Div2 C.
•  » » » » 20 months ago, # ^ |   0 I agree. The problem was probably a Div.2 C because of how easy it is to write the code for the solution given in the editorial. Maybe many people "eyeballed" the solution without proving it.
 » 20 months ago, # | ← Rev. 2 →   0 Can anybody please point out why my solution for F is exceeding time limit? Thanks in advance!50385034
•  » » 20 months ago, # ^ | ← Rev. 2 →   +4 You haven't initialised sze with 1.
•  » » » 20 months ago, # ^ |   0 Thanks a lot for replying and solving my mistake as well.However do you have an idea as to why all those 7 test cases worked (the 7th testcase had a very large input file as well)... and why particularly initialising the sze array with values as 0 was causing a tle?Thanks a lot though!
•  » » » » 20 months ago, # ^ |   0 Wait I got it, no need for replying , thanks a lot! It was a pretty silly mistake by me :(
 » 20 months ago, # |   0 G: Where is the case with dropping domino to the left? Also first formula shouldn't be dp[j]=min(dp[i-1]+c[i]) for all j, that j-th domino fall if i-th was pushed to the right?
•  » » 20 months ago, # ^ |   +5 I think the editorial has a typo. The first situation should be domino falls left.
•  » » » 20 months ago, # ^ |   0 I think so. It had confused me for some moment until I realized there is a typo.
 » 20 months ago, # |   0 I did Problem C using Binary Search, but my implementation was too complex 50403476. Anybody who also did it using Binary Search with an easier implementation?
 » 20 months ago, # |   +16 DSU contest
 » 20 months ago, # |   +8 How to solve problem G by dynamic? I need more explanation.
 » 20 months ago, # |   0 actually，I can't understand D's DSU solution.how to deal with '<'ans'>'?
•  » » 20 months ago, # ^ | ← Rev. 2 →   0 if aij equal to "=" then dish i from the first day and dish j from the second day must be equally good, then you will realize that 2 dishes must be on the same group. When you assign all dishes to the group then you can look at '<' and '>'. So if aij equal to '<' then the group of dish i from the first day are worse than the group of dish j from the second day so that makes a graph of the group of dishes, Same as '>'. After that use DFS on that graph to find which group are the worst and which one is better and so on.Here is my solution : linkPS. Sorry about my English.
 » 20 months ago, # | ← Rev. 6 →   0 The following is another alternative for implementing the proposed solution for problem 1131C - Birthday.50431644Assuming a 0-indexed array a0, a1, ..., an - 1, and an array-size parity , your valid optimal solution can be formulated as follows:ap, ap + 2, ap + 4, ..., an - 2, an - 1, an - 3, an - 5, ..., a1 - p.P.S. In the problem statement, I think that the word "low" should have been written as "short", and that the phrase "growth of two neighboring children" should have been written as "heights of two adjacent neighboring children".
 » 20 months ago, # |   0 Can someone explain E more thoroughly?
 » 20 months ago, # | ← Rev. 2 →   0 The following is another intuitive deque-based solution for problem 1131C - Birthday, pushing ordered heights at both ends of the deque in alternation until all heights are pushed to form the optimal circle with minimum height-difference between adjacent neighboring children. 50471341P.S. This idea was developed independently. Other contestants might have used a deque as well to form the requested circle.
 » 20 months ago, # |   0 Div2 Problem B: Draw! In my local machine, I'm getting the correct output for the test case, while on cf output is different, Can anybody help me with this, Here is my code .
•  » » 20 months ago, # ^ |   0 Try your original code on your local machine by entering the input values instead of copying the test case. It will output a garbage value. Solution: Replace mp(0, 0) by construct assignment pair(0, 0). and mp(x, y) with pair(x, y).
•  » » 20 months ago, # ^ |   0 In the second loop, i goes up to n, then in this line,  y = min(arr[i+1].ff,arr[i+1].ss);You try to access the element at arr[n+1] which is some garbage value that you did not set.
 » 20 months ago, # | ← Rev. 2 →   0 Next time you arrange a contest please write small stories.Too much stupid lines in a problem.This is totally annoying.
 » 20 months ago, # |   0 Can anybody help me with the number assignment process in D?
 » 20 months ago, # | ← Rev. 2 →   0 Can you modify the answer fo G ? I think the editorial has a typo. The first situation should be domino falls left.Thank you very much. I think it will be very helpful.
 » 20 months ago, # |   0 can anyone help me in question F. I used DFS. https://codeforces.com/contest/1131/submission/51000532
•  » » 19 months ago, # ^ |   0 Try the following test case it gives a wrong sequence 10 10 6 8 10 6 2 8 7 8 4 1 7 8 5 3 10 10 9
 » 19 months ago, # |   0 good tutorial
 » 18 months ago, # |   0 Given editorial for c problem is grt..!!..but what I wanna say is I got AC in different way(sadly not during the live contest)...initially I sort the array...pick the largest element & put it into the dequeue.. then in the same fashion I traversed back from (n-2) to (0) and at one move I pick the element & put at the leftmost position in dequeue( O(1) operation..) and in other operation I pick element & put at the rightmost element in dequeue... Through this I approach I got AC...but I m also surprised with editorialist's solution bcoz it is O(n) where my solution is O(nlogn)...but it works well in the given constraints
 » 14 months ago, # | ← Rev. 6 →   0 Just want to share my approach on F, don't Know how to use DSU or function mentioned but can be done using array of priority Queue, Might be a little complex and lengthy at first sight but simpler and easiera Component=> a priorityQueue(element of priorityQueue array)Priority queue is used to find element with least degree(0 or 1) in a componentalso create a list to store neighbors of every elementthere are 4 case when a new query comes.1) both are included in different(obvious from question) components(can be checked by creating a map) then take the smaller Component and put all its elements to larger component while changing their location in map but before that to merge two components we need to update two elements each of each component changing their degree and adding them in each other's list2) first element is included in some component then just add 2nd element in first element's group but before that update two elements each of each component changing their degree and adding them in each other's list3) second element is included in some component , similar to 2nd case4) no element is included in any component, put both element in new Component and mapping their location(index of their component) and add them to each other,s listAfter that we have now a graph(actually a linked List ) , traverse the list to find one element with degree 1 and using that element as first perform dfs to print the arrayhttps://codeforces.com/gym/252240/submission/60240365
 » 14 months ago, # |   0 In problem D , Otherwise assign numbers, where the vertex gets the least number greater than the vertex it goes to. how to do this??
•  » » 10 days ago, # ^ |   0 Topological sort.
 » 13 months ago, # | ← Rev. 4 →   0 In problem D , we can join the roots of the all components by one kind of edge which is < (less than). In other Words : if i is less than j we join i to j And if i is better than j we join j to i ( The opposite ). So now we have one kind of edge in the graph and we have to go through the edges and move from one component to other and we add one the component's counter (All the nodes in the same component will have the value of the component's counter). And we can only deal with the roots of the components using DSU . Code for better understanding : 62076560
 » 3 days ago, # |   0 Problem C is just of 1200 rating but solving it made me asuume its rationg tobe above 1600. Worth trying anyway!! Learnt much from it!