电科校赛
计算几何 + 两次二分
eps为1e-10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MOD ((int)(1e9) + 7)
#define eps 1e-10
using namespace std;
typedef long long ll;
int dcmp(double x) {
if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}
double sqr(double x) {
return x*x;
}
struct Point {
double x, y;
Point(double x=0, double y=0):x(x),y(y) { }
void print() {
printf("(%.3lf, %.3lf)", x, y);
}
};
Point operator + (Point A, Point B) { return Point(A.x+B.x, A.y+B.y); }
Point operator - (Point A, Point B) { return Point(A.x-B.x, A.y-B.y); }
Point operator * (Point A, double p) { return Point(A.x*p, A.y*p); }
Point operator / (Point A, double p) { return Point(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);
}
bool operator == (const Point& a, const Point &b) {
return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0;
}
double Dot(Point A, Point B) { return A.x*B.x + A.y*B.y; }
double Length(Point A) { return sqrt(Dot(A, A)); }
double Cross(Point A, Point B) { return A.x*B.y - A.y*B.x; }
struct Line {
Point p; // 直线上任意一点
Point v; // 方向向量
double ang; // 极角,即从x正半轴旋转到向量v所需要的角(弧度)
Line() {}
Line(Point P, Point v): p(P),v(v){ ang = atan2(v.y, v.x); }
Point point(double a) {
return p+(v*a);
}
bool operator < (const Line& L) const {
return ang < L.ang;
}
void print() {
p.print(), cout << " "; point(1).print(); cout << endl;
}
};
Point p[25], a, b;
Line l[25];
int N, cnt;
struct Circle
{
Point c;
double r;
Circle(){}
Circle(Point c, double r):c(c), r(r){}
Point point(double a) //根据圆心角求点坐标
{
return Point(c.x+cos(a)*r, c.y+sin(a)*r);
}
};
bool InCircle(Point x, Circle c) { return dcmp(c.r - Length(c.c - x)) >= 0;}
bool judge(Point l, Point r, Circle cl, Circle cr) {
Point mid;
while( dcmp(Length(l-r)) > 0 ) {
mid = (l + r) * 0.5;
if (InCircle(mid, cl) && InCircle(mid, cr)) return true;
if (InCircle(mid, cl)) l = mid;
else r = mid;
}
return false;
}
bool check(double M) {
Circle ca(a,M), cb(b,M);
for (int i = 0; i < cnt; ++ i) {
if ( (InCircle(l[i].p,ca) && InCircle(l[i].point(1),ca)) || (InCircle(l[i].p,cb) && InCircle(l[i].point(1),cb)) ) continue;
if ( InCircle(l[i].p,ca) && InCircle(l[i].point(1),cb) ) {
if (judge(l[i].p, l[i].point(1), ca, cb)) continue;
}
if ( InCircle(l[i].p,cb) && InCircle(l[i].point(1),ca) ) {
if (judge(l[i].p, l[i].point(1), cb, ca)) continue;
}
return false;
}
return true;
}
int main() {
while (scanf("%d", &N) != EOF) {
for (int i = 0; i < N; ++ i) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
scanf("%lf%lf", &a.x, &a.y);
scanf("%lf%lf", &b.x, &b.y);
l[0].p = p[N-1]; l[0].v = p[0] - p[N-1];
cnt = 1;
for (int i = 0; i < N-1; ++ i) {
l[cnt].p = p[i], l[cnt].v = p[i+1] - p[i];
cnt ++;
}
double R = 0.0;
for (int i = 0; i < N; ++ i) {
R = max(R, Length(p[i]-a));
}
double L = 0.0, M;
while (R - L > eps) {
M = (L + R) * 0.5;
if (!check(M)) L = M;
else R = M;
}
printf("%.3f\n", M);
}
}