AcWing 4483. 格斗场

image.png :::info i在前,j在后的例题 ::: 思路:
排序 + 双指针

  1. static int N = 200010;
  2. static int[] a = new int[N];
  3. static int n;
  4. static void solve() {
  5. n = ni();
  6. int k = ni();
  7. for (int i = 0; i < n; i++)
  8. a[i] = ni();
  9. Arrays.sort(a, 0, n);
  10. int cnt = n;
  11. for (int i = 0, j = 0; i < n; i++) {
  12. while (j < n && a[j] - a[i] <= k)
  13. j++;
  14. if (a[j - 1] > a[i])
  15. cnt--;
  16. }
  17. out.println(cnt);
  18. }