fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, x;
  6. cin >> n;
  7.  
  8. vector<int> nums(n);
  9. for(int i = 0; i < n; i++) cin >> nums[i];
  10.  
  11. cin >> x;
  12.  
  13. int index = -1;
  14.  
  15. for(int l = 0, r = n - 1; l <= r; ) {
  16. int mid = (l + r) / 2;
  17. if(nums[mid] > x) {
  18. index = mid;
  19. r = mid - 1;
  20. } else {
  21. l = mid + 1;
  22. }
  23. }
  24.  
  25. if(index != -1)
  26. cout << index << " " << nums[index] << endl;
  27. else
  28. cout << endl;
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5320KB
stdin
5
10
20
30
40
50 
stdout