CodeForces 1883G1 Dances (Easy version)

薛定谔的AC發表於2024-07-28

題目連結:CodeForces 1883G1【Dances (Easy version)】



思路

為了使得陣列a,b中的每個對應元素滿足a[i] < b[i],所以將陣列a,b按從小到大依次排列,優先刪除陣列a中較大的元素和陣列b中較小的元素,由於刪去的元素個數具有單調性,所以使用二分最佳化,計算最少要刪去幾個元素。


程式碼

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e6 + 10;
const double eps = 1e-8;

ll n, m;
ll a[N], b[N];

bool check(int x) {
  for (int i = 1; i + x <= n; i++) {
    if (a[i] >= b[i + x]) {
      return false;
    }
  }
  return true;
}

void solve() {
  cin >> n >> m;

  a[1] = 1;
  for (int i = 2; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }

  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n);

  ll l = 0, r = n, mid = 0, res = 0;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << res << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}

相關文章