Traffic Lights

本文探讨了一辆汽车从A点到B点,在遇到遵循特定周期变化的交通信号灯时,如何计算最快到达目的地的时间,同时遵守交通规则。文章提供了一个算法解决方案,并附带了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <map>
 5 #include <vector>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstring>
 9 using namespace std;
10 
11 int main()
12 {
13     double l, d, v, g, r;
14     double ans;
15     const double eps = 1e-8;
16     while(cin >> l >> d >> v >> g >> r)
17     {
18         int t = d / v / (g + r);
19         double a = d / v -  t * (g + r);
20         if(fabs(a - g) < eps)
21             ans = l / v + r;
22         else if(a < g)
23             ans = l / v;
24         else
25             ans = g + r - a + l / v;
26         printf("%.8lf\n", ans);
27     }
28     return 0;
29 }

 

转载于:https://www.cnblogs.com/cszlg/p/3246548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值