fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n; // size of array b
  6. cin >> n;
  7.  
  8. int b[n]; // array b
  9. for (int i = 0; i < n; i++) {
  10. cin >> b[i];
  11. }
  12.  
  13. int k; // value of k
  14. cin >> k;
  15.  
  16. priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
  17. vector<int> ans;
  18. int i;
  19. for(i = 0;i <= n - k;i++)
  20. q.push({b[i],i});
  21. // while(!q.empty())
  22. // {
  23. // cout << q.top().first << " ";
  24. // q.pop();
  25. // }
  26. for(int j = i;j < n;j++)
  27. {
  28. q.push({b[j],j});
  29. if(q.top().second <= j - k - 1)
  30. {
  31. while(!q.empty() && q.top().second <= j - k - 1)
  32. q.pop();
  33. }
  34. ans.push_back(q.top().first);
  35. q.pop();
  36. }
  37. for(i = 0;i < ans.size();i++)
  38. cout << ans[i] << " ";
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 5300KB
stdin
4
8 1 3 2
3
stdout
1 2