#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>usingnamespace std;constint N =100010;int n, m;int q[N];intmain(){scanf("%d%d",&n,&m);for(int i =0; i < n; i ++)scanf("%d",&q[i]);for(int i =0; i < m; i ++){int x;scanf("%d",&x);// 二分x的左端点int l =0, r = n -1;// 确定区间范围while(l < r){int mid = l + r >>1;if(q[mid]>= x) r = mid;else l = mid +1;}if(q[r]== x){
cout << r <<' ';// 二分x的右端点
r = n -1;// 右端点一定在[左端点, n - 1] 之间while(l < r){int mid = l + r +1>>1;// 因为写的是l = mid,所以需要补上1if(q[mid]<= x) l = mid;else r = mid -1;}
cout << r << endl;}else cout <<"-1 -1"<< endl;}return0;}
二、AcWing 730 机器人的跳跃问题
1. 问题描述
2. 问题解决
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>usingnamespace std;constint N =100010;int n;int h[N];boolcheck(int e){for(int i =1; i <= n; i ++){
e = e *2- h[i];if(e >=1e5)returntrue;if(e <0)returnfalse;}returntrue;}intmain(){scanf("%d",&n);for(int i =1; i <= n; i ++)scanf("%d",&h[i]);int l =0, r =1e5;while(l < r){int mid = l + r >>1;if(check(mid)) r = mid;else l = mid +1;}printf("%d\n", r);return0;}