Use_Imagination @ 2023-07-02 17:37:35
#include<bits/stdc++.h>
using namespace std;
int a[100000];
int finnd(int qWq,int x){
for(int i=0;i<=x;i++){
if(a[i]==qWq)
return i;
}
}
int judge(int k,int qwq){
int mid=1,pos=0,tail=qwq;
while(pos<=tail){
mid=(pos+tail)/2;
if(a[mid]==k) return a[mid];
if(a[mid]<k) pos=mid+1;
if(a[mid]>k) tail=mid-1;
}
return -1;
}
int main(){
int n,m,x;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++){
cin>>x;
if(judge(x,n)==-1){
cout<<-1;
break;
}
cout<<finnd(x,n)<<" ";
}
return 0;
}
by Gyf117 @ 2023-07-02 17:42:28
int mid=1,pos=0,tail=qwq-1;
by YSR330 @ 2023-07-02 17:43:19
6
by Weizhuo_Zhao @ 2023-07-02 18:10:39
@YSR330 不要无效回复
by Weizhuo_Zhao @ 2023-07-02 18:11:50
看我的AC吧
#include <iostream>
using namespace std;
int nums[1000001];
int main() {
int n, m, query;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> nums[i];
for (int i = 0; i < m; i++)
cin >> query;
int result = lower_bound(nums+1, nums+n+1, query)-nums;
cout << ((query == nums[result]) ? result : -1) << " ";
}
return 0;
}
by Weizhuo_Zhao @ 2023-07-02 18:12:30
镐的那么复杂
by lorry26 @ 2023-07-02 18:43:00
#include<bits/stdc++.h>
using namespace std;
int hsy,crz,part,first,size,last,sjx;
int main()
{
cin>>hsy>>crz;
int zzk[hsy+1],lbn[crz+1];
for(int a=1;a<=hsy;a++)
{
cin>>zzk[a];
}
for(int k=1;k<=crz;k++)
{
first=1;
last=hsy;
cin>>sjx;
while(first-last<0)
{
size=last-first;
part=first+size/2;
if(sjx==zzk[part])
{
last=part;
}
else if(zzk[part]<sjx)
{
first=part+1;
}
else
{
last=part-1;
}
}
if(sjx==zzk[last])
{
lbn[k]=last;
}
else
{
lbn[k]=-1;
}
}
for(int m=1;m<=crz;m++)
{
cout<<lbn[m]<<" ";
}
return 0;
}
这不就好了吗?
by Zzzcr @ 2023-07-02 18:49:05
@lorry26 什么阴间马蜂
by lorry26 @ 2023-07-02 18:55:08
额,本人习惯,没办法
by lorry26 @ 2023-07-02 18:59:54
@Zzzcr