molakeser @ 2024-08-28 13:46:51
#include <bits/stdc++.h>
using namespace std;
const int N=1e8+5;
#define int long long
int a[N],n,m,x;
bool findd(int l,int r)
{
int mid=(l+r)/2;
if(x==a[l])
{
cout<<l<<" ";
return 1;
}
if(l==r)return 0;
if(x<=a[mid])findd(l,mid);
elsefindd(mid+1,r);
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
{
cin>>x;
bool f=findd(1,n);
if(!f)
cout<<"-1 ";
}
return 0;
}
by wdnmd9028 @ 2024-08-28 13:58:21
1.数组1e6就够了
2.cin改成scanf
by Yxy7952 @ 2024-08-28 13:58:39
@zsfyqch0 \
本蒟蒻只知道你为什么RE
因为代码中findd函数最后必须有个返回值,这样就不会RE了
by Yxy7952 @ 2024-08-28 13:59:27
@wdnmd9028
这样能不RE?
by adsd45666 @ 2024-08-28 14:16:58
由于利用递归实现二分导致返回的值未被存储,将 findd
函数写为void类型,将f开成全局变量,存储每次查询的值,如下,即可AC
bool f;
void findd(int l,int r)
{
int mid=(l+r)/2;
if(x==a[l])
{
cout<<l<<" ";
f=1;
return;
}
if(l==r){
f=0;
return;
}
if(x<=a[mid])findd(l,mid);
else findd(mid+1,r);
}
by adsd45666 @ 2024-08-28 14:17:41
@yixingyou 确实有问题,问题不在这
by adsd45666 @ 2024-08-28 14:18:02
@zsfyqch0
by Yxy7952 @ 2024-08-28 14:19:14
@adsd45666
Ok
by molakeser @ 2024-09-16 17:50:29
明白了谢谢各位大佬@wdnmd9028 @Yxy7952 @adsd45666