【CodeForces 1156C】 Match Points 二分答案

You are given a set of points x1, x2, …, xn on the number line.

Two points i and j can be matched with each other if the following conditions hold:

neither i nor j is matched with any other point;
|xi−xj|≥z.
What is the maximum number of pairs of points you can match with each other?

Input
The first line contains two integers n and z (2≤n≤2⋅105, 1≤z≤109) — the number of points and the constraint on the distance between matched points, respectively.

The second line contains n integers x1, x2, …, xn (1≤xi≤109).

Output
Print one integer — the maximum number of pairs of points you can match with each other.

Examples
Input
4 2
1 3 3 7
Output
2
Input
5 5
10 9 5 8 7
Output
1
Note
In the first example, you may match point 1 with point 2 (|3−1|≥2), and point 3 with point 4 (|7−3|≥2).

In the second example, you may match point 1 with point 3 (|5−10|≥5).

题意:给n个元素,问距离大于z两两搭配,能匹配多少对

思路:

二分答案,结果肯定在【0,n/2】之中,那么就外层二分结果,内层check函数判断当前mid是否合法。下面说一下check的策略:
贪心的想,我要凑到mid对,最好的情况肯定是最小的mid个和最大的mid个匹配,这样是肯定能匹配到尽量多的对数的。如果有一对不满足,那整体肯定就不满足。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int a[maxn];
int n, z;

inline bool check(int x)
{
    for(int i=1, j=n-x+1; j<=n;j++,i++)
    if(abs(a[i] - a[j]) < z) return false;
    return true;
}

int main()
{
    n = read(), z = read();
    rep(i,1,n) a[i] = read();
    sort(a+1,a+1+n);
    int L = 0, R = n/2;
    int ans = 0;
    while(L<=R)
    {
        int mid = (L+R)>>1;
        if(check(mid)) L = mid + 1, ans = max(ans,mid);
        else R = mid - 1;
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107710924