### awoo's blog

By awoo, history, 3 years ago, 1065A - Vasya and Chocolate
Tutorial
Solution (Ajosteen)
1065B - Vasya and Isolated Vertices
Tutorial
Solution (Ajosteen)
1065C - Make It Equal
Tutorial
Solution (Ajosteen)
1065D - Three Pieces
Tutorial
Solution (PikMike)
1065E - Side Transmutations
Tutorial
Solution (PikMike)
1065F - Up and Down the Tree
Tutorial
1065G - Fibonacci Suffix
Tutorial
Solution (BledDest)  Comments (32)
 » 44220948 Why does my D WA on test 25? I had a similar solution, except I bruteforced the shortestdistance instead of using bds/djikstras, and used a bunch of selections for the dp instead of a pair.
•  » » 3 years ago, # ^ | ← Rev. 2 →   You probably didnt consider that in the optimal solution you need to change the chess piece on the way between position a and b and not only change pieces on position a or b?
•  » » » but a rook can always reach your desired position in 2 moves, so even if you switch to a rook at the beginning your total cost will be 3 Now if you're using a bishop and move to a position which isn't your destination, and switch to another counter and then move to your desired position, your total cost would still be 3 The same goes for switching between other counters. Your maximum cost will always be 3 (which is the same as switching to a rook at the beginning and then reaching your desired position)
•  » » » » 4 14 10 3 7 4 15 11 13 12 1 16 9 2 5 6 8  Consider, when you are in number 2 with a bishop. You can go to number 3 with a rook in 3 steps: replace bishop with rook, goto 14, goto number 3. You can go to number 3 with a knight in 3 steps: go back to 1 with bishop, replace bishop with knight, goto number 3. (Note that, you cannot replace bishop with knight in number 2 because, in that case, you need 4 steps to go to number 3)
•  » » » » Well, what if you number the positions as the positions of a knights tour? Then the optimal solution would be to start with a knight and the distance between each location would be 1.
 » 3 years ago, # | ← Rev. 2 →   O((N + M) * M) or O(M * (M + log(N))) solution for G: Same solution but changing 1 part, the part of counting the number of occurences. Instead of using an automaton, keep pref[i] ans suf[i] as the M first characters of the prefix/suffix of the i-th string. Using this, freq[i] = freq[i-2] + freq[i-1] + occurences that use the suf[i-2] + pref[i-1]. This gives us an O(N * M^2) solution like in the editorial. But you can note that starting from one point (the position where |F[i]| >= M), pref[i] == pref[i + 2] and suf[i] == suf[i + 1]. This means that the "occurences that use the suf[i-2] + pref[i-1]" will actually repeat with period 2. So you can calculate it in one phase until that point, the cost of this is O(F + F + F + ... + F[i]) = O(F[i + 1]) = O(M) and from that point on, you can reuse the number of occurences that happened in the previous calculations (or do 4 more just to be safe and use from that point on, doesn't change the complexity). Total complexity: O((N * M) * M) with one phase (additional character) happening in O(M + N) because you need to calculate the borders for KMP. You can actually use this fact to create a linear recurrence relation for freq[i] and use matrix exponentiation (the column matrix keeps freq[i], freq[i-1], the transitions and keep swapping them) starting from the point that it repeats, this would result in a O(log(M) + M + log(N) * 4^3) phase per character in the answer. The log(M) comes from the fact that fib is exponential, so the point that it starts repeating is O(log(M)). This would result in a O(M * (M + log(N))) solution. Code for O((N + M) * M) solution: https://codeforces.cc/contest/1065/submission/44225229 Edit: If there's an occurence in the transition, you can also keep calculating until it breaks K. It will break K in O(logK) steps and if there's no occurence, you just break. So I guess the second part wouldn't be necessary for the O(M^2) complexity.
