1 solutions
-
8
#include<bits/stdc++.h> using namespace std; const int N=1e5+10; int a[N]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } int m; cin>>m; while(m--) { int x;//寻找x第一次出现的位置 cin>>x; int l=1,r=n;//在l到r这个范围内查找 while(l<r) { int mid=(l+r)/2; if(a[mid]>=x) r=mid; else l=mid+1; } if(a[l]!=x) { cout<<-1<<" "; } else { cout<<l<<" "; } } return 0; }
- 1
Information
- ID
- 138
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 3
- Tags
- (None)
- # Submissions
- 127
- Accepted
- 41
- Uploaded By