zzy0618 @ 2023-02-10 22:09:56
用随机数取中间值
AC记录
#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=rand()%(r-l+1)+l;
if (a[mid]>=x)r=mid;
else l=mid+1;
}
if(a[l]==x) return l;
else return -1;
}
int main(){
srand((int)time(0));
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++){
scanf("%d",&q);
int ans=find(q);
printf("%d ",ans);
}
return 0;
}
by Killer_joke @ 2023-02-10 22:11:02
您的写法期望复杂度还是
by TernaryTree @ 2023-02-10 22:14:41
这个有意思啊,这个复杂度怎么证
by E1_de5truct0r @ 2023-02-10 22:34:54
期望是 log 复杂度。
by ABookCD @ 2023-02-10 22:35:39
@Killer_joke 怎么证明啊qwq
by ACRUSHj @ 2023-02-10 22:45:11
假了,会死循环
by Usada_Pekora @ 2023-02-10 22:46:38
by Hisaishi_Kanade @ 2023-02-10 22:48:19
你区间长度缩短
那么显然你每一次区间缩短长度期望是
总体就是期望
感觉这个过程大概对的,假了轻喷。
by Killer_joke @ 2023-02-10 22:56:07
@ArizonaYYDS 显然弱于随机选中位数的快排的最大递归深度(
by Killer_joke @ 2023-02-10 22:56:34
@Killer_joke 而且每个数不同
by zzy0618 @ 2023-02-10 23:04:13
@Killer_joke 总之整活就对了