题解 | 小红关鸡

小红关鸡

https://www.nowcoder.com/practice/5982b0bfe2fe4ed2a872fd5383271f5f

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;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务