fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX 1000
  4.  
  5. int deque[MAX];
  6.  
  7. int main() {
  8. int n, k;
  9. scanf("%d", &n);
  10.  
  11. if (n <= 0) return 0;
  12.  
  13. int arr[MAX];
  14. for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  15.  
  16. scanf("%d", &k);
  17.  
  18. if (k <= 0 || k > n) return 0;
  19.  
  20. int front = 0, rear = -1;
  21.  
  22. for (int i = 0; i < n; i++) {
  23.  
  24. // Remove out-of-window indices
  25. if (front <= rear && deque[front] <= i - k)
  26. front++;
  27.  
  28. // Remove smaller elements
  29. while (front <= rear && arr[deque[rear]] <= arr[i])
  30. rear--;
  31.  
  32. deque[++rear] = i;
  33.  
  34. if (i >= k - 1)
  35. printf("%d ", arr[deque[front]]);
  36. }
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 5320KB
stdin
9
1 3 -1 -3 5 3 6 7 2
3
stdout
3 3 5 5 6 7 7