CCF CSP Brush Question Record 18-201703-2 Student Queuing (Java)

Question number: 201703-2
Question name: Students line up
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Xiao Ming, the physical education teacher, queues up the students in his class in order. He first arranged the students in a row according to the student number from small to large, with the small student number in front, and then made multiple adjustments. One adjustment to Xiao Ming may allow a classmate to leave the team, move forward or backward a certain distance before inserting into the queue.
  For example, the following is a set of mobile examples, in which the number of students is 8.
  0) The student IDs of the students in the initial queue are 1, 2, 3, 4, 5, 6, 7, 8;
  1) For the first adjustment, the command is "Student No. 3 moves backward by 2", which means that Student No. 3 is out Team, move back the distance of 2 students, and then insert into the queue, the student numbers of the new queue are 1, 2, 4, 5, 3, 6, 7, 8;
  2) The second adjustment, the command is "Student No. 8 moves forward by 3" means that student No. 8 leaves the team, moves forward the distance of 3 students, and then inserts them into the queue. The student numbers of the new queue are 1, 2, 4, 5, 8, 3, 6, 7;
  3) For the third adjustment, the command is "No. 3 student moves forward 2", which means that student No. 3 leaves the team, moves forward the distance of 2 students, and then inserts them into the queue. Students in the new queue The student numbers are 1, 2, 4, 3, 5, 8, 6, 7.
  Xiao Ming recorded all the adjustment process. May I ask, what are the student numbers of all students from front to back?
  Please pay special attention that the numbers involved in the above move refer to the student number, not the position in the team. When moving backward, the moving distance does not exceed the number of people behind the corresponding classmate. If the distance moved backward is exactly equal to the number of people behind the corresponding classmate, the student will move to the end of the queue. When moving forward, the moving distance does not exceed the number of people in front of the corresponding classmate. If the distance moved forward is exactly equal to the number of people in front of the corresponding classmate, the student will move to the top of the queue.

Input format

  The first line of input contains an integer n , which represents the number of students. The student numbers are numbered from 1 to n .
  The second line contains an integer m , indicating the number of adjustments.
  In the next m lines, each line contains two integers p, q. If q is positive, it means that the student with student number p moves backward by q, and if q is negative, it means that student with student number p moves forward -q.

Output format

  Output a line containing n integers, separated by a space between two adjacent integers, indicating the final student numbers of all students from front to back.

Sample input

8
3
3 2
8 -3
3 -2

Sample output

1 2 4 3 5 8 6 7

Evaluation use case scale and conventions

  For all evaluation use cases, 1 ≤ n  ≤ 1000,  ≤  m ≤ 1000, all moves are legal.

 

I use an array to operate, according to the addition and subtraction of the student position of each student number, and finally output the index number in order, which is the student number. Then I summarized a change rule:

  1 2 3 4 5 6 7 8
3 2 (all numbers in the range of (3, 5) on the previous line are -1) 1 2 5 3 4 6 7 8
8 -3 (all numbers in the range of [3, 8) on the previous line are +1) 1 2 6 3 4 7 8 5
3 -2 (all numbers in the range of [4, 6) on the previous line are +1) 1 2 4 3 5 7 8 6

The following is the full score code:

import java.util.Scanner;
public class 学生排队 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int[] a=new int[n+1];
		for(int i=1;i<=n;i++){
			a[i]=i;
		}
		
		
		for(int i=1;i<=m;i++){
			int x=sc.nextInt();
			int y=sc.nextInt();	
			int l=a[x];
			int r=a[x]+y;
			 a[x]=a[x]+y;
			if(y>0){
			for(int k=l;k<=r;k++){
				
					for(int j=1;j<=n;j++){
						if(a[j]==k&&j!=x){
							a[j]-=1;
						}
					}
			}
			}
			 if(y<0){
				 for(int k=l;k>=r;k--){
				for(int j=1;j<=n;j++){
					if(a[j]==k&&j!=x){
						a[j]+=1;
					}
				}
			}
		}
				 
		
		}
		
//		for(int i=1;i<=n;i++){
//			
//				System.out.print(a[i]+" ");
//				
//			}
		for(int j=1;j<=n;j++){
		for(int i=1;i<=n;i++){
			if(a[i]==j){
				System.out.print(i+" ");
				break;
			}
		}
	}
		
		
		
	}

}

 

Guess you like

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