题目描述
超时了,怎么办
样例
#include<iostream>
using namespace std;
const int N = 100010;
int q[N];
int n,m;
int lower_bound(int q[],int target){
int l = -1, r = n,mid;
while(l+1<r){
mid = (r+l) >> 1 + l;
if(q[mid]<target){
l = mid;
}else{
r = mid;
}
}
return r;
}
int main(){
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ){
scanf("%d", &q[i]);
}
while(m--){
int target;
cin>>target;
int a = lower_bound(q,target);
if (a>=n || q[a]!=target){
cout<<"-1"<<" "<<"-1"<<endl;
continue;
}
int b = lower_bound(q,target+1)-1;
cout<<a<<" "<<b<<endl;
}
return 0;
}
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla