InfiniteRobin @ 2024-02-02 15:42:19
#include<bits/stdc++.h>
using namespace std;
int a[10000006];
int s,m,q;
void find(int n,int l,int r){
int mid;
while(l<=r&&l<=s&&r>=1){
mid=(l+r)/2;
if(a[mid]==n){
if(a[mid-1]==n){
while(a[mid]==n&&mid>=1){
mid--;
}
mid++;
}
printf("%d ",mid);
return;
}
if(a[mid]<n){
l=mid+1;
}
else if(a[mid]>n){
r=mid-1;
}
else{
break;
}
}
printf("%d ",-1);
return;
}
int main(){
scanf("%d%d",&s,&m);
for(int i=1;i<=s;i++){
scanf("%d",&a[i]);
}
while(m--){
scanf("%d",&q);
find(q,1,s);
}
return 0;
}
不知 #6 为何 TLE??
by sunny2014 @ 2024-02-10 13:39:00
using namespace std; int n,m,q,a[1000005]; int fin(int x){ int l=1,r=n; while (l<r){ int mid=l+(r-l)/2; if (a[mid]>=x) r=mid; else l=mid+1; } if (a[l]==x) return l; return -1; } int main(){ cin>>n>>m; for (int i=1 ; i<=n ; i++){ cin>>a[i]; } for (int i=1 ; i<=m ; i++){ cin>>q; cout<<fin(q)<<" "; } return 0; }```c
by sunny2014 @ 2024-02-10 13:40:25
using namespace std; int n,m,q,a[1000005]; int fin(int x){ int l=1,r=n; while (l<r){ int mid=l+(r-l)/2; if (a[mid]>=x) r=mid; else l=mid+1; } if (a[l]==x) return l; return -1; } int main(){ cin>>n>>m; for (int i=1 ; i<=n ; i++){ cin>>a[i]; } for (int i=1 ; i<=m ; i++){ cin>>q; cout<<fin(q)<<" "; } return 0; }```cpp 这是我写的,可以参考一下
by InfiniteRobin @ 2024-02-11 11:33:25
这个LaTeX
....