提交时间:2024-01-26 20:06:56
运行 ID: 38357
#include <bits/stdc++.h> using namespace std; int cnt = 0; int bserach(int nums[], int i, int n, int target) { int left = 0, right = n - 1; while (left <= right) { int mid = ((left + right)) / 2; if (nums[mid] - nums[i] > target) { right = mid - 1; } else if (nums[mid] - nums[i] < target) { left = mid + 1; } else { cnt++; int y = mid; while (true) { if (nums[mid - 1] == nums[mid]) { mid--; cnt++; } else break; } while (true) { if (nums[y + 1] == nums[y]) { cnt++; y++; } else break; } return mid; } } return -1; } int main() { int n, c; cin >> n >> c; int num[n]; for (int i = 0; i < n; i++) { cin >> num[i]; } for (int i = 0; i < n - 1; i++) { bserach(num, i, n, c) ; } cout << cnt; return 0; }