AcWing 789. 数的范围
原题链接
简单
作者:
啵啵泡泡
,
2020-09-22 17:25:58
,
所有人可见
,
阅读 2
#include <iostream>
using namespace std;
const int N = 100010;
int n, q, k;
int array[N];
int main() {
scanf("%d%d", &n, &q);
//int array[n];
for (int i = 0; i < n; ++i) {
scanf("%d", &array[i]);
}
while (q--) {
scanf("%d", &k);
int l = 0;
int r = n-1;
while (l < r) {
int mid = (l+r)/2;
if (array[mid] < k) {
l = mid+1;
}
else {
r = mid;
}
}
if (array[l] != k) {
printf("-1 -1\n");
continue;
}
int l1 = l;
int r1 = n-1;
while (l1 < r1) { //需要找到最右边匹配的数,所以这里mid要等于(l1+r1+1)/2
int mid = (l1+r1+1)/2;
if (array[mid] <= k) {
l1 = mid;
}
else {
r1 = mid-1;
}
}
printf("%d %d\n", l, l1);
}
return 0;
}