Choose an inflation time so the remaining time lets you draw all n-1 surface segments at speed d, maximizing total drawn length on the inflated sphere.
Medium7GeometryBinary searchMathImplementationNo attempts yetTime limit2sMemory limit512 MBHave you ever played connect-the-dots? For those of us with no talent for drawing, it is one of the few ways to end up with a nice picture. The page has n dots, each labelled with an integer between 1 and n. You draw n−1 straight lines: one between 1 and 2, one between 2 and 3, one between 3 and 4, and so on to the end. Here is an example before and after connecting the dots.

Today you are drawing a connect-the-dots picture on the surface of a balloon. Assume the balloon is a perfect sphere. It starts with a radius of 1 centimetre. I have drawn n dots on the surface and labelled them from 1 to n. As the balloon inflates, the dots move further and further apart. My goal is the largest possible picture, so the total length of the n−1 line segments should be as large as possible.
Because I do not want the image to look faded, I will first inflate the balloon and only then draw the n−1 lines. I can increase the volume of the balloon by v cubic centimetres per minute, and I can draw at a speed of d centimetres per minute. When I draw the line from point i to point i+1, I draw it along the surface of the balloon on a shortest path. Sometimes several shortest paths exist, but this problem only counts lengths, so it does not matter which one I draw.
I have T minutes in total. The time spent inflating the balloon plus the time spent drawing must be at most T minutes. When I stop, all n−1 lines must be fully drawn. Find the largest total line length I can draw.
The first line contains four integers n, T, v and d, where n (2≤n≤1000) is the number of points, T (1≤T≤106) is the time available to both inflate the balloon and draw the n−1 lines in minutes, v (1≤v≤1000) is the rate at which I can inflate the balloon in cubic centimetres per minute, and d (1≤d≤1000) is the speed at which I can draw in centimetres per minute.
The next n lines describe the points in order from 1 to n, with the balloon centred at the origin. Each line holds two real numbers x and y and an integer s (s∈{−1,1}), giving the point on the surface at (x,y,z) where z=s1−x2−y2. It is guaranteed that 0≤x2+y2≤1, and x and y are in centimetres. Each x and y is given to at most 6 digits after the decimal point.
Lines may cross one another or overlap completely, so a segment of the surface may be counted more than once. It is guaranteed that I can draw the picture on the balloon of radius one in at most T minutes.
Print the length in centimetres of the largest completed drawing I can make. Round to 6 digits after the decimal point and print exactly 6 of them.
Be careful when x2+y2=1. Rounding can leave 1−x2−y2 stored as a tiny negative value such as −0.0000000001, and taking its square root then fails. For the same reason the cosine of the angle between two points can land slightly outside [−1,1]. Clamping that value to the range before taking the arc cosine is the safe fix.