Find the fastest path across n horizontal layers with different speeds, entering and exiting each layer boundary at an optimal x position, and print the minimum travel time.
Hard8Dynamic programmingMathGeometryBinary searchNo attempts yetTime limit2sMemory limit512 MBAn ironman triathlon puts the athlete through 3.86 km of swimming, 180.25 km of cycling and a full marathon, and it is one of the hardest races in the sport. Viveka trains for something harder, the n-athlon. In an n-athlon the racers cross several kinds of terrain, water, sand, ice, asphalt, on the way from the starting point to the finishing point, and each racer picks the route that suits her own strengths. Last year Viveka skated the final 40 km over ice in one hour and won, while her rival Veronica sat stuck in a tar pit one metre short of the finish.
The terrain map for this year is out, and Viveka's team wants the fastest route. The race area is flat and is modelled as the plane, measured in metres. It is cut into n horizontal layers, and Viveka moves at speed vi everywhere inside layer i, so the time she spends in a layer is the length of her path inside that layer divided by vi. She may not leave the race area. Compute the smallest time she needs to get from the starting point to the finishing point.
The first line contains four decimal numbers xs, ys, xf, yf, the coordinates of the starting point and of the finishing point, in metres.
The second line contains one integer n (1≤n≤10000), the number of layers.
The third line contains n−1 decimal numbers y1,y2,…,yn−1, the y coordinate of each boundary between consecutive layers, given in order, so that ys<y1<y2<⋯<yn−1<yf. With y0=ys and yn=yf, layer i is the region (−10000,10000)×(yi−1,yi). When n=1 this line is empty.
The fourth line contains n decimal numbers v1,…,vn, Viveka's speed in each layer, in metres per second. Every vi is positive.
Every decimal number in the input has absolute value at most 104 and at most 4 digits after the decimal point.
Print the minimum time in seconds for Viveka to get from the starting point to the finishing point, rounded to exactly 6 digits after the decimal point.