要点:
每次计算时候,用当前已有时间time对 (r+g+y)取余,将红绿灯时间变化范围缩短在r+g+y范围内。time%(r+g+y)等价为 红绿灯相对于出发时刻状态 已变化的时间。(把 r 、g、y三种灯的状态变化想象成一个圈,循环变化)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int r,y,g;
int n;
cin>>r>>y>>g;
cin>>n;
int k,t;
long long int time=0;
int temp;
while(n--){
cin>>k>>t;
if(k==0){
time+=t;
}
if(k==1){//red
temp=time%(r+y+g);
if(temp<=t){// r
time+=t-temp;
}
else if(temp<=t+g){ // g
time+=0;
}
else if(temp<=t+g+y){ //y
time+=t+g+y-temp+r;
}
else{
time+=r+y+g-temp+t;//r
}
}
if(k==2){ // yellow
temp=time%(r+y+g);
if(temp<=t){ // y
time+=t-temp+r;
}
else if(temp<=t+r){//r
time+=t+r-temp;
}
else if(temp<=t+r+g){//g
time+=0;
}
else{//y
time+=(r+y+g-temp)+t+r;
}
}
if(k==3){ //green
temp=time%(r+y+g);
if(temp<=t){// g
time+=0;
}
else if(temp<=t+y){// y
time+=t+y-temp+r;
}
else if(temp<=t+y+r){// r
time+=t+y+r-temp;
}
}
}
cout<<time<<'\n';
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
错误记录:
在检查修bug的时候,结果把后面的误删了。。。检查半天都没检查出来。。被自己蠢哭了。。
以后写代码一定要小心呐!!!!!