Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int x[500005];
- int sequence[500005];
- int N, M;
- typedef struct _Node{
- /* data */
- int age;
- struct _Node* next;
- struct _Node* prev;
- }Node;
- int cmp(const void* a,const void* b){
- int A = *(int*)a;
- int B = *(int*)b;
- return A <= B ? -1 : 1;
- }
- Node* insert_seat(int i){
- Node* tmp = (Node*)malloc(sizeof(Node));
- tmp->age = sequence[i];
- tmp->next = NULL;
- tmp->prev = NULL;
- return tmp;
- }
- void change (int rate, int step, char direct, Node* dummy_head){
- if(step == N) return;
- Node* tmp = dummy_head;
- tmp = tmp->next;
- for(int i = 1; i <= N; i++){
- if(tmp->age == rate) break;
- else tmp = tmp->next;
- }
- Node* insert = (Node*)malloc(sizeof(Node));
- insert->age = tmp->age;
- Node* curNode = tmp;
- if(direct == 'R'){
- while(step--){
- curNode = curNode->next;
- }
- curNode->next->prev = insert;
- insert->prev = curNode;
- insert->next = curNode->next;
- curNode->next = insert;
- }
- else{
- while(step--){
- curNode = curNode->prev;
- }
- curNode->prev->next = insert;
- insert->next = curNode;
- insert->prev = curNode->prev;
- curNode->prev = insert;
- }
- tmp->prev->next = tmp->next;
- tmp->next->prev = tmp->prev;
- free(tmp->next);
- free(tmp->prev);
- free(tmp);
- }
- int main(){
- Node* dummy_head = (Node*)malloc(sizeof(Node));
- Node* tail;
- dummy_head->next = NULL;
- dummy_head->prev = NULL;
- tail = dummy_head;
- scanf("%d%d",&N,&M);
- for(int i = 1; i <= N; i++){
- scanf("%d",&x[i]);
- sequence[i] = x[i];
- }
- qsort(x + 1, N, sizeof(int), cmp);
- // for(int i = 1; i <= N; i++) printf("%d ",x[i]);
- // printf("\n");
- for(int i = 1; i <= N; i++){
- for(int j = 1; j <= N; j++){
- if(sequence[i] == x[j] && x[j] != -1){
- sequence[i] = j;
- x[j] = -1;
- break;
- }
- }
- }
- // for(int i = 1; i <= N; i++) printf("%d ",sequence[i]);
- for(int i = 1; i <= N; i++){
- if(i != N){
- tail->next = insert_seat(i);
- tail->next->prev = tail;
- tail = tail->next;
- }
- else{
- tail->next = insert_seat(i);
- tail->next->prev = tail;
- tail = tail->next;
- tail->next = dummy_head->next;
- tail->next->prev = tail;
- }
- }
- tail = dummy_head->next;
- // for(int i = 1; i <= N; i++){
- // printf("%d ",tail->age);
- // tail = tail->prev;
- // }
- while(M--){
- char direct;
- int step, rate;
- // getchar();
- scanf("%d%d %c",&rate, &step, &direct);
- // printf("%d %d %c\n", rate, step, direct);
- change(rate,step,direct,dummy_head);
- }
- printf("%d", tail->age);
- for(int i = 2; i <= N; i++){
- tail = tail->next;
- printf(" %d", tail->age);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment