Advertisement
a53

nth_element

a53
Jan 31st, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <vector>
  4. #define InFile "cioc.in"
  5. #define OutFile "cioc.out"
  6. using namespace std;
  7. vector<int> a;
  8.  
  9. bool myfunction(int i,int j)
  10. {
  11. return (i<j);
  12. }
  13.  
  14. int main()
  15. {
  16. int c,n,k,x;
  17. ifstream f(InFile);
  18. f>>c>>n>>k;
  19. for(int i=0;i<n;++i)
  20. f>>x,a.push_back(x);
  21. f.close();
  22. random_shuffle(a.begin(),a.end());
  23. nth_element(a.begin(),a.begin()+k,a.end(),myfunction);
  24. nth_element(a.begin(),a.begin()+k,a.end());
  25. ofstream g(OutFile);
  26. if(c==1)
  27. {
  28. g<<k;
  29. }
  30. else
  31. {
  32. for(vector<int> ::iterator it=a.begin();it!=a.end();++it)
  33. g<<*it<<' ';
  34. g<< '\n';
  35. }
  36. g.close();
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement