CCF CSP Brush Question Record 27-201812-1 Xiao Ming goes to school (Java)

Question number: 201812-1
Question name: Xiao Ming goes to school
time limit: 1.0s
Memory limit: 512.0MB
Problem Description:

Topic background

  Xiao Ming is a student at the Affiliated Middle School of the Handong University of Political Science and Law. He rides a bicycle to and from home and school every day. In order to get as much sleep as possible, he hopes to be able to estimate the time he needs to go to school. He needs to pass several sections of roads to go to school, and there is at most one traffic light between two adjacent sections of roads.
  The traffic lights in Jingzhou City work like this: each traffic light has three lights, red, yellow, and green, and a sign that can display the countdown. Assuming that the traffic lights are set to red light for r seconds, yellow light for y seconds, and green light for g seconds, then from time 0, the red light is on within [0,r) seconds, and vehicles are not allowed to pass; within [r, r+g) seconds The green light is on, the vehicle is allowed to pass; the yellow light is on within [r+g, r+g+y) seconds, and the vehicle is not allowed to pass, and then cycle in turn. The number l (l> 0) displayed on the countdown display is the number of seconds until the next signal light change.

Problem Description

  On the way to school, Xiao Ming recorded the time of each section of the road, the color of each traffic light when Xiao Ming arrived at the intersection and the countdown seconds. I hope you can help me calculate the time Xiao Ming spent in 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 input contains a positive integer n (n ≤ 100), which represents the total number of road segments that Xiao Ming has passed and the number of traffic lights he saw.
  The next n lines, each line contains two integers k and t separated by spaces. k=0 means that a section of road has passed and it takes t seconds, where t does not exceed 106; when k=1, 2, 3, it means that a red light, a yellow light, and a green light are seen, and the countdown display shows The number is t, where t does not exceed r, y, and g respectively.

Output format

  Output a number to indicate the time Xiao Ming spent in 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

  Xiao Ming first passes the first section of the road, which takes 10 seconds, then waits for 5 seconds for the red light, and then passes through the second section of the road, which takes 11 seconds, then waits for 2 seconds for the yellow light and 30 seconds for the red light, and then passes through the third section The fourth section of road takes 6 and 3 seconds respectively, and then passes the green light, and then passes through the last section of road, which takes 3 seconds. A total of 10 + 5 + 11 + 2 + 30 + 6 + 3 + 3 = 70 seconds.

Evaluation use case scale and conventions

  There are no signal lights in test points 1, 2.
  All the signal lights in test points 3 and 4 are green when being observed.
  All signal lights in test points 5 and 6 are red when being observed.
  All the signal lights in test points 7, 8 are yellow when being observed.
  Various possible situations will appear in test points 9, 10.

import java.util.Scanner;
public class 小明上学 {

	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 n=sc.nextInt();
		int[] a=new int[n];
		int[] b=new int[n];
		for(int i=0;i<n;i++){
			a[i]=sc.nextInt();
			b[i]=sc.nextInt();
		}
		int res=0;
		for(int i=0;i<n;i++){
		if(a[i]==0){
			res+=b[i];
		}else if(a[i]==1){
			res+=b[i];
		}else if(a[i]==2){
			res+=b[i]+r;
			
		}else if(a[i]==3){
			res+=0;
		}
		}
		System.out.println(res);
	}

}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108366432