6 条题解
-
1
万能二分板子,找左右边界即可 PS:记得注意下标嗷
#include <bits/stdc++.h> #define endl '\n' #define int long long #define INF 0x3f3f3f3f3f const int N = 100010; using namespace std; int arr[N]; int n,q,tmp; int bs_left(int arr[],int n,int x){ int l = 0; int r = n+1; while(l+1<r){ int mid = (l+r)/2; if(arr[mid]<x){ l = mid; }else{ r = mid; } } if(arr[r] == x){ return r-1; }else{ return -1; } } int bs_right(int arr[],int n,int x){ int l = 0; int r = n+1; while(l+1<r){ int mid = (l+r)/2; if(arr[mid]<=x){ l = mid; }else{ r = mid; } } if(arr[l] == x){ return l-1; }else{ return -1; } } signed main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>q; for(int i = 1; i<= n;i++){ cin>>arr[i]; } while(q--){ cin>>tmp; int ans_left = bs_left(arr,n,tmp); int ans_right = bs_right(arr,n,tmp); cout<<ans_left<<" "<<ans_right<<endl; } return 0; }
信息
- ID
- 88
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 656
- 已通过
- 150
- 上传者