Link to the question
Actually, its same RMID spoj question but Nishant Raj has already posted it. The Time taken by the code mentioned before is nearly 5 sec. But mine taking 0.14 sec . The only concept used over here is Doubly Linked List with few modification.
Actually, its same RMID spoj question but Nishant Raj has already posted it. The Time taken by the code mentioned before is nearly 5 sec. But mine taking 0.14 sec . The only concept used over here is Doubly Linked List with few modification.
#include<stdio.h> #include<stdlib.h> struct node { int val; struct node *left,*right; }*start=NULL,*med=NULL,*temp; int main() { int i,j,k,n,count=0; while(scanf("%i",&n)!=EOF) { if(n>0) { ++count; temp = (struct node *)malloc(sizeof(temp)); temp->val = n; temp->left = temp->right = NULL; if(med==NULL) { start = med = temp; } else { start->right = temp; temp->left = start; start = temp; if(count&1) { med = med->right; } } } else if(n==-1) { temp = med; printf("%d\n",med->val); if(count&1) med = med->left; else med = med->right; count--; if(temp->left != NULL) temp->left->right = temp->right; if(temp->right != NULL) temp->right->left = temp->left; free(temp); } else { printf("\n"); start = med = NULL; count = 0; fflush(stdin); } } return 0; }
No comments:
Post a Comment
Your comment is valuable to us