### SYury's blog

By SYury, history, 2 weeks ago, ,

1059A - Cashier

Tutorial
Code

1059B - Forgery

Tutorial
Code

1059C - Sequence Transformation

Tutorial
Code

1059D - Nature Reserve

Tutorial
Code

1059E - Split the Tree

Tutorial
Code (first solution)
Code (second solution)

•
• +74
•

 » 2 weeks ago, # |   +12 Fast editorial!!
 » 2 weeks ago, # |   +17 Love you!!! Thank you posting Editorial this quick.Still, no news of last round's editorial
 » 2 weeks ago, # | ← Rev. 2 →   0 Excellent problems! Lightening fast editorial! What more does one need!?
 » 2 weeks ago, # |   +9 Can someone please explain D? I cannot understand the editorial.
•  » » 2 weeks ago, # ^ | ← Rev. 2 →   +28 You can also ternary search the x-coordinate of the circle's center and judge the better side by calculating the minimum necessary radius. Let's assume the x-coordinate of the center be x0, then the center should be (x0,R). We enumerate all points (xi,yi) and find out the minimum radius separately for each point, which will be the R in equation like: (x0-xi)^2 + (R-yi)^2 = R^2. The maximum necessary R will be the minimum radius we need.
•  » » » 2 weeks ago, # ^ |   0 hOW TO DO ternary search<>.Thanks in advance
•  » » » » 2 weeks ago, # ^ | ← Rev. 2 →   0 This is a nice explanation of ternary search. Hope it helps.
•  » » » » » 2 weeks ago, # ^ |   0 I know ternary search but how to use here
•  » » » » » » 2 weeks ago, # ^ |   -21 how to use brain? you just do it
•  » » » » » » 2 weeks ago, # ^ |   0 my analysis ternary search using by X coordinate which from common point with river.
•  » » » » » » 2 weeks ago, # ^ |   0 Given: River line (i.e. y=0) must be a tanget to the circle.Hence your centre point will be some (X,Y) where Y is the radius. Now find minimum Y(Use binary Search) and slide circle left to right ,to and fro (ternary search), there will be some x such that it contains all the points that satisfy for y ( if valid). Fix your x with ternary search , then find min R , for which circle (x,R) contains all points, and decide ternary search domain on basis of min value of R.2nd approach is efficient one.
 » 2 weeks ago, # |   0 Thanks for Toturials and Code in C++(finally!)
 » 2 weeks ago, # |   0 For D, I did a ternary search to find the X coordinate of the center of the circle. Then calculating the radius is easy.
•  » » 2 weeks ago, # ^ |   0 Bro can you explain in little more detail
 » 2 weeks ago, # |   0 D can also be solved with a randomized algorithm much like the one used in minimum covering circle problem. The only thing to change is to make the new circle always tangent to x-axis.
 » 2 weeks ago, # |   +1 What is wrong in this solution? Is it the precision errors for doubles in Java?
•  » » 2 weeks ago, # ^ |   +8 Yup I guess so. Same thing happened to me in c++ as well. You can improve it by writing as
•  » » » 2 weeks ago, # ^ |   0 Thanks!! I also tried this r2 - (y - r)2 = y2 - 2yr, where y < 107, r < 5 * 1013. So we get rid of r2 which is big. Passed Test #4, but failed on #6.
•  » » » » 2 weeks ago, # ^ | ← Rev. 3 →   0 you need to do sqrt(y^2 — 2yr) = sqrt(y)*sqrt(y — 2r) because y*r is also too big.You can also just change your binary search bound from 0...1e15 (I'm guessing you have) to 0...1e16 and it'll magically pass with your first original equation.
 » 2 weeks ago, # | ← Rev. 9 →   0 In the Code for problem 1059C - Sequence Transformation, it is sufficient to generate the required answer from n and mul only without using the global array int seq[maxn]. In particular, when n ≥ 4, the number of times mul is appended to the answer array should be exactly equal to the ceiling of n / 2, regardless of the contents of seq. The following is an Updated Code.
 » 2 weeks ago, # |   0 thanks for the very quick editorial totally loving it!!
 » 2 weeks ago, # |   0 Can anyone give more detailed explanation of B please?
