求大佬解答一下

P2249 【深基13.例1】查找

Besokus @ 2024-11-22 15:46:14

求大佬解答一下,为什么RE,用了二分法时间复杂度没这么大啊

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdio> 
#include<algorithm>

using namespace std;

struct Num {
    long id, data;
};

void erfen(struct Num a[], int size, long target) {
    int l = 0, r = size, mid = (l + r) / 2;
    int index = 0;
    while (l <= r) {
        mid = (l + r) / 2;
        if (target < a[mid].data) {
            r = mid - 1;
        }
        else if (target > a[mid].data) {
            l = mid + 1;
        }
        else 
            if (mid == 0) {
                printf("%d ", a[mid].id);
                return;
            }
            else {//查看前面有没有和mid相同的,找到第一个
                for (int j = mid; j > 0; j--) {
                    if (a[j].data == a[mid].data && j != 0) {
                        continue;
                    }

                    else {
                        printf("%d ", a[j + 1].id);
                        return;
                    }
                }
            }
        }

    printf("-1 ");
}

int main() {
    int n, m, x;
    Num s[100005] = {0};
    scanf("%d %d", &n, &m);
    //vector<Num>a;
    vector<long long>b;
    int i;
    for (i = 0; i < n; i++) {
        scanf("%d", &x);
        s[i].data = x;
        s[i].id = i + 1;
        //a.push_back(s[i]);
    }
    for (int j = 0; j < m; j++) {
        scanf("%d", &x);
        b.push_back(x);
    }

    for (int k = 0; k < b.size(); k++) {
        erfen(s, i, b[k]);
    }

    return 0;
}

by Mr_Terminator @ 2024-11-22 15:48:06

@Besokus 仔细看看这题 n 最大到多少以及你数组开了多少


by yzm0325 @ 2024-11-22 15:48:22

@Besokus 《RE是超时》


by yzm0325 @ 2024-11-22 15:48:55

@Besokus 《1e6=100000》


by yzm0325 @ 2024-11-22 15:50:04

建议重学如何看评测结果以及小学乘法。


by xuruize666 @ 2024-12-01 13:18:53

R U OK?


|