fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct cmp{
  5. bool operator()(pair<int,int> &p1, pair<int,int> &p2){
  6. return p1.first>p2.first;
  7. }
  8. };
  9.  
  10. int main(){
  11.  
  12. int n;cin>>n;
  13. int arr[n];
  14. for(int i=0;i<n;i++)
  15. cin>>arr[i];
  16.  
  17. int k;cin>>k;
  18.  
  19. priority_queue<pair<int,int>,vector<pair<int,int>>, cmp> pq;
  20. int i=0,j=0,res=0;
  21. while(j<n){
  22. pq.push({arr[j],j});
  23. if(j-i == k){
  24. //do something
  25. while(pq.top().second <= i)
  26. pq.pop();
  27. i++;
  28. }
  29. if(pq.size() >= k)
  30. res = max(pq.top().first,res);
  31. j++;
  32. }
  33. cout<<res<<endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 4456KB
stdin
3
8 2 4
2
stdout
2