Topic background
Guangming District, where the middle school affiliated to Handong University of political science and law is located, recently implemented a smart city project called "smart light". Specifically in the field of transportation, you can see the status of all traffic lights in the bright area at this moment through the "smart bright" terminal. Xiaoming's school has also installed a "smart light" terminal. Xiaoming wants to use the information given by this terminal to estimate the time when he comes home from school.
Problem description
Once after school, Xiao Ming had planned his way home and could predict the time of passing through each section. At the same time, Xiao Ming saw the indication status of all traffic lights on the road at the departure time through the "smart light" terminal installed in the school. Please help calculate the time required for Xiao Ming to go home this time.
Input format
The first line of input contains three positive integers r, y and g separated by spaces, indicating the setting of traffic lights. These three numbers do not exceed 106.
The second line of input contains a positive integer n, indicating the total number of road segments and traffic lights Xiaoming has passed.
The next n lines contain two integers K and t separated by spaces. k=0 means that it will take T seconds to pass a section of road, where t does not exceed 106; When k=1, 2 and 3, it indicates the departure time respectively. The traffic light status here is red, yellow and green, and the number displayed on the countdown display board is t, where T will not exceed r, y and g respectively. (it means that when Xiao Ming reaches the intersection, he needs to consider what kind of traffic lights are at this time)
Output format
Output a number indicating the time taken for Xiao Ming to go home from school this time.
sample input
30 3 30
8
0 10
1 5
0 11
2 2
0 6
0 3
3 10
0 3
sample output
46
Example description
Xiao Ming passed the first section of the road first, which took 10 seconds. The first traffic light starts with a red light, with 5 seconds left; When Xiao Ming arrives at the intersection, the traffic light has turned green. He doesn't have to wait to pass directly. Next, it takes 11 seconds to pass the second section of the road. The second traffic light starts with a yellow light, with two seconds left; When Xiao Ming arrives at the intersection, the traffic light has changed to a red light with 11 seconds left. It takes 9 seconds to pass the third and fourth sections. The third traffic light is green when it starts, with 10 seconds left; When Xiao Ming arrives at the intersection, the traffic light has changed to a red light with two seconds left. It takes 3 seconds to go through the last section of the road. Total 10 + 11 + 11 + 9 + 2 + 3 = 46 seconds.
Scale and agreement of evaluation cases
Some test points have special properties:
* there are no signal lights in the first two test points.
Input data scale of test point:
* the first 6 test points ensure n ≤ 103.
* ensure n ≤ 105 at all test points.
#include <iostream> #include <algorithm> #include <stdio.h> #include <stack> #include <cstring> #include <math.h> #define MAX 1005 using namespace std; int main() { int r, y, g; cin >> r >> y >> g; int n; cin >> n; int k, t; long long sum = 0;//Calculate the last time int x = 0;//Used to select the traffic lights when Xiao Ming arrives at the intersection for (int i = 0; i < n; i++) { cin >> k >> t; if (k == 0) //Straight ahead { sum += t; } else if (k == 1) { //It is used to judge the traffic lights when Xiao Ming reaches the intersection in an r+y+g cycle x = sum % (r + y + g); if (x < t) //red light { sum += t - x;//The red light does not change when driving in front. You need to wait when you reach the intersection } else if (x >= t && x < t + g) //green light { continue;//adopt } else if (x >= t + g && x < t + g + y) //Yellow lamp { //The yellow light is impassable //When you reach the yellow light at the intersection, you need to wait for the remaining seconds of yellow light + the time of a red light sum += t + g + y + r - x; } else if (x >= t + g + y && x < r + g + y) //red light { sum += t + g + y + r - x;//Wait for the remaining red light seconds } } else if (k == 2) { x = sum % (r + y + g); if (x < t) //Yellow lamp { sum += t + r - x;//Yellow light needs to wait } else if (x >= t && x < t + r) //red light { sum += t + r - x;//Wait at red light } else if (x >= t + r && x < t + r + g) //green light { continue; } else if (x >= t + r + g && x < r + g + y) //Yellow lamp { sum += t + r + g + y + r - x;//Yellow light needs to wait } } else if (k == 3) { x = sum % (r + y + g); if (x < t) //green light { continue; } else if (x >= t && x < t + y) //Yellow lamp { sum += t + y + r - x;//Yellow light needs to wait } else if (x >= t + y && x < t + r + y) //red light { sum += t + y + r - x;//Wait at red light } else if (x >= t + r + y && x < r + g + y) //green light { continue; } } } cout<<sum; }
Reference blog address link
https://blog.csdn.net/happywlg123/article/details/87864575