fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n,k;
  6. cin>>n>>k;
  7. int ar[n];
  8. for(int i=0;i<n;i++){
  9. cin>>ar[i];
  10. }
  11. deque<int> s;
  12. // 3 5 1 4 2 6 3 5 4
  13. // 0 1 2 3 4 5 6 7 8
  14. // 5 7 8
  15. for(int i=0;i<n;i++){
  16.  
  17. while(!s.empty() && ar[s.back()]<=ar[i]){
  18. s.pop_back();
  19. }
  20.  
  21. s.push_back(i);
  22. if(s.front()==i-k){
  23. s.pop_front();
  24. }
  25. if(i>=k-1){
  26. cout<<ar[s.front()]<<endl;
  27. }
  28.  
  29.  
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 5420KB
stdin
9 4
3 5 1 4 2 6 3 5 4
stdout
5
5
6
6
6
6