CCF CSP Xiaoming school c ++ java python csp201812_2 100 minutes

CCF CSP Xiaoming school c ++ java python csp201812_2 100 minutes

Topic background
  Handong University of Politics and Light District High School Affiliated to the province where the recently introduced called "smart light" smart city projects. Specific to the transport sector, through "smart light" terminal, you can see all the traffic lights bright area of the state at this moment. Xiao Ming school also installed "smart light" terminal, Xiao Ming would like to take advantage of the information given in this terminal, estimated time of your own home from school.
Description of the problem
  once after school, Xiao Ming has been planned route of their own home, and can predict through various sections of time. Meanwhile, Xiao Ming through the "wisdom bright" terminal installed in schools, to see all the traffic lights indicate the status of the road after the departure time. Please help Xiaoming computing time required for the home.
Input format
  of the three first input line comprises a space-separated positive integers r, y, g, represents a set of traffic lights. The three number not more than 10 6 .
  Enter the second line contains a positive integer n, the number of road segments and the number of lights passing through a total of Bob.
  The next n lines, each line contains two space-delimited integers k, t. k = 0 indicates a period after the road, will it takes t seconds, where t is not more than 10 . 6 ; when K = l, 2,3, respectively, the departure time , the state where the traffic light is red, yellow, green light, and the countdown display is a digital display on the card t, t here are no more than r, y, g.
Output format
  output a number that represents the time Bob came home from school with.
Sample input
30 30. 3
. 8
0 10
. 1. 5
0. 11
2 2
0. 6
. 3 0
. 3 10
0. 3
sample output
46
Example Description
  Xiaoming first passage through the first segment, when 10 seconds. Starting the first traffic light is red light, 5 seconds left; when Bob reach the intersection, the traffic light has changed to green, do not wait directly. Next, after the second section passage with a time 11 seconds. Starting the second traffic light is yellow light, left two seconds; Xiaoming when reaching the intersection, the traffic light has changed to red, 11 seconds left. Subsequently through the third, fourth segment road, when a 9 seconds. Starting the third traffic light is green light, 10 seconds left; when Bob reaches the junction, the traffic light has changed to red, left two seconds. Next, after the final section of the road with a time of 3 seconds. Total 10 + 11 + 11 + 9 + 2 + 3 = 46 seconds.
Evaluation scale use cases and agreed to
  have special properties that some test points:
  * there are no lights in the first two test points.
  Scale testing of the input data points:
  * ensure the first six test points 10 ≤ n- . 3 .
  * Ensure that all test points 10 ≤ n- . 5 .
Analysis
       of this question and the first question in comparison with some difficulty, the title is given in the remaining time of departure time of each intersection traffic lights, so when Bob through an intersection, the intersection if it is, you do not need to calculate directly plus you can, if not the intersection, but traffic lights, you need to calculate the current traffic light in this state, then under different circumstances · to calculate the waiting time by the intersection of time. Wherein, as a rule of traffic lights red, green, yellow alternately.
       This problem because the larger size of data, ** int type will overflow, so the results use the long type for storage, which is the reason a lot of people do not get out of. ** But if using python then do not consider this issue.
c ++ code is as follows

#include<cstdio>
typedef long long ll;
int main(){
	int r,y,g;
	scanf("%d%d%d",&r,&y,&g);
	int count;
	scanf("%d",&count);
	int index,time;
	ll result = 0;
	ll total = r + y + g;
	for(int i = 0;i < count;i++){
		scanf("%d%d",&index,&time);
		if(index == 0){
			result += time;
		}
		else if(index == 1){
			ll temp = result % total;
			if(temp < time){
				result += (time - temp);
			}
			else {
				if(temp > time + g){
					if(temp < time + g + y){
						ll tt = temp - time - g;
						result += (r + y - tt);
					}
					else{
						ll tt = temp - time - g - y;
						result += (r - tt);
					}
				}
			}	
		}
		else if(index == 2){
			ll temp = result % total;
			if(temp < time){
				result += (time - temp + r);
			}
			else{
				if(temp < time + r){
					ll tt = temp - time;
					result += (r - tt);
				}
				else if(temp > time + r + g){
					ll tt = temp - time - r - g;
					result += (y - tt + r); 
				}
			}
		}
		else{
			ll temp = result % total;
			if(temp > time){
				if(temp < time + y){
					ll tt = temp - time;
					result += (y - tt + r);
				}
				else{
					if(temp < time + y + r){
						ll tt = temp - time - y;
						result += (r - tt);
					}
				}
			}
		}
	}
	printf("%lld\n",result);
	return 0;
} 

java code as follows

import java.util.Scanner;

public class csp201812_2 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int r = input.nextInt();
		int y = input.nextInt();
		int g = input.nextInt();
		int count = input.nextInt();
		long total = r + y + g;
		long result = 0;
		int index;
		int time;
		for(int i = 0;i < count;i++){
			index = input.nextInt();
			time = input.nextInt();
			if(index == 0){
				result += time;
			}
			else if(index == 1){
				long temp = result % total;
				if(temp < time){
					result += (time - temp);
				}
				else {
					if(temp > time + g){
						if(temp < time + g + y){
							long tt = temp - time - g;
							result += (r + y - tt);
						}
						else{
							long tt = temp - time - g - y;
							result += (r - tt);
						}
					}
				}	
			}
			else if(index == 2){
				long temp = result % total;
				if(temp < time){
					result += (time - temp + r);
				}
				else{
					if(temp < time + r){
						long tt = temp - time;
						result += (r - tt);
					}
					else if(temp > time + r + g){
						long tt = temp - time - r - g;
						result += (y - tt + r); 
					}
				}
			}
			else{
				long temp = result % total;
				if(temp > time){
					if(temp < time + y){
						long tt = temp - time;
						result += (y - tt + r);
					}
					else{
						if(temp < time + y + r){
							long tt = temp - time - y;
							result += (r - tt);
						}
					}
				}
			}
		}
		System.out.println(result);
		input.close();
	}

}

python3 code is as follows

ryg = input().split(" ")
r = (int)(ryg[0])
y = (int)(ryg[1])
g = (int)(ryg[2])
count = (int)(input())
total = r + y + g
result = 0
for i in range(count):
    te = input().split()
    index = (int)(te[0])
    time = (int)(te[1])
    if index == 0:
        result += time
    elif index == 1:
        temp = result % total
        if temp < time:
            result += (time - temp)
        else:
            if temp > time + g:
                if temp < time + g + y:
                    tt = temp - time - g
                    result += (r + y - tt)
                else:
                    tt = temp - time - g - y
                    result += (r - tt)
    elif index == 2:
        temp = result % total
        if temp < time:
            result += (time - temp + r)
        else :
            if temp < time + r:
                tt = temp - time
                result += (r - tt)
            elif temp > time + r + g:
                tt = temp - time - r - g
                result += (y - tt + r)
    else :
        temp = result % total
        if temp > time:
            if temp < time + y:
                tt = temp - time
                result += (y - tt + r)
            else:
                if temp < time + y + r:
                    tt = temp - time - y
                    result += (r - tt)
print(result)

ok!'re done, if you have a better way, can communicate oh in the comments area.

Published 19 original articles · won praise 13 · views 4087

Guess you like

Origin blog.csdn.net/qq_38929464/article/details/86891251