队内差距K组队问题

 

 

二分然后验证

Code

#include<bits/stdc++.h>
using namespace std;
long long n,m;
long long a[500005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    int l=1;
    int g=n/2;
    int r=n/2+1;
    int ans=0;
    while(l<=g&&r<=n)
    {
        if(a[r]-a[l]>=m)
        {
            ++l,++r;
            ans++;
        }
        else ++r;
    }
    cout<<ans<<endl;
} 

 

posted @ 2020-05-30 17:38  ___Charles  阅读(146)  评论(0)    收藏  举报