fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct node
  4. {
  5. long int n;
  6. struct node *l,*r;
  7. }node;
  8. typedef node * tree;
  9. tree maknod(long int d)
  10. {
  11. tree t;
  12. t=(tree)malloc(sizeof(tree));
  13. t->n=d;
  14. t->l=t->r=NULL;
  15. return t;
  16. }
  17.  
  18. tree ins(tree t,long int d)
  19. {
  20. if(t==NULL)
  21. return maknod(d);
  22. else if(t->n > d)
  23. t->l=ins(t->l,d);
  24. else
  25. t->r=ins(t->r,d);
  26. return t;
  27. }
  28. long int findmax(tree t)
  29. {
  30. if(t->r==NULL)
  31. return (t->n);
  32. findmax(t->r);
  33. }
  34. tree del(tree t,long int d)
  35. {
  36. if(t!=NULL)
  37. {
  38. if(t->n==d)
  39. {
  40. if(t->l==NULL && t->r==NULL)
  41. return NULL;
  42. if(t->l==NULL)
  43. return t->r;
  44. if(t->r==NULL)
  45. return t->l;
  46. t->n=findmax(t->l);
  47. t->l=del(t->l,t->n);
  48. }
  49. if(t->n>d)
  50. t->l=del(t->l,d);
  51. if(t->n<d)
  52. t->r=del(t->r,d);
  53. }
  54. return t;
  55. }
  56. long int print(tree t)
  57. {
  58. if(t->r==NULL)
  59. return t->n;
  60. print(t->r);
  61. }
  62. int main()
  63. {
  64. long int num,k,i;
  65. tree t=NULL;
  66. scanf("%ld ",&num);
  67. long int a[num];
  68. for(i=0;i<num;i++)
  69. scanf("%ld",&a[i]);
  70. scanf("%ld",&k);
  71. for(i=0;i<k;i++)
  72. {
  73. t=ins(t,a[i]);
  74. }
  75. for(i=0;i<=num-k;i++)
  76. {
  77. if(i<num-k)
  78. {
  79. printf("%ld",print(t));
  80. printf(" ");
  81. }
  82. else
  83. printf("%ld",print(t));
  84. t=del(t,a[i]);
  85. ins(t,a[i+k]);
  86. }
  87. return 0;
  88. }
Runtime error #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
Standard output is empty