Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
0
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
//#include "function.h"

typedef struct _Node{
    int age;
    struct _Node* prev;
    struct _Node* next;
}Node;

Node* head;

/***********************************/
#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED



extern Node* head;
Node* createList(int N){
    head=(Node*)malloc(sizeof(Node)*(N+1));

    for(int i=1;i<=N;i++){
        scanf("%d",&head[i].age);
    }

    for(int i=2;i<=N;i++){
        head[i-1].next=&(head[i]);
        head[i].prev=&(head[i-1]);
    }
    head[N].next=&(head[1]);
    head[1].prev=&(head[N]);

    return head;
}

void find(int A,int K){
    Node* cur=&head[A];
    head[A].next->prev=head[A].prev;
    head[A].prev->next=head[A].next;

    for(int i=0;i<K;i++){
        cur=cur->next;
    }
    head[A].prev=cur;
    head[A].next=cur->next;
    cur->next->prev=&head[A];
    cur->next=&head[A];
}

Node* solve(int N, int M){
    int A,K;
    for(int i=0;i<M;i++){
        scanf("%d %d",&A,&K);
        find(A,K);
    }

    return &head[1];
}

#endif

/***********************************/

int main(){

    int N, M;

    scanf("%d%d", &N, &M);
    head = createList(N);
    Node *p = solve(N, M);
    for(int i = 0; i < N; i++){
        printf("%d ", p -> age);
        p = p -> next;
    }
    printf("\n");
    return 0;
}