Skip to content

Instantly share code, notes, and snippets.

@hsuan1117
Created March 4, 2024 15:29
Show Gist options
  • Save hsuan1117/072734d761158e8fe7cc37b957f968c7 to your computer and use it in GitHub Desktop.
Save hsuan1117/072734d761158e8fe7cc37b957f968c7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
struct Node *prev;
} Node;
Node *head;
Node *tail, *tmp;
Node *idx[1000000];
int killed[1000000];
int N;
void output(){
Node* check ;
check = head ;
while(check!= NULL){
printf("%d -> ",check->data);
check = check->next ;
}
printf("\n");
}
void output_tail(){
Node* check ;
check = tail ;
while(check!= NULL){
printf("%d -> ",check->data);
check = check->prev ;
}
printf("\n");
}
void createlist(int N) {
head = malloc(sizeof(Node));
head->data = 1;
head->next = NULL;
head->prev = NULL;
idx[1] = head;
tail = head;
tmp = head;
for (int i = 2; i <= N; i++) {
tmp = malloc(sizeof(Node));
tmp->data = i;
tmp->next = NULL;
tmp->prev = tail;
tail->next = tmp;
tail = tmp;
idx[i] = tmp;
}
//output();
//output_tail();
}
void kill(int pos) {
if(idx[pos] == NULL) {
printf("Penguin07 QQ\n");
return;
}
Node* k;
Node* prev;
/*if(pos > N/2) {
prev = head ;
while(prev->data != pos && prev->next != NULL){
prev = prev->next ;
}
} else {
prev = tail;
while(prev->data != pos && prev->prev != NULL){
prev = prev->prev ;
}
}*/
prev = idx[pos];
//printf("prev = %d\n", prev->data);
if(prev->data == pos && prev->next != NULL){
k = prev->next;
//printf("kill = %d\n", k->data);
prev->next = k->next ;
if(k->next != NULL)
k->next->prev = prev;
else
tail = prev;
printf("%d\n",k->data);
idx[k->data] = NULL;
free(k);
} else {
puts("Penguin07 QQ");
}
//output();
//output_tail();
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int K;
scanf("%d%d", &N, &K);
createlist(N);
for (int i = 0; i < K; i++) {
int pos;
scanf("%d", &pos);
kill(pos);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment