Traffic Lights

Traffic Lights
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A car moves from point A to point B at speed v meters per second. The action takes place on the X-axis. At the distance d meters from A there are traffic lights. Starting from time 0, for the first g seconds the green light is on, then for the following r seconds the red light is on, then again the green light is on for the g seconds, and so on.

The car can be instantly accelerated from 0 to v and vice versa, can instantly slow down from the v to 0. Consider that it passes the traffic lights at the green light instantly. If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn't have time to pass it. But if it approaches the traffic lights at the moment when the green light has just turned on, it can move. The car leaves point A at the time 0.

What is the minimum time for the car to get from point A to point B without breaking the traffic rules?

提示:

1.注意“If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn't have time to pass it”。

2.只要不是等待绿灯,都全速行驶

AC  Code:

 #include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; int main()
{
double l, d, v, g, r;
double ans;
const double eps = 1e-;
while(cin >> l >> d >> v >> g >> r)
{
int t = d / v / (g + r);
double a = d / v - t * (g + r);
if(fabs(a - g) < eps)
ans = l / v + r;
else if(a < g)
ans = l / v;
else
ans = g + r - a + l / v;
printf("%.8lf\n", ans);
}
return ;
}
上一篇:Visual Studio 2013 Web开发、新增功能:“Browser Link”


下一篇:golang的ssh例子