牛客题解 主持人的烦恼

链接:https://ac.nowcoder.com/acm/problem/13591
来源:牛客网

题解

作者 岛田小雅

排序输出题,把所有人的颜值从小到大排序,然后从左到右遍历。如果能一起上台就把两个人一起挑出去,不行就把小的丢出去,大的留下来跟后面一个人比较颜值。

复杂度 \(O(n)\)(不知道对不对)

AC 代码

作者 岛田小雅
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+2;
int n, m;
int arr[N];

int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    while(cin >> n >> m)
    {
        for(int i = 1; i <= n; i++) cin >> arr[i];
        sort(arr+1,arr+1+n);
        int ans = 0;
        for(int i = 1; i < n;)
        {
            if(arr[i+1]-arr[i] >= m) i++;
            else
            {
                ans++;
                i += 2;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}
posted @ 2022-09-19 10:02  岛田小雅  阅读(26)  评论(1编辑  收藏  举报