样例能过,全WA求助

P2249 【深基13.例1】查找

liang121147 @ 2023-12-13 13:46:24

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int num[1000010] = {};
    int check[1000010] = {};
    int m, n;
    int out[1000010] = {};
int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c>'9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
int main() {

    memset(out, -1, sizeof(out));
    n = read(), m = read();
    for (int i = 1; i <= n; i++) {
        num[i] = read();
    }
    for (int i = 1; i <= m; i++) {
        check[i] = read();
    }
    int temp = m;
    while (m--) {
        int low = 1;
        int high = n;
        while (low-high <0) {
            int mid = (high - low) / 2 + low;
            if (num[mid] > check[m]) {
                high = mid-1;
            }
            if (num[mid] < check[m]) {
                low = mid+1;
            }
            if (num[mid] == check[m]) {
                out[m] = mid;
                high = mid;
            }
        }
    }
    for (int i = 1; i <= temp; i++) { cout << out[i] << ' '; }
    cout << endl;
    system("pause");
    return 0;
}

|