题目链接: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;
}
标签:int,ll,mid,CodeForces,Dances,version,Easy,1883G1
From: https://www.cnblogs.com/againss/p/18328507