### awoo's blog

By awoo, history, 23 months ago, translation,

1187A - Stickers and Toys

Idea: MikeMirzayanov

Tutorial

1187B - Letters Shop

Idea: MikeMirzayanov

Tutorial
Solution (PikMike)

1187C - Vasya And Array

Idea: Roms

Tutorial
Solution (Roms)

1187D - Subarray Sorting

Idea: Roms

Tutorial
Solution (Roms)

1187E - Tree Painting

Idea: BledDest

Tutorial
Solution (Vovuh)
Alternative solution (PikMike)

1187F - Expected Square Beauty

Idea: Roms and adedalic

Tutorial

1187G - Gang Up

Idea: BledDest

Tutorial
Solution (BledDest)

• +105

 » 23 months ago, # |   +22 For G, could you explain more on how to "compress all edges that connect the same nodes of the network into one edge with varying cost"?
•  » » 23 months ago, # ^ | ← Rev. 2 →   +20 It refers to this section:To model the increase of discontent from the companies of people, let's convert each edge $(x, y)$ of the original graph into a large set of edges: for each layer $i$, let's add $k$ edges with capacity $1$ from the crossroad $x$ in the layer $i$ to the crossroad $y$ in the layer $i + 1$. The first edge should have the cost equal to $d$, the second edge — equal to $3d$, the third — $5d$ and so on, so if we choose $z$ minimum cost edges between this pair of nodes, their total cost will be equal to $d z^2$. Don't forget that each edge in the original graph is undirected, so we should do the same for the node representing $y$ in layer $i$ and the node representing $x$ in layer $i + 1$.Here, we add edges with capacities equal to $1$ and cost equal to $d$, $3d$ and so on between some pairs of nodes. While looking for augmenting paths, we may analyze only one of these edges instead of all of them. For example, let's add one edge with capacity $k$ between these nodes, and maintain the flow on it (let's denote current flow as $f$). If we want to traverse this edge from layer $i$ to layer $i + 1$, then we may denote the current cost of this edge as $(2f+1)d$, because we picked every edge with cost $d$, $3d$, ..., $(2f - 1)d$, and the next one will have cost $(2f+1)d$. And if we traverse it in the opposite direction, then the current cost is $(-(2f-1)d)$ for the same reason. It essentially reduces the number of edges in a network (roughly divides it by $k$).
•  » » » 23 months ago, # ^ |   0 Is it possible to combine this optimization with Dijkstra with potentials?
 » 23 months ago, # |   +20 Is there a small typo in Tutorial for C? It says "$b_i = 1$ otherwise" but this seems like it should be $b_i = -1$.
•  » » 23 months ago, # ^ |   +3 i think so
•  » » 23 months ago, # ^ |   0 Maybe my reply is bit unrelated. I saw your code for problem C. Can you tell me why have you excluded "r" from getting initialized when using the statement "Arrays.fill(sorted, l, r, true)" .
•  » » » 23 months ago, # ^ |   +1 I assume you're referring to 56395721.The reason for this is that sorted is an array of length n-1, where sorted[i] means that (i, i+1) is a sorted pair. Thus, saying the indices 3 through 5 are sorted is like saying the 3rd and 4th pairs are sorted.
•  » » » » 23 months ago, # ^ |   0 Oh, Thanks.
 » 23 months ago, # |   +1 For E, you mention a simpler idea without rerooting; can anybody explain this idea?My intuition was to pick a root node (one that maximizes the height of the tree) and then return the sum of the sizes of the subtrees. It failed because I didn't know how to break ties between candidate roots — or maybe the whole approach was wrong from the beginning :/I'd appreciate any ideas and intuition that led you to solve this problem; It would help me and other people improve!
•  » » 23 months ago, # ^ | ← Rev. 6 →   +25 The approach can be simplified a little because we don't actually need to reroot the tree. The score of a child node would be the score of the parent minus the size of the child subtree plus the number of nodes not in the child subtree, which would be parentScore - childSize + n - childSize = parentScore + n - 2 * childSize.It would look like this: void dfs(int node, int p, long long score) { best = max(best, score); for (int neighbor : adj[node]) { if (neighbor != p) { dfs(neighbor, node, score + n - 2 * sz[neighbor]); } } } Full solution: 56483647
•  » » » 23 months ago, # ^ |   0 Thank you sir!
•  » » » 11 months ago, # ^ | ← Rev. 2 →   0 Nice answer brother!!
 » 23 months ago, # |   +10 My approach for C was a little different.First, I initialize whole of my answer array to 1.I used a 2-D array boolean overlap[i][j]. What I did was for each range that should be sorted, I traversed through the range and made overlap[i][i+1] = true for each L <= i < R.Now for each range that should be unsorted, I find two consecutive indices i and i+1 inside the range such that overlap[i][i+1] is false. If we succeed to find such 'i', I simply do ans[i] = ans[i+1]+1. This makes the range unsorted. If we could not find such i for any of the ranges, we conclude that it is impossible, and print NO. Otherwise, we simply print YES and whole of the answer array in the next line.Link to my AC submission: 56331868
