题目不列了,直接说思路。
记 t1 为一轮 "绿黄红" 的用时,即 t1 = g + y + r ;
算法:
1、假设小明刚出发时第n个红绿灯刚好切换为绿灯,即出门时绿灯倒计时为g。
2、走过去用的时间对t1取余就是最新一轮 "绿黄红"已经经过了的时间,如果这个时间小于g,则是绿灯,否则需要等待,等待用时为一轮总时间t1减去这个取余得到的值。
但是怎么让出发时刚好为绿灯呢,可以让小明提前出门并且走慢点!举个例子:
实际:小明出门时某个灯为黄灯倒计时为x,小明走到过去用了total时刻,
现在:小明提前 t2 = ( y - t ) + g 时刻出门,那么出门时刻该灯j就刚好切换到绿灯状态,现在再让小明走慢点,过去要用total+t2时刻。因为提前出门但是又走慢了,换算过来,小明到该红绿灯时,红绿灯状态和实际上一毛一样。
那么,对于每个灯我们都作如上换算,利用total+t2这个数以及最开始说到的算法就可以确定小明要等待的时间了。
代码:
#include<iostream>
using namespace std;
int main()
{
int r,y,g,n,k,t;
long long t1,t2;
long long total=0;
cin >> r >> y >> g;
cin >> n;
t1 = r + y + g;
for(int i=0; i<n; i++)
{
cin >> k >> t;
if(k==1) t2 = (r-t) + y + g; //保证刚出门时为绿灯,因此要提前t2时刻出门
else if(k==2) t2 = (y-t) + g;
else if(k==3) t2 = (g-t);
if(k==0) total += t;
else if((total+t2)%t1 < g) total += 0;
else total += ( t1 - (total+t2)%t1 );
}
cout << total;
return 0;
}