fork(1) download
  1. #include <iostream>
  2. #include <queue>
  3. #include <list>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. priority_queue<int,vector<int> > pq;
  9. vector <int> p;
  10. p.push_back(3);
  11. p.push_back(3);
  12. p.push_back(3);
  13. p.push_back(3);
  14. p.push_back(5);
  15. p.push_back(5);
  16. p.push_back(5);
  17. p.push_back(5);
  18. p.push_back(1);
  19. p.push_back(8);
  20. p.push_back(8);
  21. p.push_back(8);
  22. p.push_back(8);
  23. p.push_back(8);
  24. for(int i=0;i<p.size();i++)
  25. {
  26. int k= p[i];
  27. if(pq.size() < 5) // top 'm' elements say m = 5 here
  28. {
  29. pq.push(k);
  30. }
  31. else if (pq.top() >= k)
  32. {
  33. pq.pop();
  34. pq.push(k);
  35. }
  36. }
  37. std::list<int> mylist;
  38. while ( !pq.empty() )
  39. {
  40. mylist.push_front(pq.top());
  41. pq.pop();
  42. }
  43. for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
  44. std::cout << ' ' << *it;
  45. cin.get();
  46. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
 1 3 3 3 3