超时了帮帮本蒻只因

P2249 【深基13.例1】查找

Joseph_J @ 2023-07-06 13:25:37

#include<bits/stdc++.h>

using namespace std;
int a[1000001];
int q[100001];
bool t=false;
int main(){

    int n,m;
    cin>>n>>m;

    for(int w=1;w<=n;w++){

        cin>>a[w];
    }

    for(int i=1;i<=m;i++){

        cin>>q[i];
    }

    for(int e=1;e<=m;e++){

        t=false;
        for(int r=1;r<=n;r++){

            if(q[e]==a[r]){

                t=true;
                cout<<r<<' ';
                break; 
            }else{
                t=false;
            }

        }
        if(t==false){   
            cout<<"-1"<<' ';
            }   
    }

    return 0;
} 

by OldDriverTree @ 2023-07-06 13:37:16

@s1mple__navi 你这时间复杂度不对呀


by Joseph_J @ 2023-07-06 13:38:25

@OldDriverTree 求教


by OldDriverTree @ 2023-07-06 13:43:01

@s1mple__navi 你这代码的时间复杂度是 O(nm),明显不对,建议学习二分查找


by Joseph_J @ 2023-07-06 13:50:24

@OldDriverTree 行我用一下,主要是个人感觉有点麻烦。


by OldDriverTree @ 2023-07-06 13:52:33

@s1mple__navi 就几行,不麻烦呀


|