CCF-CSP 201703-2 Students line up

Problem description
  Sports teacher Xiao Ming wants to line up the students in his class in order. He first asked the students to line up in order of the student number from small to large, with the student number first in the row, and then made multiple adjustments. Adjusting Xiaoming once may let a classmate leave the team, move forward or backward a certain distance before inserting into the queue.
  For example, the following gives an example of a group of movements, in which the number of students is 8.
  0) The student numbers 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 3 moves backward 2", indicating that student 3 Team, move the distance of 2 classmates back, and then insert into the queue, the student numbers of the students in the new queue are 1, 2, 4, 5, 3, 6, 7, 8;
  2) The second adjustment, the command is "Student No. 8 moves forward 3", means that No. 8 student leaves the team, moves forward the distance of 3 classmates, and then inserts into the queue. The student numbers of the students in the new queue are 1, 2, 4, 5, 8, in order. 3, 6, 7;
  3) The third adjustment, the command is "Student No. 3 moves forward 2", which means that No. 3 student leaves the team, moves forward the distance of 2 classmates, and then inserts into the 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 the students from front to back?
  Please pay special attention that the numbers involved in the above movement refer to the student number, not the position in the team. When moving backwards, the moving distance should not exceed the number of people behind the corresponding classmate. If the moving distance is exactly equal to the number of people behind the corresponding classmate, the classmate will move to the back of the queue. When moving forward, the moving distance should not exceed the number of people in front of the corresponding classmate. If the moving distance is exactly equal to the number of people in front of the corresponding classmate, the classmate will move to the front of the queue.
Input format
  The first line of the input contains an integer n, which represents the number of students, and the student number is numbered from 1 to n.
  The second line contains an integer m, indicating the number of adjustments.
  In the next m lines, there are two integers p, q in each line. If q is positive, it means that the student with the student number p moves backward q, if q is negative, it means that the student with the student number p moves forward -q.
Output format
  Output one line, containing n integers, and two adjacent integers are separated by a space to indicate the 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 size and conventions
  For all evaluation use cases, 1 ≤ n ≤ 1000, 1 ≤ m ≤ 1000, all moves are legal .

Problem-solving ideas:
use a vector to store students with 1-n numbers, and then use the insert and erase methods in the vector to solve this problem.
Summary of experience:
Pay attention to moving forward and backward, the insertion method is different.
Similarly, the method of deleting the original data is different after the forward movement is completed and the backward movement is completed.
Note: During the test, you can print the current sorting situation after each move, so that you can clearly see the process and whether it is consistent with the title description.

C ++ code:

#include<bits/stdc++.h>
using namespace std;
int main() {
	int n,m;
	scanf("%d %d",&n,&m);
	vector<int> v;
	for(int i=1;i<=n;i++){
		v.push_back(i);
	}
	while(m--){
		int p,q;
		scanf("%d %d",&p,&q);
		for(int i=0;i<n;i++){
			if(p == v[i]){
				if(q>0){ //往后移动
					v.insert(v.begin()+i+1+q,v[i]);
					v.erase(v.begin()+i);
				}else{ //往前移动
					v.insert(v.begin()+i+q,v[i]);
					v.erase(v.begin()+i+1);
				}
				break; //记得移动完毕要break退出,不然遇到往后移的,会重复往后移,之后越界
			}
		}
	}
	for(auto i:v){
		printf("%d ",i);
	}
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100712725