•  » » 3 years ago, # ^ | ← Rev. 2 →   That also implies that a big N doesn't matter as can be seen here: https://codeforces.cc/contest/1065/submission/44226008. n = min(n, 100) works. To be more exact, the bound on N mattering would be O(log(M) + log(K)).
 » https://codeforces.cc/contest/1065/submission/44147964 For C,my approach is a bit similar can anyone help me,where i am wrong?
 » Can someone explain d.
 » Can anyone please suggest what is wrong with my approach? Submission I am first sorting the input array in descending order followed by storing a vector of blocks sliced to reach the subsequent lower level. For instance, if the input array is 4 3 2 2 1  my vector is 1 2 4 5  Finally, I am traversing to find out if a single or combination of blocks is less than or equal to k or not and incrementing the counter as a result.
 » Hi! I have a problem can anybody help? if u submit this solution for problem D with MS C++ u get accept! Link to code! but gnu++ gives WA! do u know why?
 » Can E solve by polya?
•  » » Yes, you can. 44419650 As the editorial said every subset of segments [0..l1), [l1, l2), …, [lm−1, lm) is achievable. There is a bijective relation between the set of subsets of segments and the set of transformations G and every transformation add An - k where k is the sum of the cardinalities of the segments in the associated subset.
•  » » » Yes! Actually, the formula solve by polya is the same as the official tuitual. Wonderful!
•  » » » » what is polya?
 » This for problem D, can anyone tell me why my solution times out (https://codeforces.cc/contest/1065/submission/44417842)
 » In the tutorial of Problem E: cnt function : why is there *(mod+1)/2 instead of /2 directly in the return. I am getting wrong answer with /2 but I am not able to understand why.
 » 3 years ago, # | ← Rev. 2 →   Why this this solution for problem 1065C is not correct ? Since the attainable equal height is the Smallest number. then Number of Chops can be : ceil((SumOfHeight - N * minHeight) / K)
•  » » Because we cant remove blocks parially from any particular height
•  » » aryan29's explanation is correct. Here, I'm providing a visual representation of a counter case so that people can get an intuitive understanding of the situation. Consider the test case: 3 3 5 3 1  Here ceil((SumOfHeight - N * minHeight) / K) will give 2 slices which is incorrect as we can not remove blocks from a height partially. Whereas the correct number of slices is 3. » 3 years ago, # | ← Rev. 2 →   Problem D can be solved with a single BFS using a state of four dimensions: current x, current y, piece type, current square we are at. The result is the minimum among final states. https://codeforces.cc/contest/1065/submission/46351519
 » Can someone please explain how to solve "1065E — Side Transmutations"? PikMike forgot to explain what l is? Sorry for my amazingly bad english.
 » Can anyone explain 1065C?I didn't understand the editorial.thanks in advance.
•  » » here is my code that may help u. if u don't understand then sorry. https://codeforces.cc/contest/1065/submission/91731954
 » Problem E is so nice. A subtle change in perspective, by viewing the string as a whole, solves it instantly!
 » •  » »
•  » » »
•  » » » » As memes are trending in this comment section I thought lets start with basics
 » Another solution for F : Let's convert this graph to SCC's and bridges, where each SCC component has a weight denoting number of distinct leaf nodes in that SCC. Start a dfs from the root, and when we hit each bottom, start uniting (union — find algorithm) the nodes on the basis of minimum distance of a leaf to their parents. Maintaining the weight of component is simple, initialize the weights of leaf components initially as 1, and non leaves as 0, take care of uniting weights while uniting nodes. Now we have a set of SCC's and to add bridges in between them, consider each edge in original tree not joining 2 nodes of same components. Now that we have a graph of SCC's and bridges, we need to find a path from root(component having node 1) to a leaf component having maximum sum of weights (maximum number of distinct leaves essentially). This is a simple dfs traversal, visit each child of a node, get maximum answer for respective children, and answer for parent is the weight of root + maximum of answers of all children. Time Complexity is O(N) or O(NlogN) depending on the implementation. Note : You could do union of the complete graph in O(N) too.
 » can't understand the solution to problem B. Can anyone explain it in an easier way ?