•  » » 2 weeks ago, # ^ | ← Rev. 2 →   +8 My solution is to check if there are exactly 8 inked cells around a cell(not empty).If so, just put a pen on a new sign.After that, compare the new sign with the given one and you can know it's YES or NO.
•  » » 2 weeks ago, # ^ |   0 my solution is as follow --> i have checked if cell(x,y) is '#' or not. if it is, than i have checked for all 8 adjacent direction to it that whether at least one out of 8 it is possible to make 3X3 matrix.if it is than we can fill this cell and ans is YES and it is not possible to make 3X3 matrix than NO.--> if cell is not '#' than continue.my solution is . http://codeforces.com/contest/1059/submission/43840425
•  » » » 2 weeks ago, # ^ |   0 Beautiful solution.
 » 2 weeks ago, # |   +4 please anyone explain,in problem c, why this algo. works?
•  » » 2 weeks ago, # ^ |   0 The gcd(a, a+1) is always 1. So in order to maximize lexicographically the sequence, you have to remove all the consecutive pairs. The best option to do that is to remove the odd numbers in which the gcd will be always 1. So if you have 1,2,3,4,5,...N and you remove the odd numbers you have now 2,4,6,8,... = 2(1,2,3,...floor(n/2)).
•  » » » 2 weeks ago, # ^ |   0 Well explained but we need to consider when n=3. Here is the implementation of same.http://codeforces.com/contest/1059/submission/43883859
 » 2 weeks ago, # | ← Rev. 2 →   +1 How can you prove that for the problem 1059C, the rest of the answer is the answer for floor(n/2) multiplied by 2?
•  » » 2 weeks ago, # ^ |   0 Nevermind, i just figured it out. Thanks
•  » » » 2 weeks ago, # ^ | ← Rev. 2 →   0 How? Can you please tell?And How it is an nlog n solution? It seems to be O(n) solution
•  » » » » 2 weeks ago, # ^ |   +6 The gcd(a, a+1) is always 1. So in order to maximize lexicographically the sequence, you have to remove all the consecutive pairs. The best option to do that is to remove the odd numbers. So if you have 1,2,3,4,5,...N and you remove the odd numbers you have now 2,4,6,8,10,... but that is equal to 2*(1,2,3,...floor(n/2)). From there is direct that the rest of the solution is the solution for floor(n/2) times 2
•  » » » » » 2 weeks ago, # ^ |   0 Thanks a lot
 » 2 weeks ago, # |   +19 the running time of the code for problem C is O(n) and not O(nlogn)
•  » » 2 weeks ago, # ^ | ← Rev. 2 →   0 Yes, I think time complexity is O(n). We build the output of size n.
•  » » 2 weeks ago, # ^ |   +5 Fixed, thank you.
 » 2 weeks ago, # |   +16 For problem E can someone please explain the solution a lttle bit easy way. I can't understand the intuition behind the solution that why it's optimal?
•  » » 2 weeks ago, # ^ |   +8 Consider if you are at a node in a dfs, and all the children are all already part of a chain. Obviously you can only continue one chain from one of your children, and possibly you have to start a new chain if the children's chains are too long. So for each of the children, just take the one that will allow you to add the most amount of nodes in the future. The reason is that for any of the children's chains, all their ancestors that could be added later are the same, so you might as well take the chain that can be extended the furthest possibly later. You can check how many ancestors you can add to a chain using binary lifting/sparse table.
•  » » » 2 weeks ago, # ^ |   0 "You can check how many ancestors you can add to a chain using binary lifting/sparse table." -> Or simple binary search.
•  » » » 2 weeks ago, # ^ | ← Rev. 3 →   0 Thanks for the reply!
 » 2 weeks ago, # |   0 Can anyone tell the use of the statement "for(int i = 0; i < n/2; i++)seq[i] = seq[2*i + 1]/2;" in the author's code?
•  » » 2 weeks ago, # ^ |   0 There was no need. Just comment it out, it will work perfectly fine. seq array was just to keep the positions so that every time odd positions (from the remaining can be removed) but since it is already initialized with i+1, every time it will get alternate even odd. Just dry run for say n=10 and you will understand why it was there and why it works fine without it.
•  » » » 2 weeks ago, # ^ |   0 Thanks a lot
 » 2 weeks ago, # | ← Rev. 6 →   +5 D solution: each point(xi,yi) requires minimum Ri where reserve is (X,0) Ri = sqrt((yi-Ri)^2+(xi-X)^2) is interpreted Ri^2 = (yi-Ri)^2+(xi-X)^2 after transposition yi*Ri*2 = yi^2+(xi-X)^2 thus we can find equation Ri = (yi^2 + (xi-X)^2)/(2* yi) so , R = max( Ri = (yi^2 + (xi-X)^2)/(2*yi) )
