思路:
红绿灯每种灯亮划分区间,在[0,r]区间内红灯亮,在(r,g+r]区间内绿灯亮,在(r+g,r+g+y]区间内黄灯亮,在划分好区间后只需要判断当小明到达红绿灯时是哪个灯在亮,就可以判断出通过红绿灯的时间
余数表示在一个新的周期中,灯已经走过的时间。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int y =sc.nextInt();
int g = sc.nextInt();
int x = g+r+y;
int n = sc.nextInt();
long sum = 0;//int会溢出
for(int i=0;i<n;i++) {
int flag = sc.nextInt();
int time = sc.nextInt();
long tmp = 0;
if(flag == 0) {
sum += time;
}
else {
if(flag == 1) { //红灯
tmp = (sum-time+r)%x;
}
else if(flag == 2) //黄灯
tmp = (sum-time)%x;
else if(flag == 3) { //绿灯
tmp = (sum-time+r+g)%x;
}
if(tmp<=r) { //在[0,r]红灯区间内
sum += r-tmp;
}
else if(tmp>r+g) //在(r+g,r+g+y]黄灯区间内
sum +=x-tmp+r;
}
}
sc.close();
System.out.println(sum);
}
}