题目描述
include[HTML_REMOVED]
using namespace std;
const int N = 100010;
int n,q;
int arr[N];
//第一次判断 找第一次出现下标
bool isBlue1(int num,int x){
if(num < x) return true;
else return false;
}
int binary_search1(int arr[],int len,int x){
int l = -1,r = len;
while((l+1) < r){
int mid = (l+r)/2;
if(isBlue1(arr[mid],x)){
l = mid;
}
else {
r = mid;
}
}
if(arr[r] == x ) return r;
else return -1;
}
//第二次出现的下标
bool isBlue2(int num,int x){
if(num <= x) return true;
else return false;
}
int binary_search2(int arr[],int len,int x ){
int l = -1,r = len;
while((l+1) < r){
int mid = (l+r)/2;
if(isBlue2(arr[mid],x)){
l = mid;
}
else {
r = mid;
}
}
if(arr[l] == x) return l;
else return -1;
}
int main(){
cin>>n>>q;
for(int i=0;i[HTML_REMOVED]>arr[i];
}
while(q--){
int x;cin>>x;
int res1 = binary_search1(arr,n,x);
int res2 = binary_search2(arr,n,x);
cout<<res1<<" "<<res2<<endl;
}
return 0;
}
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla