题解 | 小红关鸡 | 二分 O(n log n)

小红关鸡

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

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;

int n, k;
int x[N];

void solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> x[i];
    }
    sort(x + 1, x + 1 + n);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        int j = upper_bound(x + 1, x + n + 1, x[i] + k) - x;
        ans = max(ans, j - i);
    }
    cout << double(ans) / n << "\n";
}

int main() {
    cout << setprecision(6) << fixed;
    cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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