P1160 queue arrangement and p1160 queue Arrangement
Description
In a school, the teacher will arrange N students in the class into a column, and the students will be numbered 1 ~ N, he takes the following method:
1. First arrange the first student into the queue, then there is only one person in the queue;
2.2 ~ Students on N are listed in sequence, and the way the students numbered I are listed is as follows: Students numbered I specified by the teacher are numbered 1 ~ In I-1, the left or right side of a student (that is, the person who has already joined the column;
3. Remove M (M <N) students from the queue. The positions of other students remain unchanged.
After all the students arranged in the queue according to the above method, the teacher wants to know the numbers of all the students from left to right.
Input/Output Format
Input Format:
The 1st behavior of the input file arrange. in is a positive integer N, indicating that N students exist.
2nd ~ Line N, line I contains two integers k, p, where k is a positive integer smaller than I, p is 0 or 1. If p is 0, students I are inserted to the left of students k. If p is 1, students I are inserted to the right.
The N + 1 behavior is a positive integer M, indicating the number of removed students.
In the next M line, each row has a positive integer x, which indicates that the number x is removed from the queue. If the number x is no longer in the queue, this command is ignored.
Output Format:
The input file arrange. out contains only one row and contains a maximum of N positive integers separated by spaces. It indicates the numbers of all the students from left to right in the queue. The line ends with a line break without spaces.
Input and Output sample
Input example #1:
41 02 11 0233
Output sample #1:
2 4 1 insert classmate 2 to the left of classmate 1. The queue is: 2 1 insert classmate 3 to the right of classmate 2. the queue is: 2 3 1 insert classmate 4 to the left of classmate 1. The queue is: 2 3 4 1 remove classmate 3 from the queue. The queue is: 2 4 1 student 3 is no longer in the queue, ignore the last command and the final queue: 2 4 1
Description
For 20% of the data, N ≤ 10;
For 40% of data, N ≤ 1000;
For 100% of data, N, M ≤ 100000.
The tag is a queue, but the positive solution is a linked list ..
The idea is clear, that is, a linked list.
But the logic may be a little complicated.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 int read (int & n) 7 {8 char c = '-'; int x = 0; 9 while (c <'0' | c> '9') c = getchar (); 10 while (c> = '0' & c <= '9') 11 {12 x = x * 10 + (c-48); 13 c = getchar (); 14} 15 n = x; 16} 17 const int MAXN = 100001; 18 struct node19 {20 int pre, nxt, pos, flag; 21} s [MAXN]; 22 int n, m, where, how; 23 int main () 24 {25 read (n); 26 for (int I = 1; I <= N; I ++) 27 {28 s [I]. flag = 1; // 29 s [I] will certainly appear at the beginning. pos = 0; 30 s [I]. pre = 0; 31 s [I]. nxt = 0; 32} 33 s [1]. pos = 1; 34 for (int I = 2; I <= n; I ++) 35 {36 read (where); // target student 37 read (how ); 38 if (how = 0) // 39 {40 s [s [where] on the left. pre]. nxt = I; 41 s [I]. pre = s [where]. pre; 42 s [where]. pre = I; 43 s [I]. nxt = where; 44 if (s [where]. pos = 1) 45 {46 s [where]. pos = 0; 47 s [I]. pos = 1; 48} 49} 50 else51 {52 s [s [where]. nxt]. pre = I; 53 s [I]. nxt = s [where]. nxt; 54 S [where]. nxt = I; 55 s [I]. pre = where; 56 57} 58} 59 60 read (m); 61 for (int I = 1; I <= m; I ++) 62 {63 read (where); 64 if (s [where]. flag = 0) continue; 65 if (s [where]. pos = 1) 66 s [s [where]. nxt]. pos = 1; 67 s [where]. flag = 0; 68 s [s [where]. pre]. nxt = s [where]. nxt; 69 s [s [where]. nxt]. pre = s [where]. pre; 70} 71 for (int I = 1; I <= n; I ++) 72 {73 if (s [I]. pos = 1) 74 {75 printf ("% d", I); 76 int p = s [I]. nxt; 77 while (p! = 0) 78 {79 printf ("% d", p); 80 p = s [p]. nxt; 81} 82} 83} 84 return 0; 85}