fork download
  1. #include<iostream>
  2. #include<set>
  3.  
  4. using namespace std;
  5.  
  6. multiset< int > Set;
  7.  
  8. int main()
  9. {
  10. int i,n,target,k,x,numOfElements,mx=-100000000,mn=100000000;
  11. cin>>numOfElements;
  12. for(i=0,n=0,target=1,k=0;i<numOfElements;i++)
  13. {
  14. cin>>x; // input
  15. mn=min(mn,x);
  16. mx=max(mx,x);
  17. Set.insert(x); //inserting in the set
  18. k++;
  19. if(target==k) {
  20. n++; // n is incremented
  21. //Code for finding nth number in the set
  22.  
  23. int l=mn,r=mx;
  24. while(l<=r)
  25. {
  26. int m=(l+r)>>1;
  27. multiset<int>::iterator it=Set.lower_bound(m),itt=Set.upper_bound(m);
  28. int d=distance(Set.begin(),it),D=distance(Set.begin(),itt);
  29. if(d<=n-1 && D>n-1) // it is nth element of multiset
  30. {
  31. cout<<(*it)<<endl;
  32. break;
  33. }
  34. else if(d>n-1) r=m-1;
  35. else l=m+1;
  36. }
  37.  
  38. ///
  39. k=0;
  40. target++; // next target
  41. }
  42. }
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 3480KB
stdin
15
1 3 2 3 2 4 4 4 4 3 5 2 1 12 10
stdout
1
2
2
3
2