二分了,但是最后TLE,,c++

P2249 【深基13.例1】查找

damuzhi @ 2023-11-15 20:37:16

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n,m;
    cin >> n >> m;
    int b[n];
    for(int i = 0; i < n; i++){
        cin >> b[i];
    }

    int l;

        int left = 0,right = n - 1;
        int mid = (left + right) / 2;
        while (b[mid] != l) {
            if (b[mid] > l) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
            if (right < left) {
                break;
            }
            mid = (left + right) / 2;
        }
        while(b[mid] == b[mid - 1] && left <= right && mid - 1 >= 0){
            mid--;

        }
        if (left <= right && b[mid] == l) {
            cout << mid + 1;
        } else {
            cout << -1;
        }
        cout << " ";
    }

    return 0;
}

by heyx0201 @ 2023-11-15 20:53:43

@damuzhi STL lower_bound


|