•  » » 11 days ago, # ^ |   0 Oggy orz!
 » 23 months ago, # |   0 Can we solve E using Diameter of tree__ and applying DFS to only on one leaf node of because its other siblings will give same answer.(not so sure about this approach).56363139 This is my submission i used DP but in another way i calculated for each edge its ans and took max from that but i don't know why i am getting TLE on test case 71. My hash function for creating unique value for edge(parent,child) = ((parent<<20)+child) as its unique for each edge. Also i have avoided all the symmetrical siblings by applying another DFS. But still getting TLE. Could anyone suggest why?
•  » » 23 months ago, # ^ |   0 I tried that(using the diameter) but it's not posible because there are many paths that can represent the diameter of the tree and try all of them it's very expensive.
•  » » 23 months ago, # ^ |   0 I changed map --> unordered_map and got AC with your previous submission!
•  » » » 23 months ago, # ^ |   0 Actually at first i used unordered_map,int> for the edge,that was giving me compilation error then i changed it to map then it compiled,also after that i used (x<<20+y) as a hash function for edge(x,y) also it means x->y != y->x,but i forgot to change it to unordered map.but thanks,it got passed.
 » 23 months ago, # | ← Rev. 3 →   +11 Here is an intuition behind C:Start with a number $\geq n$ and start assigning that number to the array from left to right.Decrease the number when you can and keep it the same when you must.Now, it's pretty easy from here. Just initialize the whole array with all $-1$ and for facts to type-1 fill the range $[l + 1, r]$ with $0$. After that cumulate the array. Now for all the ranges of type-2 check if those ranges are not sorted. If any range of type-2 is sorted, then answer is NO, else you have your answer.Here is my submission which I solved after the contest: 56371547A follow-up question could be to minimize the maximum number in the solution array.
•  » » 23 months ago, # ^ |   0 Yup the follow-up could be quite easily solved using the method mentioned in the tutorial, by just setting the rightmost value to 1 and then figuring out the leftmost value using array b.
•  » » » 23 months ago, # ^ | ← Rev. 2 →   0 I think it won't work.See the problem states "not sorted in non-decreasing order",doesn't necessarily mean "sorted in decreasing order".For this test case ~~~~~ 7 2 1 1 3 0 4 7 ~~~~~ the ans is 2.In your way it would be 4.
•  » » » » 23 months ago, # ^ |   0 Hey, he had asked to minimize the maximum number.Lol, Do check your argument again.
•  » » » » » 23 months ago, # ^ | ← Rev. 2 →   0 Sorry,I couldn't quite catch u.Had he asked to minimize the num of elements in solution array? If not,u do check my arguments again.
•  » » 23 months ago, # ^ |   0 Why starts form L+1, not from L in case of type 1?
•  » » » 23 months ago, # ^ |   0 Give it a dry run on sample case-1 with $[l, r]$.
•  » » » 23 months ago, # ^ |   0 Its to handle this type of case 10 3 1 1 5 1 6 10 0 2 7 
•  » » » 23 months ago, # ^ |   0 Let's say you start with $[l, r]$. Then, if you have some type-1 interval, say $[2, 4]$, your answer is gonna be: index: 1 2 3 4 5 value: -1 0 0 0 -1 And when you start assigning bigger number from left (say, $11$ for brevity), you will have: 10 10 10 10 9 But you see, interval $[1,2]$ is sorted (in non-decreasing order). Thus, it will fail. Otherwise, with $[l + 1, r]$ it would be: 10 9 9 9 8 which will pass.
 » 23 months ago, # | ← Rev. 5 →   0 what is s[v] in E? I'm not exactly sure.
