P9633 题解
大意
给定两个数组 Impossible,否则输出
思路
先把
使用二分查找 upper_bound 和 lower_bound 来找到数组
更新
最后判断 Impossible。否则,输出答案
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 5e6 + 10;
int a[N], b[N];
int n, m, t, ans;
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> b[i];
}
b[0] = -INF, b[m + 1] = INF;
sort(a + 1, a + n + 1);
sort(b, b + m + 1);
for (int i = 0; i <= m; i++) {
int pos1 = upper_bound(a + 1, a + n + 1, b[i]) - a;
int pos2 = lower_bound(a + 1, a + n + 1, b[i + 1]) - a - 1;
int len = pos2 - pos1 + 1;
ans = max(ans, len);
}
if (ans == 0) {
cout << "Impossible\n";
} else {
cout << ans << "\n";
}
ans = 0;
}
return 0;
}