求助大佬QAQ,能过样例,但洛谷MLE和RE了

P2249 【深基13.例1】查找

jiunichenyin @ 2024-03-08 16:34:06

#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#define maxn 1000010
using namespace std;
long long a[maxn],in[maxn];
int mid(long long a[], int n, long long k) {
    if (a[n / 2] == k) {
        int ans = n / 2;
        while (1) {
            if (a[ans - 1] != k) {
                break;
            }
            else {
                ans--;
            }
        }
        return ans;
    }
    else if (a[n / 2] < k) {
        if (a[n / 2 + 1] > k) {
            return -1;
        }
        mid(a, n + n / 2, k);
    }
    else if (a[n / 2] > k) {
        if (a[n / 2 - 1] < k) {
            return -1;
        }
        mid(a, n - n / 2, k);
    }
}
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < m; ++i) {
        cin >> in[i];
    }
    for (int i = 0; i < m; ++i) {
        if (mid(a, n, in[i]) != -1) {
            cout << mid(a, n, in[i]) << " ";
        }
        else {
            cout << "-1" << " ";
        }
    }
    return 0;
}

|