•  » » 23 months ago, # ^ |   0 It's the amount of nodes of the subtree that belongs to the node V(including itself)
•  » » » 23 months ago, # ^ |   0 thx
 » 23 months ago, # | ← Rev. 2 →   -8 I have a question regarding my solution for problem D. My approach was along these lines: bool solve(l, r){ idx_a = max_el_idx(A[l]..A[r-1]) // index of max element in array A between l and r idx_b = max_el_idx(B[l], B[r-1]) // index of max element in array B between l and r if(idx_a > idx_b) return false; push(A, idx_a, idx_b); // pushes the element at index idx_a in array A, to index idx_b return solve(0, idx_b) && solve(idx_b+1, r); } Then we can just call solve(0, n) and print "yes" if true, and "no" otherwise. The algorithm works, but I got a time limit. Link to submission: 56341552 My questions are the following: Is the time complexity of this approach not O(P * log(P)) where P = Sum over all n-s? Is there an algorithm that I can use to be able to efficiently (hopefully O(1)) answer queries of the type max(A, l, r) -> returns the maximum element in the array A, between indices l and r? Looking forward to your help!p.s. Feel free to criticise any part of my code! I'm here to learn :D
•  » » 23 months ago, # ^ |   +1 Seems like you're looking for a range query data structure. Range maximum queries without modifications can be done in O(1) using sparse tables, while queries with updates can be done in O(log n) using binary indexed trees/Fenwick trees or segment trees.
•  » » » 23 months ago, # ^ |   0 Thanks a lot!
•  » » 23 months ago, # ^ |   +1 Your solution is $O(N^2)$e.g. an ascending array, you check every elements in your find max index code.
•  » » » 23 months ago, # ^ |   0 Aaah you are right. Thanks a lot :)
 » 23 months ago, # |   +2 Why so many hacks on D?
 » 23 months ago, # |   0 Why a solution for problem "D" with complexity (n log(n)) got TLE ? https://codeforces.com/contest/1187/submission/56335176
•  » » 23 months ago, # ^ |   +1 Because the STL of C++ takes more time than you thought.Why not try Fenwick Tree?
•  » » » 23 months ago, # ^ |   0 I thought that the complexity is (n log n) so I didn't think about anything I will try this, Thanks for your help :)
 » 23 months ago, # | ← Rev. 2 →   0 My solution for B is way different than what's written in there! https://codeforces.com/contest/1187/submission/56331183
 » 23 months ago, # |   0 Hey there. First of all, nice competition :) and thank you for the editorial. Small question, in F, where does the log(n) factor come from?
•  » » 23 months ago, # ^ |   0 the result need to mod $M$, so it takes $log(N)$ to get $x^{-1}$
•  » » » 23 months ago, # ^ |   0 Yeah, my bad. Of course, it should be $\log(MOD)$.
•  » » » 23 months ago, # ^ |   0 Since we are calculating inverses of values that are at most $n$, we can use the extended euclidean algorithm to find modular inverse in $O(log(n))$ time.
 » 23 months ago, # |   +9 In problem E, the solution can also be represented as $\displaystyle\max_{v} \sum_{u} dist(u, v)$ by thinking of it with the "contribution method": once we fix a root $r$, then $r$ gets counted once, its neighbours are counted twice, and so on.This way of looking at it might be helpful to rule out solutions based on "maximizing height", "choosing a proper diameter", and so on.
•  » » 23 months ago, # ^ |   0 Really interesting, but how does it rule out the other solutions? Is it because there's no greedy way to pick the right node v?
 » 23 months ago, # |   +7 Please link this editorial to Contest materials. Thank You
 » 23 months ago, # |   0 can someone explain why in awoo's solution for E the dfs(i) is called from non leaf (g[i].size()>1) I am not able to get it. Thank you
 » 23 months ago, # |   0 I solve C in this approach:make a connected component for all sorted range using Disjoint Set then i test the unsorted cases on the ranges if all range inside one component you can't build it.this My code : 56333646
•  » » 23 months ago, # ^ |   +1 Wow, I have to say that was a beautiful approach and easy to understand. Thanks, for sharing.
 » 23 months ago, # |   0 E explanation in detail . https://codeforces.com/blog/entry/68139
 » 23 months ago, # |   0 For C, can somebody explain array'nxt' stands for?
 » 23 months ago, # |   0 In F problem why were we using a>=mod then a-=mod? Shouldnt we do a=a%mod and if(a<=0)then a+=mod.? Thankyou
 » 23 months ago, # |   0 For problem C I first merged the overlapping intervals of the first type(increasing ones). Now all the increasing intervals are non overlapping. Then I checked if there is any interval of second type that comes entirely inside an interal of first type. In such case answer is not possible. Then I filled the array with [n, n — 1....1]. Next I iterated all the increasing intervals and made them increasing. For example if a increasing segment is [4, 7] then it would look like this for n = 10Before [10, 9, 8, 7, 6, 5, 4, 3, 2, 1].After [10, 9, 8, 7, 8, 9, 10, 3, 2, 1]. In this way as soon as we get out of the increasing intervals the array is non increasing. But I am getting WA. 56470710. Can someone point out my mistake or provide a test case where it fails
