## C. Match Points(二分+贪心)

https://codeforces.com/contest/1156/problem/C

i 和 j 只能用一次，并且满足|a[i]-a[j]| >=z.

4 3

1 4 5 7

AC代码：

``` 1 #include<bits/stdc++.h>
2 using namespace std;
3 # define ll long long
4 const int maxn = 2e5+100;
5 int a[maxn];
6 int n,z;
7 bool check(int pos)
8 {
9     if(n%2!=0&&pos>=(n+1)/2)return false;
10     if(n%2==0&&pos>n/2)return false;
11     for(int i=1; i<=pos; i++)
12     {
13         if(a[n-pos+i]-a[i]>=z)
14             continue;
15         return false;
16     }
17     return true;
18 }
19 int main()
20 {
21   //  int n=3;
22   //  cout<<(n>>1)<<endl;
23     scanf("%d %d",&n,&z);
24     for(int i=1; i<=n; i++)
25     {
26         scanf("%d",&a[i]);
27     }
28     sort(a+1,a+n+1);
29     int l=0,r=(n>>1),ans=0;// 注意右边界，必须保证每个数只能用一次
30     while(l<=r)
31     {
32         int mid=l+r>>1;
33         if(check(mid))
34         {
35             l=mid+1;
36             ans=mid;
37         }
38         else
39             r=mid-1;
40     }
41     printf("%d\n",ans);
42 }```