fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <algorithm>
  4. #include <stdlib.h>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. int main() {
  9. // your code goes here
  10. int n;
  11. cin >> n;
  12. vector<int> A;
  13. for(int i = 0; i < n; i++) {
  14. int a;
  15. cin >> a;
  16. A.push_back(a);
  17. }
  18. int B; cin >> B;
  19.  
  20. multiset<int> q;
  21. for(int i = 0; i < A.size(); i++){
  22. if(q.size() >= B){
  23. auto it = --q.end();
  24. if(A[i] < *it){
  25. q.erase(it, q.end());
  26. q.insert(A[i]);
  27. }
  28. }
  29. else q.insert(A[i]);
  30. for(auto it = q.begin(); it != q.end(); it++) cout << *it << " ";
  31. cout << endl;
  32. }
  33. auto it = --q.end();
  34. cout << *it << endl;
  35. }
Success #stdin #stdout 0s 15240KB
stdin
46 8 16 80 55 32 8 38 40 65 18 15 45 50 38 54 52 23 74 81 42 28 16 66 35 91 36 44 9 85 58 59 49 75 20 87 60 17 11 39 62 20 17 46 26 81 92
9
stdout
8 
8 16 
8 16 80 
8 16 55 80 
8 16 32 55 80 
8 8 16 32 55 80 
8 8 16 32 38 55 80 
8 8 16 32 38 40 55 80 
8 8 16 32 38 40 55 65 80 
8 8 16 18 32 38 40 55 65 
8 8 15 16 18 32 38 40 55 
8 8 15 16 18 32 38 40 45 
8 8 15 16 18 32 38 40 45 
8 8 15 16 18 32 38 38 40 
8 8 15 16 18 32 38 38 40 
8 8 15 16 18 32 38 38 40 
8 8 15 16 18 23 32 38 38 
8 8 15 16 18 23 32 38 38 
8 8 15 16 18 23 32 38 38 
8 8 15 16 18 23 32 38 38 
8 8 15 16 18 23 28 32 38 
8 8 15 16 16 18 23 28 32 
8 8 15 16 16 18 23 28 32 
8 8 15 16 16 18 23 28 32 
8 8 15 16 16 18 23 28 32 
8 8 15 16 16 18 23 28 32 
8 8 15 16 16 18 23 28 32 
8 8 9 15 16 16 18 23 28 
8 8 9 15 16 16 18 23 28 
8 8 9 15 16 16 18 23 28 
8 8 9 15 16 16 18 23 28 
8 8 9 15 16 16 18 23 28 
8 8 9 15 16 16 18 23 28 
8 8 9 15 16 16 18 20 23 
8 8 9 15 16 16 18 20 23 
8 8 9 15 16 16 18 20 23 
8 8 9 15 16 16 17 18 20 
8 8 9 11 15 16 16 17 18 
8 8 9 11 15 16 16 17 18 
8 8 9 11 15 16 16 17 18 
8 8 9 11 15 16 16 17 18 
8 8 9 11 15 16 16 17 17 
8 8 9 11 15 16 16 17 17 
8 8 9 11 15 16 16 17 17 
8 8 9 11 15 16 16 17 17 
8 8 9 11 15 16 16 17 17 
17