nelson33

Untitled

Mar 6th, 2023
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int x[500005];
  5. int sequence[500005];
  6. int N, M;
  7.  
  8. typedef struct _Node{
  9.     /* data */
  10.     int age;
  11.     struct _Node* next;
  12.     struct _Node* prev;
  13. }Node;
  14.  
  15. int cmp(const void* a,const void* b){
  16.     int A = *(int*)a;
  17.     int B = *(int*)b;
  18.     return A <= B ? -1 : 1;
  19. }
  20.  
  21. Node* insert_seat(int i){
  22.     Node* tmp = (Node*)malloc(sizeof(Node));
  23.     tmp->age = sequence[i];
  24.     tmp->next = NULL;
  25.     tmp->prev = NULL;
  26.     return tmp;
  27. }
  28.  
  29. void change (int rate, int step, char direct, Node* dummy_head){
  30.     if(step == N) return;
  31.     Node* tmp = dummy_head;
  32.     tmp = tmp->next;
  33.     for(int i = 1; i <= N; i++){
  34.         if(tmp->age == rate) break;
  35.         else tmp = tmp->next;
  36.     }
  37.     Node* insert = (Node*)malloc(sizeof(Node));
  38.     insert->age = tmp->age;
  39.     Node* curNode = tmp;
  40.     if(direct == 'R'){
  41.         while(step--){
  42.             curNode = curNode->next;
  43.         }
  44.         curNode->next->prev = insert;
  45.         insert->prev = curNode;
  46.         insert->next = curNode->next;
  47.         curNode->next = insert;
  48.     }
  49.     else{
  50.         while(step--){
  51.             curNode = curNode->prev;
  52.         }
  53.         curNode->prev->next = insert;
  54.         insert->next = curNode;
  55.         insert->prev = curNode->prev;
  56.         curNode->prev = insert;
  57.     }
  58.     tmp->prev->next = tmp->next;
  59.     tmp->next->prev = tmp->prev;
  60.     free(tmp->next);
  61.     free(tmp->prev);
  62.     free(tmp);
  63. }
  64.  
  65. int main(){
  66.     Node* dummy_head = (Node*)malloc(sizeof(Node));
  67.     Node* tail;
  68.     dummy_head->next = NULL;
  69.     dummy_head->prev = NULL;
  70.     tail = dummy_head;
  71.     scanf("%d%d",&N,&M);
  72.     for(int i = 1; i <= N; i++){
  73.         scanf("%d",&x[i]);
  74.         sequence[i] = x[i];
  75.     }
  76.     qsort(x + 1, N, sizeof(int), cmp);
  77.     // for(int i = 1; i <= N; i++) printf("%d ",x[i]);
  78.     // printf("\n");
  79.     for(int i = 1; i <= N; i++){
  80.         for(int j = 1; j <= N; j++){
  81.             if(sequence[i] == x[j] && x[j] != -1){
  82.                 sequence[i] = j;
  83.                 x[j] = -1;
  84.                 break;
  85.             }
  86.         }
  87.     }
  88.     // for(int i = 1; i <= N; i++) printf("%d ",sequence[i]);
  89.     for(int i = 1; i <= N; i++){
  90.         if(i != N){
  91.             tail->next = insert_seat(i);
  92.             tail->next->prev = tail;
  93.             tail = tail->next;
  94.         }
  95.         else{
  96.             tail->next = insert_seat(i);
  97.             tail->next->prev = tail;
  98.             tail = tail->next;
  99.             tail->next = dummy_head->next;
  100.             tail->next->prev = tail;
  101.         }
  102.     }
  103.     tail = dummy_head->next;
  104.     // for(int i = 1; i <= N; i++){
  105.     //     printf("%d ",tail->age);
  106.     //     tail = tail->prev;
  107.     // }
  108.     while(M--){
  109.         char direct;
  110.         int step, rate;
  111.         // getchar();
  112.         scanf("%d%d %c",&rate, &step, &direct);
  113.         // printf("%d %d %c\n", rate, step, direct);
  114.         change(rate,step,direct,dummy_head);
  115.     }
  116.     printf("%d", tail->age);
  117.     for(int i = 2; i <= N; i++){
  118.         tail = tail->next;
  119.         printf(" %d", tail->age);
  120.     }
  121.     printf("\n");
  122. }
Advertisement
Add Comment
Please, Sign In to add comment