求调,二分一个都过不了
查看原帖
求调,二分一个都过不了
1562283
Mikumikumi123楼主2025/1/18 19:54
#include<iostream>
using namespace std;
#include<vector>
int main() {
    int m, n;
    cin >> m >> n;
    vector<int>v;
    for (int i = 0;i < m;i++) {
        int data;
        cin >> data;
        v.push_back(data);
    }
    int count = 0;
    for (int i = 0;i < m;i++) {
        int target = v[i] + n;
        int l = i;
        int r = m - 1;
        while (r >= l) {
            int mid = l + (r - l) / 2;
            if (v[mid] >= target) {
                r = mid - 1;
            }
            else {
                l = mid + 1;
            }
        }
        if (r+1!=m&&v[r + 1] == target) {
            count++;
            while (r + 2 != m && v[r + 2] == v[r + 1]) {
                count++;
                r++;
            }
        }
    }
    cout << count;
    return 0;
}
2025/1/18 19:54
加载中...