Modern Cpp
二分写法
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n, k;
std::cin >> n >> k;
std::vector<int> x(n);
for(int i = 0; i < n; i++){
std::cin >> x[i];
}
std::sort(x.begin(), x.end());
int max = 1;
for(int i = 0; i < n; i++){
int cnt = std::upper_bound(x.begin(), x.end(), x[i] + k) - x.begin() - i;
max = std::max(max, cnt);
}
std::cout << std::fixed << std::setprecision(10) << 1. * max / n << "\n";
return 0;
}

京公网安备 11010502036488号