#6超空间求助

P2249 【深基13.例1】查找

hyh0926 @ 2024-01-23 09:41:44

#include<bits/stdc++.h>
using namespace std;
int n,m,a,b[1000000001];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a;
        if(b[a]==0)
        {
            b[a]=i;
        }
    }
    for(int i=1;i<=m;i++)
    {
        int t;
        cin>>t;
        if(b[t]==0)
        {
            cout<<-1<<" ";
            continue;
        }
        cout<<b[t]<<" ";
    }
    return 0;
}

by Addicted_Game @ 2024-01-23 09:44:18

这是二分题,不是桶排序


by ___nyLittleT___ @ 2024-01-23 09:44:47

数据那么大开数组肯定会爆


by chen_kun @ 2024-01-23 09:45:51

@hyh0926 b数组开1e6+10就够了 还有这题不能暴力要用二分算法不然会超时


by chen_kun @ 2024-01-23 09:46:26

#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000010],num;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=m;i++){
        cin>>num;
        if(lower_bound(a+1,a+n+1,num)==upper_bound(a+1,a+n+1,num)) cout<<"-1 ";
        else cout<<lower_bound(a+1,a+n+1,num)-a<<" ";
    }
    return 0;
}

@hyh0926


by ___nyLittleT___ @ 2024-01-23 09:52:56

#include<bits/stdc++.h>
using namespace std;
int n,m,x;
int a[1000005];
map<int,bool>vis;
int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        vis[a[i]]=1;
    }
    while(m--){
        scanf("%d",&x);
        if(!vis[x]){
            printf("-1 ");
            continue;
        }
        int y=lower_bound(a+1,a+1+n,x)-a;
        printf("%d ",y);
    }
    return 0;
}

@hyh0926


by hyh0926 @ 2024-01-23 09:54:32

Thank,此贴结。


|