解法:
cpp
#include<iostream>
#include<vector>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> vec(n);
for (int& x : vec) cin >> x;
int x;
cin >> x;
int l = 0, r = n-1, cnt = 0;
while (l <= r) {
cnt++;
int mid = l + (r - l) / 2;
if (vec[mid] > x)
r = mid - 1;
else if (vec[mid] < x)
l = mid + 1;
else {
cout << mid << endl;
cout << cnt;
return;
}
}
cout << -1 << endl;
cout << cnt;
}
int main() {
solve();
return 0;
}