由题可知路程所花费的时间一定为 $l$,所以我们要计算的便是等待红灯所花费的时间。

Luka 开始开车时,所有交通信号灯都呈红色,并且开始循环。

信号灯将按 $d$ 升序排列。

这两句话是本题的关键。因为有序读入红绿灯的距离,所以只要从 $1$ 至 $n$ 顺次枚举。由题意知每个红绿灯的周期为 $g_i + r_i$,所以每到一个路口时,判断当前的状况,若为红灯,则在答案中加上等红灯的时间,并会影响下一次的情况判断。

给下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define init(x) memset (x,0,sizeof (x))
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 150;
const int MOD = 1e9 + 7;
ll read ();
int n,m,ans;
struct traffic
{
int s,r,g;
} a[MAX];
int main ()
{
//freopen (".in","r",stdin);
//freopen (".out","w",stdout);
n = read ();m = read ();
for (int i = 1;i <= n;++i)
{
a[i].s = read ();
a[i].r = read ();
a[i].g = read ();
}
for (int i = 1;i <= n;++i)
{
ans += a[i].s - a[i - 1].s;//路程距离
int rest = ans % (a[i].r + a[i].g);//留下单个周期的不完整部分
if (rest < a[i].r) ans += a[i].r - rest;//等待红灯的时间
}
printf ("%d\n",ans + (m - a[n].s));//最后的路程不能忘
return 0;
}
ll read ()
{
ll s = 0;int f = 1;
char ch = getchar ();
while ((ch < '0' || ch > '9') && ch != EOF)
{
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9')
{
s = s * 10 + ch - '0';
ch = getchar ();
}
return s * f;
}