为啥RE 蒟蒻求调 互关

P2249 【深基13.例1】查找

_Christina @ 2024-08-24 15:55:15

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N], q[N],mid;
int n, m; //数字个数和询问次数
int bin_search(int x) {
    int l = 1, r = n;
    while (l < r) {
        mid = l + (r - l) / 2;
        if (x > a[mid]) {
            l = mid + 1;
        } else {
            r = mid;
        }
    }
    if (a[l] == x) {
        return l;
    } else {
        return -1;
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int j = 1; j <= m; j++) {
        cin >> q[j];
        cout << bin_search(q[j]) << " ";
    }
    return 0;
}

by DGFLSzfd @ 2024-08-24 15:57:43

数组定小了!!!


by DGFLSzfd @ 2024-08-24 15:57:54

@_Christina


by szrgjxms @ 2024-08-24 15:58:08

数组开小了


by DGFLSzfd @ 2024-08-24 15:58:16

const int N = 1e6+10;

改成这样!!!!


by szrgjxms @ 2024-08-24 15:58:54

N ≤ 10^6


by _Christina @ 2024-08-24 16:02:02

可是改过之后就编译失败了!


by _Christina @ 2024-08-24 16:03:54

现在对了,谢谢大家!!!


|