【题目描述】
依次放学的时候,小明已经规划好了自己的回家路线,并且能够预测经过各个路段的时间。同时,小明通过学校里安装的“智慧光明”终端,看到了出发时刻路上经过的所有红绿灯的指示状态。请帮忙计算小明此次回家所需要的时间。
【输入格式】
输入的第一行包含空格分隔的三个正整数r,y,g,表示红绿灯的设置.
输入的第二行包含一个正整数n,表示小明总共经过的道路段数和路过的红绿灯数目。
接下来的n行,每行包含空格分隔的两个整数k,t.k=0表示经过了一段道路,将会耗时t秒。k=1,2,3时,分别表示出发时刻,此处的红绿灯状态是红灯、黄灯、绿灯,且倒计时显示牌上现实的数字是t。
【输出格式】
输出一个数字,表示此次小明放学所用的时间。
【样例输入】
30 3 30
8
0 10
1 5
0 11
2 2
0 6
0 3
3 10
0 3
【样例输出】
46
代码:
#include<iostream>
using namespace std;
int main() {
long long r = 0;
long long y = 0;
long long g = 0;
cin >> r >> y >> g;
long long total = r + y + g;
int n = 0;
cin >> n;
long long ** info = new long long*[n];
for (int i = 0; i < n; i++)
info[i] = new long long[2];
long long totaltime = 0;
long long temp = 0;
for (int i = 0; i < n; i++) {
cin >> info[i][0] >> info[i][1];
}
for (int i = 0; i < n; i++) {
if (info[i][0] == 0) {
totaltime += info[i][1];
}
else if (info[i][0] == 1) {
temp = r - info[i][1] + totaltime;
if (temp%total >= 0 && temp%total < r) {
//此时还是红灯
totaltime += r - temp % total;
}
else if (temp%total >= r && temp%total < r + g)
{
//此时是绿灯
}
else if (temp%total >= r + g && temp%total < total) {
//此时是黄灯
totaltime += total - temp % total+r;
}
}
else if (info[i][0] == 2) {
temp = total - info[i][1] + totaltime;
if (temp%total >= 0 && temp%total < r) {
//此时还是红灯
totaltime += r - temp % total;
}
else if (temp%total >= r && temp%total < r + g)
{
//此时是绿灯
}
else if (temp%total >= r + g && temp%total < total) {
//此时是黄灯
totaltime += total - temp % total+r;
}
}
else if (info[i][0] == 3) {
temp = r+g - info[i][1] + totaltime;
if (temp%total >= 0 && temp%total < r) {
//此时还是红灯
totaltime += r - temp % total;
}
else if (temp%total >= r && temp%total < r + g)
{
//此时是绿灯
}
else if (temp%total >= r + g && temp%total < total) {
//此时是黄灯
totaltime += total - temp % total+r;
}
}
}
cout << totaltime << endl;
system("pause");
return 0;
}
三种指示灯的转换关系是:红灯->绿灯->黄灯->红灯