Author: vrintle
Editorial
It is obvious that there are $$$n^2$$$ possible points available i.e., all $$$(x_i, y_j)$$$ pairs. And, we can observe that with the increase in radius, the number of points will either increase or remain the same, so the function (number of points inside) is monotonous with the radius. Hence, we can do a binary search on the radius.
Now, we just have to calculate the amount of points inside the circle for a given radius. Let's observe the function: $$$x^2 + y^2 = r^2$$$. In this, as $$$x$$$ increases, $$$y$$$ decreases. So, if we sort the arrays by squared values, then we can use two pointers to calculate the count, which takes linear time.
Hence the complexity: $$$\mathcal{O}(n \cdot \log{R})$$$
Solution
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
#define ld long double
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
const int N = 1e6 + 7;
int x[N], y[N];
void solve() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) {
cin >> x[i];
}
for(int i = 0; i < n; i++) {
cin >> y[i];
}
sort(x, x + n, [&](auto& x, auto& y) {
return abs(x) < abs(y);
});
sort(y, y + n, [&](auto& x, auto& y) {
return abs(x) < abs(y);
});
ld low = 0, high = 2e9, eps = 1e-9;
auto check = [&](ld r) {
int j = n - 1, ans = 0;
for(int i = 0; i < n; i++) {
while(j >= 0 && r * r - x[i] * x[i] - y[j] * y[j] < eps * eps) {
j--;
}
ans += j + 1;
}
return ans >= k;
};
while(high - low > eps) {
ld mid = (high + low) / 2;
if(check(mid)) high = mid;
else low = mid;
}
ld ans = check(low) ? low : high;
cout << fixed << setprecision(9) << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef VRINTLE
freopen("./tests/test_0.txt", "r", stdin);
freopen("./out.txt", "w", stdout);
freopen("./err.txt", "w", stderr);
#endif
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
return 0;
}




