fork download
  1. #include<stdio.h>
  2. #include<cmath>
  3. #include<iostream>
  4. #include<vector>
  5. using namespace std;
  6.  
  7.  
  8.  
  9. int BinarySearch(vector<int>& A, int l, int r, int key)
  10. {
  11. int m ;
  12. if(A[r-1] < key )
  13. return r;
  14. if(A[0] > key)
  15. return 0;
  16.  
  17. while( r > l )
  18. {
  19. m = (r+l)/2;
  20.  
  21. if( A[m] >= key && A[m-1] < key )
  22. return m;
  23. else if(A[m] > key)
  24. r = m;
  25. else
  26. l = m;
  27. cout << l << " "<< m <<" " << r << " " << r -l << endl ;
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. int t,i , np, nl, c, x, y, j , k;
  34. vector<int> points;
  35. freopen("in.txt", "rt", stdin);
  36. scanf("%d",&t);
  37. points.reserve(t);
  38. for(i=0; i<t; i++){
  39. scanf("%d",&points[i]);
  40. }
  41. scanf("%d %d",&x, &y);
  42. k = BinarySearch(points, 0, t, x);
  43. cout << k << endl;
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 2.71s 3144KB
stdin
Standard input is empty
stdout
134521420