wangruize88 @ 2023-08-04 15:19:51
#include <iostream>
using namespace std ;
long long a [10000001] ;
int main () {
long long n , m , x ;
cin >> n >> m ;
for ( int i = 1 ; i <= n ; i ++ ) cin >> a[i] ;
int f , b = 0 , c ;
for ( int i = 1 ; i <= m ; i ++ ) {
cin >> x ;
f = n / 2 , c = 0 ;
while ( ! ( a[f-1] != x && a[f] == x ) ) {
if ( a[f] > x ) f -= f / 2 ;
else if ( a[f] < x ) f += f / 2 ;
else f -- ;
if ( c > n / 2 ) {
cout << -1 << ' ' ;
b = 1 ;
break ;
}
c ++ ;
}
if ( b != 1 ) cout << f << ' ' ;
b = 0 ;
}
return 0 ;
}
by _LPxiao_ @ 2023-08-04 15:33:37
@wangruize88
#include<bits/stdc++.h>
using namespace std;
int n,m,q,a[1000005];
int find(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;
else 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;
int ans=find(q);
cout<<ans<<" ";
}
return 0;
}
by A_chicken_boy @ 2023-08-04 15:38:41
@wangruize88 太复杂了
by wangruize88 @ 2023-08-05 14:46:02
@wwangzzillin 谢谢
by ypylcs @ 2023-08-13 15:50:52
long long 数组不能开这么大