•  » » 2 weeks ago, # ^ |   0 Hi, I get your point that Ri = (yi^2 + (xi-X)^2)/(2* yi). As this function is quadratic, we can find it's minima using ternary search. But actual function to minimize is R = max( Ri = (yi^2 + (xi-X)^2)/(2*yi) ). So I this case, I don't get how can we assume R is decreasing then increasing and use ternary search to find the answer. Please help, Thanks in advance!!
•  » » » 2 weeks ago, # ^ | ← Rev. 2 →   0 draw graph ( X , Ri ), it can interpreted for ( X , (xi-X)^2/(2* yi)+ yi/ 2 ) if you calculated by "partial differential equation" for X , yi is constant numberso , we can guess it is quadratic functionwe can imagine many quadratic functions in the field ,than where "max is minimum point" has special feature that there is crossed point (at max Ri)even near at "minimum point" increasing is larger than two crossed functions which parallel mid go to minimum point.also you can make skyline that looks similar quadratic function.so you can try ternary search for X .
 » 2 weeks ago, # |   +5 Hello everybody. Is it possible to prove problem C? (why it works that way)
•  » » 2 weeks ago, # ^ |   0 As long as you have both both even and odd numbers the gcd is 1, so you need to get rid of either odd or even. It is faster to get rid of odd and get 2 as gcd (for any other number you have to get rid from all evens numbers before, which is profitable only for trivial case n=3). What happens after you delete all odd numbers? All numbers (and their gcd) are miltipliers of 2. So you can divide everything by 2 and get back 1,2,3... — same problem.hope it helps
•  » » » 2 weeks ago, # ^ | ← Rev. 11 →   +5 In other words, starting with problem size n ≥ 1 and the GCD g = 1, the present sequence can be expressed as {g, 2g, 3g, ..., ng}, the answer to the three base cases when n ≤ 3 are {g}, {g, 2g} and {g, g, 3g}, respectively, and the reduced recursive case when n ≥ 4 is the same problem with problem size floor(n / 2) and the GCD is 2g, where g is appended p =  ceil(n / 2) times to the answer before solving the reduced problem, removing from the present sequence all p odd multiples of g that are not divisible by 2g, typically {g, 3g, 5g, ..., (2p - 1)g}.
•  » » » 2 weeks ago, # ^ |   +10 Thanks
•  » » 11 days ago, # ^ |   0 Intuition is this.Imagine if you could remove every number so gcd becomes gcd*k. Well, you have to remove all the other non divisible by k.Now lets compare what the result would look like if you did it:1111...22222...444444 111..333..99999But we can make the observation that it there are less numbers to remove to get gcd -> 2*gcd than other values of k. Because this takes n/2 numbers as opposed to 2n/3, 3n/4 ... which will thus be lexicographically lower.So it is always optimal to try to remove odd numbers when to rescursively solve.Edge case is 3, where the amount of odd numbers = non multiples of 3, so we take 3 instead of 2. You can look in my code if interested.
 » 2 weeks ago, # | ← Rev. 2 →   0 Hello,Can you please explain what is the advantage of doing middle = sqrt(left * right) in case answer is greater then 1.0 in binary search? [It is code for D problem I am referring to]Thank you!
•  » » 2 weeks ago, # ^ |   +8 Read this post.
 » 2 weeks ago, # |   0 For problem D,why do we init R to 1e16? Also, why is l set to 1e-16 and r to 1e16 in the can function ? I think for yi > 0, there is always a possible solution if we can set radius to infinity, am I wrong?
•  » » 2 weeks ago, # ^ |   0 the edge case is (x,y)=(1e7, 1),(1e7, 1e7),(-1e7, 1), (-1e7, 1e7), as far as I remember solutions is smth like 1e15/2
•  » » » 2 weeks ago, # ^ |   0 Oh, I figured it out after I posted this question, thank you.
 » 2 weeks ago, # |   0 I still don't understand B QAQ Is there anyone can help me.
