题目描述
include[HTML_REMOVED]
using namespace std;
vector[HTML_REMOVED]arr;
int n, m;
int funl(int x)
{
int l = 0;
int r = n -1;
while(l < r)
{
int mid = (l + r) / 2;
if(arr[mid] >= x) r = mid;
else l = mid + 1;
}
if(arr[l] == x) return l;
else return -1;
}
int funx(int x)
{
int l = 0, r = n - 1;
while(l < r)
{
int mid = (l + r + 1) / 2;
if(arr[mid] <= x) l = mid;
else r = mid - 1;
}
if(arr[l] == x) return l;
else return -1;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++)
{
int x;
cin >> x;
arr.push_back(x);
}
while(m –)
{
int x;
cin >> x;
int l = funl(x);
int r = funx(x);
cout << l << ‘ ‘ << r << endl;
}
return 0;
}
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla