CF744D 题解

· · 题解

首先观察性质。答案可以二分,并且最优解的圆上一定有蓝点。

但是如果只保留所有解中圆上有蓝点的解那么可行解在圆的半径上不一定是连续的一个前缀。这是因为圆的半径比较小的时候可能只碰到红点。

假设可行解的半径的范围为 [l_1,r_1],[l_2,r_2],...,[l_k,r_k],那么我们考虑先求出 \max_{i=1}^k(l_i) 再进行二分。

容易发现对于一个半径为 l_i 的解,它包含的红点一定在圆上,若在圆内则一定能调整成一个在原来的圆内部的一个更小的圆作为解。

所以我们就可以枚举在圆上的红点,找到一个圆使得红点在圆上并且没有任何一个蓝点在圆内,可行的半径是前缀,可以二分。对于判定考虑把一个解看作一个角度,即圆心相对于红点的角度,每个蓝点会禁用一段角度,排序处理即可。

接着枚举蓝点进行同样的操作,不同的蓝点禁用区间,且解必须在某一个红点区间内。

上述做法复杂度为 \Theta(n^2\log n\log w),w 为答案值域,常数极大,无法通过。

两个二分都可以抽象成这样的问题,总共有 n 个值需要二分,要求最大值。最大值不一定需要求出所有值,所以可以考虑优化二分方式。

对于按顺序二分的方法首先可以想到把二分下界设为已经得到的最大值,并且在二分这个点之前提前 check 一次下界,这样是更优的,真正进入二分的点的数量是单调栈的操作次数。

例如值为 \{1,4,2,3,5\} 的变量只有值为 \{1,4,5\} 进入了二分。

如果随机二分变量的顺序,那么每次进入二分都能期望排除一半的变量,期望 \log 次进入二分之后就没有变量能进入二分了。

也就是说有 n 个变量进行了第一步的 check,有期望 \log n 个变量进行了二分。

复杂度降到 \Theta(n\log n\log w+n^2\log n)。

本题卡精度,请注意实现。

