CCF CSP Brush Question Record 28-201812-2 Xiao Ming is out of school (java)

Question number: 201812-2
Question name: Xiaoming from school
time limit: 1.0s
Memory limit: 512.0MB
Problem Description:

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 "Wisdom Bright". Specific to the transportation field, 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 he will return home from school.

Problem Description

  When school was over, Xiao Ming had already planned his route home and was able to predict the time he would pass through various road sections. 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 it takes Xiao Ming to go home 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, which represents the total number of road segments and traffic lights passed by Xiao Ming.
  The next n lines, each line contains two integers k and t separated by spaces. k=0 means passing a section of road, it will take t seconds, where t does not exceed 106; k=1, 2, 3, respectively, means the departure time , the traffic lights here are red, yellow, green , And the number displayed on the countdown display card is t, where t does not exceed r, y, and g respectively.

Output format

  Output 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

Sample description

  小明先经过第一段路,用时 10 秒。第一盏红绿灯出发时是红灯,还剩 5 秒;小明到达路口时,这个红绿灯已经变为绿灯,不用等待直接通过。接下来经过第二段路,用时 11 秒。第二盏红绿灯出发时是黄灯,还剩两秒;小明到达路口时,这个红绿灯已经变为红灯,还剩 11 秒。接下来经过第三、第四段路,用时 9 秒。第三盏红绿灯出发时是绿灯,还剩 10 秒;小明到达路口时,这个红绿灯已经变为红灯,还剩两秒。接下来经过最后一段路,用时 3 秒。共计 10+11+11+9+2+3 = 46 秒。

评测用例规模与约定

  有些测试点具有特殊的性质:
  * 前 2 个测试点中不存在任何信号灯。
  测试点的输入数据规模:
  * 前 6 个测试点保证 n ≤ 103。
  * 所有测试点保证 n ≤ 105。

 

这题主要是看周期的,我的方法确实略有繁琐,不过提交上去是100分,以后有时间再做优化。

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

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		long r=sc.nextInt();
		long y=sc.nextInt();
		long g=sc.nextInt(); 
		int n=sc.nextInt();
		long[] a=new long[n];
		long[] b=new long[n];
		for(int i=0;i<n;i++){
			a[i]=sc.nextInt();
			b[i]=sc.nextInt();
		}
		long t=0;
		for(int i=0;i<n;i++){
			if(a[i]==0){
				t+=b[i];
			} else if(a[i]==1){//red
				if(b[i]-t>=0){
					t+=b[i];
				}else{
					long x=(t-b[i])%(r+g+y);
					if(x<=g){//green
							t+=0;
					}else if(x>g&&x<=g+y){//yellow
							t+=g+y-x+r;
					}else if(x>g+y&&x<=y+r+g){//red
							t+=y+r+g-x;
						}
					}
			}else if(a[i]==2){//yellow
				if(b[i]-t>=0){
					t+=b[i]+r;
				}else{
					long x=(t-b[i])%(r+g+y);
					if(x<=r){//red
							t+=r-x;
					}else if(x>r&&x<=r+g){//green
							t+=0;
					}else if(x>g+r&&x<=y+r+g){//yellow
							t+=y+r+g-x+r;
						}
					}
			}else if(a[i]==3){//green
				if(b[i]-t>=0){
					t+=0;
				}else{
					long x=(t-b[i])%(r+g+y);
					if(x<=y){//yellow
							t+=y-x+r;
					}else if(x>y&&x<=r+y){//red
							t+=r+y-x;
					}else if(x>r+y&&x<=y+r+g){//green
							t+=0;
						}
					}
			}
			
					
		}
		
		System.out.println(t);

	}

}

 

Guess you like

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