这很难评

P2249 【深基13.例1】查找

guoen23 @ 2024-06-22 16:17:33

#include<bits/stdc++.h>
using namespace std;
int a,b,f,g = 0;
long long c[1000005],d[100005] = {'0'};
bool e = false;
int main(){
    cin>>a>>b;
    for(int i = 0;i<a;i++){
        cin>>c[i];
    }
    for(int i = 0;i<b;i++){
        cin>>d[i];
        for(int j = 0;j <a;j++){
            if(c[j] == d[i]){
                f = j + 1;
                e = true;
                break;
            }
        }
        if(e == false){
            cout<<-1;
        }
        else{
            cout<<f<<" ";
            e = false;
        }
    }
}

本人写了代码,样例过了,提交全是TLE和WA(虽然我知道这是暴力算法)[求解(ToT)/~~~]


by xQWQx @ 2024-06-22 16:39:28

@guoen23 用二分


by dtw35l @ 2024-06-26 15:36:42

@guoen23

第21行

cout<<-1;

改成

cout<<-1<<" ";

每次输出都应该后缀空格

解决TLE要二分


by guoen23 @ 2024-06-29 15:45:24

@dtw35l 好的,就AC两个[/(ㄒoㄒ)/~~]


by Enmin @ 2024-07-05 20:32:07

@guoen23 二分


|