这样第一个测试集怎么会报WA呢

P2249 【深基13.例1】查找

SoupEgg @ 2024-03-03 20:59:38

#include<iostream>
#include<stdlib.h>
using namespace std;

const int MAX_N = 1000001;
const int MAX_M = 100001;

long long int a[MAX_N];
int b[MAX_M];
int c[MAX_M];

int binarySearchLastOccurrence(int key, int low, int high) {
    int result = -1;

    while(low <= high){
        int mid = (low+high)/2;
        if(a[mid] == key && ((a[mid] != a[mid-1] && mid != 1) || mid == 1)){
            result = mid;
            break;
        }
        if(a[mid] >= key){
            high = mid-1;
        }else{
            low = mid+1;
        }
    }

    return result;
}

int main() {
    int n, m;
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    for (int j = 1; j <= m; j++) {
        cin >> b[j];
        c[j] = binarySearchLastOccurrence(b[j], 0, n - 1);
    }

    for (int j = 1; j <= m; j++) {
        cout << c[j] << " ";
    }

    return 0;
}

by lieyang25 @ 2024-03-04 09:50:54

或许是左区间从1开始呢?


by hedong22 @ 2024-03-08 21:00:06

把a[0]设个负数就好了


|