•  » » 23 months ago, # ^ |   0 The interval of the second type need not be completely inside an interval of first type. Just check for the case 10 2 1 2 7 0 4 9 The answer should be NO. Your code returns YES 10 9 10 11 12 13 14 3 2 1
•  » » » 23 months ago, # ^ |   0 I have solved it now. Thanks for replying
•  » » » 23 months ago, # ^ |   0 I don't undertand why this output "10 9 10 11 12 13 14 3 2 1" is wrong ???[a2...a7]=[9,10,11,12,13,14] is sorted . [a4...a9]=[11,12,13,14,3,2] is not sorted. I think this output is true , why your answer is NO . Can you explain to me? Sorry for my poor english.
 » 23 months ago, # |   +2 Can you explain PikMike's solution of E task please? Especially what does dp[] mean and why we add to dp[v] (tot — sum[u]).
•  » » 23 months ago, # ^ |   +3 Divide the problem into two parts. Part 1Consider that you are given a starting vertex ( fixed beforehand ). And now, consider the given tree with root as the given vertex. Then the answer is just sum of subtrees recursively. This is because, when you choose that vertex, you will add $|V|$ to the answer, which is the size of subtree rooted at that given vertex. And, when you select any child of the root, say $to$, then you add size of subtree rooted at $to$. Part 2Now, lets say for first part, we arbitrarily root the tree at vertex 0 ( it is arbitrary, any vertex will work ). Then, for each vertex first we calculate size of subtrees by dfs. Then, we calculate $dp[v]$ for each vertex $v$, which denotes the sum of sizes of all subtrees of the subtree rooted at $v$. Now, the main task of rerooting enters. Notice that, if $v$ is the current vertex and $to$ the next vertex ( in dfs ), and we know that answer with $v$ as first vertex is $dp[v]$, then answer with first vertex ( or root ) as $to$ will be $dp[to]$ = $dp[v]+size[v]-2*size[to]$. ( To see it clearly, try to simulate an example ).
•  » » » 23 months ago, # ^ |   0 Thanks a lot :-)
 » 23 months ago, # |   0 Can anyone explain me the problem B? I am a beginner in this field and I am not able to understand this problem (not even after seeing the solution). Any help would be commendable.
•  » » 23 months ago, # ^ |   0 Just have a look at the code. You will surely get it.Your text to link here...
•  » » » 23 months ago, # ^ |   0 Thank you so much!!! Just wanted to know from you, how can I improve my competitive coding skills?
 » 23 months ago, # | ← Rev. 2 →   0 Problem E (little modification of Vovuh's code) void dfs(int v, int p = -1) { ans = max(ans, dp[v]); for (auto to : g[v]) { if (to == p) continue; dp[to] = siz[0] + dp[v]-dp[to]-siz[to] + dp[to] - siz[to]; dfs(to, v); } } 
 » 23 months ago, # | ← Rev. 2 →   0 Test Cases of 1187C - Vasya And Array are weak. My first accepted code gives the wrong answer for this test case: 7 4 1 1 2 0 2 5 0 5 6 1 6 7 
 » 23 months ago, # |   0 For question E, I did not get the intuition they suggested in the editorial. If anyone could explain in a bit more clearer way it would really be appreciated. My intuition for the problem was to find the longest path on the tree and calculate the result of that path first and then its side branches.Any suggestions or comment on my method would be really appreciated.
 » 22 months ago, # |   0 Hello, can someone explain me why tutorial for 1187E has recursion approach calculating values for each tree node and for DFS too: int calcsize(int v, int p = -1) { siz[v] = 1; for (auto to : g[v]) { if (to == p) continue; siz[v] += calcsize(to, v); } return siz[v]; } Won't it lead to stack overflow situation for tests with large N (e.g where perimeter of tree equals to N)? I see that such kind of solutions take AC. Does it mean that tests are not strong enough?
•  » » 22 months ago, # ^ |   0 Well, stack size is 256MB by default in codeforces, didn't know about that.
 » 19 months ago, # |   0 Can anyone suggest more problems like E-Tree Painting ?
 » 10 months ago, # |   0 Can anyone explain the editorial for A?
 » 9 months ago, # |   0 awoo Thanks for the great explanation.What is the time complexity in problem B? I think it should be O(n + m*t) which of order 2? And this should give TLE.Please clarify. Thanks