•  » » 2 weeks ago, # ^ |   0 Just put the pen into whatever possible spot you can, then check if any cell is not painted in the board we make.
•  » » » 13 days ago, # ^ |   0 Thanks, I solve it. Also, sorry to reply you so late.
 » 2 weeks ago, # | ← Rev. 3 →   0 My solution to problem C: Determine that we have write first I numbers, consider the last number, let's call it GCD. we know that next number is one of the multiple of GCD. now we know that every multiple of GCD that smaller or equal to n is in sequence now, and every multiple of any multiple of GCD is one of the multiple of GCD. and we know that for a fix number K, there is exactly floor(n/ K) multiple of K in range of 1 to n. so for every multiple of GCD (let's call it M) we calculate number of multiple of M and between those M that the number of its multiple are maximum we choose the greatest M and for floor(n / GCD) - floor(n / M) print GCD.
 » 2 weeks ago, # | ← Rev. 2 →   0 Does anyone know whats the problem with test 7 in problem E? Here is my submission. Thanks in advance! NVM solved it
 » 2 weeks ago, # | ← Rev. 8 →   0 In problem 1059B - Forgery, another O(mn) algorithm is to check all pen locations in the sub-grid 2 ≤ i ≤ n - 1 and 2 ≤ j ≤ m - 1. If none of the eight neighbors at a pen location (i, j) in this sub-grid is empty, then all these non-empty eight neighbors are painted. Finally, all cells in the grid are checked. If a non-empty cell is not painted, then the answer is "NO". Otherwise, the answer is "YES".43922930
 » 2 weeks ago, # |   0 In the Solution for problem D,how do you find the upper bound for the answer .having 1e18 gave me overflow issues,but upper bound of 1e17 gave me ac. https://codeforces.com/contest/1059/submission/43925303(my code with 1e18) https://codeforces.com/contest/1059/submission/43925436(my code with 1e17)can someone explain how to exactly find the upper bound for the value of the radius?
•  » » 2 weeks ago, # ^ |   0 It's okay if you put it on 1e15.For test 2 -10^7 1 10^7 1 the answer is somthing like 1e15/2.
•  » » » 2 weeks ago, # ^ |   +1 thanks
•  » » 2 weeks ago, # ^ |   0 you can use __float128 for precision.problem that you square long double almost 1e18 and subtraction and sqrt makes a little error.so change some part ld to float128 or how about without sqrt
•  » » » 2 weeks ago, # ^ |   0 thanks.didnt know about __float128.will try using it
 » 2 weeks ago, # |   +3 It is worth noting that in 1059E - Split the Tree you can calculate how far up vertical path that starts in current vertex can go without knowledge of binary lifting.For every vertex calculate sum[vertex] — sum of values in all vertices on path from root to current vertex. Traverse tree recursively and maintain stack of parents for current vertex. We can use binary search over this stack to find last position where sum[current_vertex] - sum[stack[position]] exceeds sum limit in single vertical path. Difference of position and stack size will be the length of maximal vertical path that starts from our current vertex.I've tried to come up to a linear solution for this problem but that was not successful. Can anyone provide a linear solution or prove that it is not possible?
•  » » 5 days ago, # ^ | ← Rev. 2 →   +5 Hi, I believe I have a solution in O(n log* (n)). I just need a DSU. I sort the vertices by height (I can do it in O(n), by count sort), and greedily travel up compressing the paths. Correct me if I am wrong (please).Some dirty code for this: https://codeforces.com/contest/1059/submission/44409581 but I used STL sort here, so it is actually O(n log (n)), but easy to modify to get O(n log* (n)).Don't know if we can get linear though.EDIT: it's not O(n log* (n)) actually, but the time to perform O(n) operations in the DSU structure, so some inverse Ackermann function, or something.
•  » » » 4 days ago, # ^ |   +3 So, If I understand everything correctly, your idea is to pick naive solution (while there is at least one vertex that is not yet included into any path do following: start from the deepest such vertex and start a new path from this vertex, then expand this path upwards as long as we can) and make this observation:It only makes sense to check upward expansion of some path to some vertex that is not yet covered with any other path. So, instead of naively checking every vertex on our path upwards, we want to quickly skip all vertexes included into other paths and check closest not covered vertex to be included into our path. This check (can vertex A be included into the path that has its lowest vertex in B) can be done in O(1) with precalculated vertex depths and sums from each vertex to tree root. DSU can help us to keep groups of visited vertexes together and for every group store the highest vertex of that group to quickly find the next unvisited vertex on our path.Let's prove compexity. In this algorithm, we will cover all the vertexes with the paths and (in worst case) unite all sets of DSU. In details we will repeat this: Start path from deepest not covered vertex and start path upwards expansion — vertexes can be ordered by depth in O(N) using counting sort find the closest unused vertex, uniting all groups of covered vertexes together in DSU that we are skipping — O(N·α(N)) in total of all steps check if we can include found vertex into this path — O(1) for each step if we can include this vertex into our path then include it and continue expansion, otherwise finish expansion Steps 1 and 4 will give us exactly N vertex inclusions into groups of used each done in O(α(N)) in total.So we have O(N·α(N)) solution, where α(N) is inverse Ackermann function (and can be ignored in practical solutions analysis due to its extremely slow growth).Thanks for sharing your ideas, that is really cool improvement of a very simple solution.
 » 2 weeks ago, # |   0 https://codeforces.com/contest/1059/submission/43989515what's wrong with this solution of problem C can anyone please point out ...
