fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. int bs(vector<int> &b, int x, int low, int high){
  6. int ans = -1;
  7. while(low <= high){
  8. int mid = low + (high-low) / 2;
  9.  
  10. if(b[mid] <= x){
  11. low = mid + 1;
  12. }
  13. else{
  14. ans = mid;
  15. high = mid - 1;
  16. }
  17. }
  18. return ans;
  19. }
  20.  
  21. int main() {
  22. // your code goes here
  23. int n;
  24. cin>>n;
  25.  
  26. vector<int> b(n);
  27. for(int i = 0; i < n; i++){
  28. cin>>b[i];
  29. }
  30.  
  31. int x;
  32. cin>>x;
  33. int low = 0, high = n-1;
  34. int index = bs(b,x,low,high);
  35. cout<<index<<endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 5320KB
stdin
7
3 5 5 8 8 10 12
6
stdout
3