# 半平面求交——点在凸包外

#include <bits/stdc++.h>
using namespace std;
const int maxn = 50010;
const double EPS = 1e-8;
struct Point
{
double x, y;
};
Point p[maxn];
struct Line
{
Point a, b;
};
Line l[maxn], st[maxn];
int n;
double operator *(const Point &x, const Point &y)
{
return x.x * y.y - x.y * y.x;
}
Point operator - (Point x, const Point &y)
{
x.x -= y.x, x.y -= y.y;
return x;
}
Point operator *(const Line &x, const Line &y)
{
double a1 = (y.b - x.a) * (y.a - x.a), a2 = (y.a - x.b) * (y.b - x.b);
Point r;
r.x = (x.a.x * a2 + x.b.x * a1) / (a2 + a1);
r.y = (x.a.y * a2 + x.b.y * a1) / (a2 + a1);
return r;
}
bool operator == (const Point &a, const Point &b)
{
return fabs(a.x - b.x) < EPS && fabs(a.y - b.y) < EPS;
}
bool JudgeOut(const Line &x, const Point &p0)
{
return (p0 - x.a) * (x.b - x.a) > -EPS;
}
bool Parallel(const Line &x, const Line &y)
{
return fabs((x.b - x.a) * (y.b - y.a)) < EPS;
}
bool HplaneIntersection(const int &tem)
{
for (int i = 0; i < n; i++)
l[i].a = p[i], l[i].b = p[i - 1 - tem + (i - 1 - tem < 0 ? n : 0)];
int top = 1, bot = 0;
st[0] = l[0], st[1] = l[1];
for (int i = 2; i < n; i++)
{
if (Parallel(st[top], st[top - 1]) || Parallel(st[bot], st[bot + 1])) return 0;
while (bot < top && JudgeOut(l[i], st[top]*st[top - 1])) top--;
while (bot < top && JudgeOut(l[i], st[bot]*st[bot + 1])) bot++;
st[++top] = l[i];
}
while (bot < top && JudgeOut(st[bot], st[top]*st[top - 1])) top--;
while (bot < top && JudgeOut(st[top], st[bot]*st[bot + 1])) bot++;
}
int main(int argc, char const *argv[])
{
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
int lft = 1, rgt = (n - 1) >> 1, mid;
while (rgt > lft)
{
mid = (lft + rgt) >> 1;
if (!HplaneIntersection(mid)) rgt = mid;
else lft = mid + 1;
}
printf("%d\n", rgt);
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：半平面求交——点在凸包外 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)