201812-1 Xiaoming goes to school

Background of the topic
  Xiaoming is a student in the middle school affiliated to Handong University of Political Science and Law. He rides bicycles to and from home and school every day. In order to get as much sleep as possible, he hopes to be able to predict the time he needs to go to school. He needs to go through several roads to go to school, and there is at most one traffic light between two adjacent roads.
  The traffic lights in Jingzhou City work like this: each traffic light has three red, yellow, and green lights and a display card that shows the countdown. Assuming that the traffic light is set to red light r seconds, yellow light y seconds, and green light g seconds, then from time 0, the red light is on within [0, r) seconds, and the vehicle is not allowed to pass; within [r, r + g) seconds Turn on the green light, the vehicle is allowed to pass; [r + g, r + g + y) turns on the yellow light within seconds, the vehicle is not allowed to pass, and then cycle in turn. The number l (l> 0) displayed on the countdown display card refers to the number of seconds from the next change of the signal light.
Problem Description
  On the way to school, Xiao Ming recorded the time passed by each section of the road, and the color and countdown seconds of each traffic light when Xiao Ming arrived at the intersection. I hope you can help calculate the time Xiaoming spent on school this time.
Input format
  The first line of input contains three positive integers r, y, g separated by spaces, indicating the setting of traffic lights. None of these three numbers exceed 106.
  The second line of the input contains a positive integer n (n ≤ 100), which represents the total number of road segments that Xiao Ming passed and the number of traffic lights he saw.
  The next n lines each contain two integers k and t separated by spaces. k = 0 means passing a period of time, which takes t seconds, where t does not exceed 106; when k = 1, 2, 3, it means that you see a red light, yellow light, green light, and the countdown display shows on the card The number is t, where t will not exceed r, y, and g, respectively.
Output format
  outputs a number indicating the time Xiaoming used to go to 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
70
sample description
  Xiaoming first passed the first section of road, it took 10 seconds, then waited for a red light of 5 seconds, and then passed the second section of road, it took 11 Seconds, then wait for a yellow light for 2 seconds and a red light for 30 seconds, and then pass the third and fourth roads in 6, 3 seconds respectively, then pass the green light, and then pass the last road in 3 seconds. A total of 10 + 5 + 11 + 2 + 30 + 6 + 3 + 3 = 70 seconds.
Evaluation case size and agreement
  Test points 1, 2 do not have any signal lights.
  All signal lights in test points 3 and 4 are green when observed.
  All signal lights in test points 5 and 6 are red lights when observed.
  All signal lights in test points 7 and 8 are yellow lights when observed.
  Various possible situations will appear in test points 9, 10.

#include <iostream>

using namespace std;

int main ()
{
	int r,y,g;
	cin>>r>>y>>g;
	int n;
	cin>>n;
	int time=0;
	while(n--)
	{
		int k,t;
		cin>>k>>t;
		if(k==0)
		{
			time+=t;
		 } 
		else if(k==1)
		{
		 	time+=t;
		} 
		else if(k==2)
		{
			time+=t+r;	
		}
	}
	cout<<time;
	return 0;
 } 

Published 30 original articles · won 9 · visited 1311

Guess you like

Origin blog.csdn.net/weixin_43625164/article/details/105435024