AtCoder Beginner Contest 181 -F - Silver Woods(几何,并查集)

AtCoder Beginner Contest 181 -F - Silver Woods(几何,并查集)

题面:

题意:

在一个二维平面中,有直线\(y=100,y=-100\)两条直线和\(\mathit n\)个钉子,坐标为\((x_i,y_i)\),现在要使一个圆从平面的最左侧移动到最右侧,要求圆一直在两个直线夹着的区域,且不可以包含钉子(钉子大小可以忽略不记,即可以在圆的边框上),不能跃出直线。问你圆的半径最大是多少?

思路:

我们可以考虑二分答案值\(\mathit r\),然后去判断当前的数值是否满足条件,以此来调整答案的区间。

对于当前的半径值\(\mathit r\),对于任意两点以及点和上下界组成的点对,点线对,若距离为\(dis\),那么圆可以顺利通过当且仅当\(2*r\leq dis\) ,那么我们不妨将所有距离小于\(2*r\)的点对,点线对建立一条边,若上届线和下界线不连通,则表明:通道区域中有一个从左到右连续且距离大于等于\(2*r\)的通道,即圆可以顺利通过。我们以此利用并查集算法来判断二分的mid数值即可。

也可以直接建立全部的边,按照距离升序来排序,一直加边到上届线和下界线连通时,该边为最大圆的直径。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
pii b[maxn];
struct node {
    double val;
    int x, y;
    node()
    {

    }
    node(double _v, int _x, int _y)
    {
        val = _v;
        x = _x;
        y = _y;
    }
    bool operator<(const node &b) const
    {
        return val < b.val;
    }
};
std::vector<node> v;
double dis(pii aa, pii bb)
{
    return sqrt((aa.fi - bb.fi) * (aa.fi - bb.fi) + (aa.se - bb.se) * (aa.se - bb.se));
}
int far[maxn];
int dsu_sz[maxn];
void dsu_init(int n)
{
    repd(i, 0, n) {
        far[i] = i;
        dsu_sz[i] = 1;
    }
}
int findpar(int x)
{
    if (x == far[x]) {
        return x;
    } else {
        return far[x] = findpar(far[x]);
    }
}
void mg(int x, int y)
{
    x = findpar(x);
    y = findpar(y);
    if (x == y) {
        return;
    }
    if (dsu_sz[x] > dsu_sz[y]) {
        dsu_sz[x] += dsu_sz[y];
        far[y] = x;
    } else {
        dsu_sz[y] += dsu_sz[x];
        far[x] = y;
    }
}
int S, T;
void solve()
{
    for (auto e : v) {
        mg(e.x, e.y);
        if (findpar(S) == findpar(T)) {
            printf("%.6f\n", e.val/2 );
            return ;
        }
    }
}
int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    n = readint();
    S = n + 1;
    T = n + 2;
    repd(i, 1, n) {
        b[i].fi = readint() + 100;
        b[i].se = readint();
        v.pb(node(100 - b[i].se, S, i));
        v.pb(node(b[i].se + 100, T, i));
    }
    repd(i, 1, n) {
        repd(j, i + 1, n) {
            v.pb(node(dis(b[i], b[j]), j, i));
        }
    }
    sort(ALL(v));
    dsu_init(n + 2);
    solve();
    return 0;
}



posted @ 2020-11-20 20:30  茄子Min  阅读(156)  评论(0编辑  收藏  举报