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

#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++;
	return top > bot + 1;
}
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;
}


丛林深处有军事基地,它被n个瞭望塔保护,这些瞭望塔保护它们构成的凸多边形内部,指挥所可以放置在凸包内部任意地点,问敌人至少需要炸毁多少瞭望塔才能使指挥所失去保护?


对于最多需要炸毁多少瞭望塔,可以用二分的思想转换为判定性问题

设炸毁k个瞭望塔,首先k个瞭望塔肯定是凸包上k个连续的点。

对应指挥部的位置,有n种方法炸毁瞭望塔,每种对应一个半平面,而让指挥部完全暴露,相当于这n个半平面的交为空集;

因为原始点集是有许多的,所以在半平面交是不用每次都排序,时间复杂度可以由O(nLogn2)降为O(nlogN) 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值