•  » » 2 weeks ago, # ^ |   0 never mind got it...
•  » » 2 weeks ago, # ^ |   0 your code didn't consider 3 exponent but consider only at case of 2
•  » » » 2 weeks ago, # ^ | ← Rev. 2 →   0 yes..that was missing ...thanks for pointing it out
 » 2 weeks ago, # |   +1 didn't get D problem's editorial
 » 13 days ago, # | ← Rev. 4 →   0 I have solved E with greedy and a strange binary search.If L is  + ∞,we can use greedy:we must consider leaves first,so every node(except leaves) will join the son with minimum value if the son's value and the node's value are not exceed S,else the node's value is its number.To ignore L,we can binary search C that every node's number will add C,then do the above greedy,if the vertical path with maximum number of nodes is not exceed L,then decrease C,else increase C.This is my submission.However,I don't know whether I'm right.If anyone knows how to prove it or prove it is false,please reply to me,thanks:)ps:My English is poor,if you are Chinese,you can talk with me in Chinese.My email:744388629@qq.com
 » 13 days ago, # |   0 In problem E, the second solution's code, some of the '-' maybe make mistake and it becomes '—'. Please modify it correctly. Thank you.
•  » » 13 days ago, # ^ |   0 Fixed, thank you.
 » 12 days ago, # | ← Rev. 2 →   0 .
 » 11 days ago, # |   0 Can someone give me ideas/methods to reduce my floating-point precision for the ternary + binary search solution?Tried binary search — style ternary search for like 20 times, couldnt get it to work.Still can't get it precise enough for the ternary search, and im out of ideas to optimize (already spent like 6 hours on trying random stuff lol).https://pastebin.com/sCFbMcyVAny ideas/suggestion will be highly appreciated
 » 11 days ago, # | ← Rev. 2 →   0 I am having a hard time with precision in problem D any suggestion will be appreciated 44064198
•  » » 10 days ago, # ^ |   +3 Try changing (high >= low) to something like (low-high < EPS) and set the epsilon to 1e-8 or something.
 » 10 days ago, # |   0 How is something like this passing E 44173661?Pretty sure this hits N^2 behaviour if you make a tree where you have a chain and then at the end of it it spreads out...
 » 9 days ago, # |   0 Consider the following for problem E: Test Case Generatorint n; cin >> n; cout << n << ' ' << n << ' ' << n+1 << endl; for (int i = 1; i <= n; i++) cout << 1 << " \n"[i==n]; for (int i = 2; i <= n/2; i++) cout << i-1 << ' '; for (int i = n/2+1; i <= n; i++) cout << n/2 << " \n"[i==n];Setting n = 1e5 will time out many naive greedy solutions (approx 5000ms on csa editor, cf custom invocation limits input size) such as mineCan you please add such a case because as of right now a whole bunch of N^2 solutions are "shortest solution size" or whatever but actually shouldn't be passing the problem
 » 7 days ago, # |   0 Can you please explain editorial of Nature Reserve little more..
 » 41 hour(s) ago, # |   0 In problem A , is it necessary to complete the smoke continuously. for example.. suppose she will 2min to complete one smoke and we have 2 time interval of 5min.But according to given solution she could take only ((5/2)+(5/2))= 4 smoke but actually she could be able to take ((5+5)/2)=5. so i don't think solution is correct. JAVA Solution must be..import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); long n=sc.nextInt(),l=sc.nextInt(),a=sc.nextInt(); long t1=0,l1=0; if(n>0) {t1=sc.nextInt();l1=sc.nextInt();} long sum1=0; long sum=t1+l1; long count=t1/a; long r=t1%a; for(int i=0;i