求调!

P2249 【深基13.例1】查找

midsummer_zyl @ 2023-07-27 19:44:02

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int a[N], b[N];
int main() {
    int n, m, i, j;
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (i = 1; i <= m; i++)
        scanf("%d", &b[i]);
    for (i = 1; i <= m; i++) {
        for (j = 1; j <= n; j++) {
            if(a[i] == b[j]) {
                printf("%d ", j);
                break;
            }
        }
        if(j == n)
            printf("-1 ");
    }
    return 0;
}

大佬纠纠错!


by xuzihao123 @ 2023-07-27 19:46:00

@midsummer_zyl

会超时的,要用二分查找


by zcs_kim @ 2023-07-27 19:47:54

@midsummer_zyl O(nm)太大了,会T,用二分


by midsummer_zyl @ 2023-07-27 19:51:25

OK,懂了


by Farland @ 2023-07-27 19:52:30

@midsummer_zyl

还有一个错误:

  1. 您定义的 N 至少要开在 10^6+1 以上,因为 1\le n\le 10^6

|