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. 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 . 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 Sample output 1 2 4 3 5 8 6 7 Evaluation use case scale and conventions For all evaluation use cases, 1 ≤ n ≤ 1000, 1 ≤ 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;
}
}
}
}
}