#include <bits/stdc++.h>
#define endl '\n'
#define ld long double
using namespace std;
const int N = 1005;
const ld _eps = 1e-9, eps = 1e-9, pi = acos(-1);
int n, m, p[N];
ld ans;
struct point{
    ld x, y;
    point() : x(0), y(0) {}
    point(ld x, ld y) : x(x), y(y) {}
    friend point operator + (const point &a, const point &b){
        return point(a.x + b.x, a.y + b.y);
    }
    friend point operator - (const point &a, const point &b){
        return point(a.x - b.x, a.y - b.y);
    }
    friend ld operator * (const point &a, const point &b){
        return a.x * b.y - a.y * b.x;
    }
    friend point operator * (const point &a, const ld &k){
        return point(a.x * k, a.y * k);
    }
    friend point operator / (const point &a, const ld &k){
        return point(a.x / k, a.y / k);
    }
    ld angle() const{
        return atan2(y, x);
    }
    ld abs() const{
        return sqrt(x * x + y * y);
    }
}red[N], blue[N];
point turn(point p, ld _cos, ld _sin){
    return point(p.x * _cos - p.y * _sin, p.x * _sin + p.y * _cos);
}
ld mns(ld x, ld y){
    return x - y < 0 ? x - y + 2 * pi : x - y;
}
vector<pair<ld, ld> > calc(point p, vector<point> vec, ld len){
    vector<pair<ld, int> > tmp;
    vector<pair<ld, ld> > res;
    int cnt = 0;
    for (point p2 : vec){
        ld a = (p2 - p).abs() / 2, c = len;
        if (a >= c)
            continue;
        ld b = sqrt(max(c * c - a * a, (ld)0));
        ld a1 = turn(p2 - p, a, -b).angle(), a2 = turn(p2 - p, a, b).angle();
        a1 += _eps;
        if (a1 > pi) a1 -= 2 * pi;
        a2 -= _eps;
        if (a2 < -pi) a2 += 2 * pi;
        tmp.emplace_back(a1, 1);
        tmp.emplace_back(a2, -1);
        if (a1 > a2)
            cnt++;
    }
    if (cnt){
        tmp.emplace_back(-pi, cnt);
        tmp.emplace_back(pi, -cnt);
    }
    sort(tmp.begin(), tmp.end());
    cnt = 0;
    for (int i = 0; i < (int)tmp.size() - 1; i++){
        cnt += tmp[i].second;
        ld l = tmp[i].first, r = tmp[i + 1].first;
        if (r - l > eps && cnt)
            res.emplace_back(l, r);
    }
    return res;
}
bool check1(int j, vector<point> &vec, ld mid){
    vector<pair<ld, ld> > res = calc(red[j], vec, mid);
    if (res.empty())
        return 1;
    for (int k = 0; k < (int)res.size(); k++)
        if (mns(res[(k + 1) % ((int)res.size())].first, res[k].second) > eps)
            return 1;
    return 0;
}
bool check2(vector<point> &vec1, vector<point> &vec2, int j, ld mid){
    vector<pair<ld, ld> > res1 = calc(blue[j], vec1, mid), res2 = calc(blue[j], vec2, mid);
    if (res1.empty())
        res1.emplace_back(-pi, pi);
    else{
        vector<pair<ld, ld> > tmp = res1;
        res1.clear();
        if (tmp[0].first + pi > eps)
            res1.emplace_back(-pi, tmp[0].first);
        for (int i = 0; i < (int)tmp.size() - 1; i++)
            if (tmp[i + 1].first - tmp[i].second > eps)
                res1.emplace_back(tmp[i].second, tmp[i + 1].first);
        if (pi - tmp.back().second > eps)
            res1.emplace_back(tmp.back().second, pi);
    }
    vector<pair<ld, int> > vec;
    for (pair<ld, ld> p : res1){
        vec.emplace_back(p.first, 1);
        vec.emplace_back(p.second, -1);
    }
    for (pair<ld, ld> p : res2){
        vec.emplace_back(p.first, 1);
        vec.emplace_back(p.second, -1);
    }
    sort(vec.begin(), vec.end());
    int cnt = 0;
    ld sum = 0;
    for (int i = 0; i < (int)vec.size() - 1; i++){
        cnt += vec[i].second;
        if (cnt > 0)
            sum += vec[i + 1].first - vec[i].first;
    }
    return sum + _eps * 10 < 2 * pi;
}
int main(){
    srand(time(0));
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> red[i].x >> red[i].y;
    for (int i = 1; i <= m; i++)
        cin >> blue[i].x >> blue[i].y;
    ld dw = 0;
    {
        for (int i = 1; i <= n; i++)
            p[i] = i;
        random_shuffle(p + 1, p + 1 + n);
        vector<point> vec;
        for (int i = 1; i <= m; i++)
            vec.push_back(blue[i]);
        for (int i = 1; i <= n; i++){
            int j = p[i];
            ld l = dw, r = 1e9, mid;
            if (!check1(j, vec, dw))
                continue;
            while (r - l > eps){
                mid = (l + r) / 2;
                if (check1(j, vec, mid)) dw = l = mid;
                else r = mid;
            }
        }
    }
    if (dw > 1e9 - 1){
        cout << -1;
        return 0;
    }
    {
        for (int i = 1; i <= m; i++)
            p[i] = i;
        random_shuffle(p + 1, p + 1 + m);
        vector<point> vec1, vec2;
        for (int i = 1; i <= n; i++)
            vec1.push_back(red[i]);
        for (int i = 1; i <= m; i++){
            vec2.clear();
            int j = p[i];
            for (int k = 1; k <= m; k++)
                if (k != j)
                    vec2.push_back(blue[k]);
            ld l = dw, r = 1e9, mid;
            if (!check2(vec1, vec2, j, dw))
                continue;
            while (r - l > eps){
                mid = (l + r) / 2;
                if (check2(vec1, vec2, j, mid)) dw = l = mid;
                else r = mid;
            }
        }
    }
    cout << fixed << setprecision(8) << dw;
    return 0;
}