[CCF Xiao Ming is out of school] Xiao Ming is out of school

Topic background
  Guangming District, where the High School Affiliated to Handong University of Political Science and Law is located, has recently implemented a smart city project called "Smart Brightness". Specifically in the field of transportation, through the "Smart Bright" terminal, you can see the state of all traffic lights in the Guangming District at this moment. Xiao Ming’s school has also installed a "Wisdom Bright" terminal. Xiao Ming wants to use the information given by this terminal to estimate the time it takes for him to return home from school.
Problem description
  When school is over, Xiao Ming has planned his route home, and can predict the time to pass each section of the road. At the same time, Xiao Ming saw the status of all the traffic lights passing by on the road at the time of departure through the "Wisdom Bright" terminal installed in the school. Please help calculate the time Xiao Ming needs to go home this time.
Input format
  The first line of input contains three positive integers r, y, g separated by spaces, which represent the setting of traffic lights. None of these three numbers exceed 106.
  The second line of input contains a positive integer n, which represents the total number of road segments and traffic lights passed by Xiao Ming.
  For the next n lines, each line contains two integers k and t separated by spaces. k=0 means that after a section of road, it will take t seconds, where t does not exceed 106; k=1, 2, 3, respectively represent the departure time, the traffic light status here is red, yellow, and green , And the number displayed on the countdown display card is t, where t does not exceed r, y, and g respectively.
Output format
  outputs a number to indicate the time it took 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
  Xiaoming first passes the first section of the road and takes 10 seconds. When the first traffic light departs, it is a red light, and there are 5 seconds left; when Xiao Ming arrives at the intersection, this traffic light has already turned into a green light, and he does not need to wait to pass directly. Then pass the second section of road, which takes 11 seconds. When the second traffic light started, it was a yellow light, and there were two seconds left; when Xiao Ming arrived at the intersection, the traffic light had changed to a red light, and there were 11 seconds left. Then pass the third and fourth sections of road in 9 seconds. When the third traffic light started, it was a green light, and there were 10 seconds left; when Xiao Ming arrived at the intersection, the traffic light had changed to a red light, and there were two seconds left. After passing the last section of the road, it takes 3 seconds. A total of 10+11+11+9+2+3 = 46 seconds.
Evaluation use case scale and conventions

Some test points have special properties:
  * There is no signal light in the first 2 test points.
  The input data scale of the test points:
  * The first 6 test points ensure that n ≤ 103.
  * All test points guarantee n ≤ 105.

I wrote, and there are many problems. . Seeing the code of a big man, I gave initiation, the idea is too clever. Hurry up and put the link, good things should be shared together.

CCF certification 201812-2 Xiao Ming is out of school

#include <iostream>
using namespace std;

int main() {
    
    
    int light[3], n, k, t;
    cin >> light[0] >> light[2] >> light[1];
    cin >> n;
    int x = light[0] + light[1] + light[2];
    long long sum = 0;
    while (n--) {
    
    
        cin >> k >> t;
        if (k == 0)
            sum += t;
        else {
    
    
            if (k == 1)
                k = 0;
            else if (k == 3)
                k = 1;
            t = (light[k] - t + sum) % x;
            while (t > light[k]) {
    
    
                t -= light[k];
                k = (k + 1) % 3;
            }
            if (k == 0)
                sum += light[k] - t;
            else if (k == 2)
                sum += light[k] - t + light[0];
        }
    }
    cout << sum;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/110771515