UVA 1396 - Most Distant Point from the Sea(二分+半平面交)

217 篇文章 19 订阅
3 篇文章 0 订阅

题目链接:点击打开链接

思路:半平面交模板题, 半平面交算法的函数中, 第一个参数是n个平面的向量, 所以我们只需要二分答案m,然后把每个向量向中心方向平移m长度, 然后求半平面交, 如果交出来的面积为0, 则缩小m, 否则, 增大m。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 100;
int T,n,m;
struct point {
    double x, y;
    point(double x=0, double y=0):x(x), y(y) {}
};
typedef point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const point& a, const point& b) {
    return a.x < b.x || (a.x == b.x && a.y < b.y);
}
int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}
bool operator == (const point& a, const point& b) {
    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
Vector Normal(Vector A) {
    double L = Length(A);
    return Vector(-A.y/L, A.x/L);
}
struct Line {
    point p;
    Vector v;
    double ang;
    Line() {}
    Line(point p, Vector v) : p(p), v(v) { ang = atan2(v.y, v.x); }
    bool operator < (const Line& L) const {
        return ang < L.ang;
    }
};
bool onLeft(Line L, point p) {
    return cross(L.v, p-L.p) > 0;
}
point getintersection(Line a, Line b) {
    Vector u = a.p - b.p;
    double t = cross(b.v, u) / cross(a.v, b.v);
    return a.p + a.v * t;
}
int halfplane(Line* L, int n, point* poly) {
    sort(L, L+n);
    int first, last;
    point *p = new point[n];
    Line *q = new Line[n];
    q[first=last=0] = L[0];
    for(int i = 1; i < n; i++) {
        while(first < last && !onLeft(L[i], p[last-1])) last--;
        while(first < last && !onLeft(L[i], p[first])) first++;
        q[++last] = L[i];
        if(fabs(cross(q[last].v, q[last-1].v)) < eps) {
            last--;
            if(onLeft(q[last], L[i].p)) q[last] = L[i];
        }
        if(first < last) p[last-1] = getintersection(q[last-1], q[last]);
    }
    while(first < last && !onLeft(q[first], p[last-1])) last--;
    if(last - first <= 1) return 0;
    p[last] = getintersection(q[last], q[first]);
    int m = 0;
    for(int i = first; i <= last; i++) poly[m++] = p[i];
    return m;
}
point p[200], poly[200];
Line L[200];
Vector v[200], v2[200];
double x, y;
int main() {
    while(~scanf("%d",&n) && n) {
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf",&x,&y);
            p[i] = point(x, y);
        }
        for(int i = 0; i < n; i++) {
            v[i] = p[(i+1)%n] - p[i];
            v2[i] = Normal(v[i]);
        }
        double l = 0, r = 20000;
        while(r - l > 1e-6) {
            double mid = (r + l) / 2;
            for(int i = 0; i < n; i++) L[i] = Line(p[i]+v2[i]*mid, v[i]);
            int m = halfplane(L, n, poly);
            if(!m) r = mid;
            else l = mid;
        }
        printf("%.6f\n", l);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值