Algorithm
In some country live wizards. They love to ride trolleybuses.
A city in this country has a trolleybus depot with n trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of d meters from the depot. We know for the i-th trolleybus that it leaves at the moment of time ti seconds, can go at a speed of no greater than vi meters per second, and accelerate with an acceleration no greater than a meters per second squared. A trolleybus can decelerate as quickly as you want (magic!). It can change its acceleration as fast as you want, as well. Note that the maximum acceleration is the same for all trolleys.
Despite the magic the trolleys are still powered by an electric circuit and cannot overtake each other (the wires are to blame, of course). If a trolleybus catches up with another one, they go together one right after the other until they arrive at the final station. Also, the drivers are driving so as to arrive at the final station as quickly as possible.
You, as head of the trolleybuses' fans' club, are to determine for each trolley the minimum time by which it can reach the final station. At the time of arrival at the destination station the trolleybus does not necessarily have zero speed. When a trolley is leaving the depot, its speed is considered equal to zero. From the point of view of physics, the trolleybuses can be considered as material points, and also we should ignore the impact on the speed of a trolley bus by everything, except for the acceleration and deceleration provided by the engine.
The first input line contains three space-separated integers n, a, d (1 ≤ n ≤ 105, 1 ≤ a, d ≤ 106) — the number of trolleybuses, their maximum acceleration and the distance from the depot to the final station, correspondingly.
Next n lines contain pairs of integers ti vi (0 ≤ t1 < t2... < tn - 1 < tn ≤ 106, 1 ≤ vi ≤ 106) — the time when the i-th trolleybus leaves the depot and its maximum speed, correspondingly. The numbers in the lines are separated by spaces.
For each trolleybus print a single line the time it arrives to the final station. Print the times for the trolleybuses in the order in which the trolleybuses are given in the input. The answer will be accepted if the absolute or relative error doesn't exceed 10 - 4.
3 10 10000
0 10
5 11
1000 1
1000.5000000000
1000.5000000000
11000.0500000000
1 2 26
28 29
33.0990195136
In the first sample the second trolleybus will catch up with the first one, that will happen at distance 510.5 meters from the depot. The trolleybuses will go the remaining 9489.5 meters together at speed 10 meters per second. As a result, both trolleybuses will arrive to the final station by the moment of time 1000.5 seconds. The third trolleybus will not catch up with them. It will arrive to the final station by the moment of time 11000.05 seconds.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<cstdio>
#include<cmath>
typedef long long ll;
int main(){
ll n, a, d; scanf("%lld %lld %lld", &n, &a, &d);
double y(-1), x(0);
while(n--){
ll t, v; scanf("%lld %lld", &t, &v);
if(2 * d * a > v * v){x=0.5 * v / a + 1.0 * d / v;}
else{x=sqrt(2.0 * d / a);}
x += t;
y = (y > x) ? y : x;
printf("%.5lf\n", y);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
0 10
5 11
1000 1
Output
1000.5000000000
11000.0500000000
Demonstration
Codeforces Solution-A. Wizards and Trolleybuses-Solution in C, C++, Java, Python