uvalive 4992 半平面交

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5E4 + 10;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) { }
};

typedef Point Vector;

Vector operator + (const Vector& A, const Vector& B)
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B)
{
	return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Vector& A, double p)
{
	return Vector(A.x * p, A.y * p);
}
double Dot(const Vector& A, const Vector& B)
{
	return A.x * B.x + A.y * B.y;
}
double Cross(const Vector& A, const Vector& B)
{
	return A.x * B.y - A.y * B.x;
}
double Length(const Vector& A)
{
	return sqrt(Dot(A, A));
}
Vector Normal(const Vector& A)
{
	double L = Length(A);
	return Vector(-A.y / L, A.x / L);
}

double PolygonArea(vector<Point> p)
{
	int n = p.size();
	double area = 0;
	for (int i = 1; i < n - 1; i++)
		area += Cross(p[i] - p[0], p[i + 1] - p[0]);
	return area / 2;
}

// 有向直线。它的左边就是对应的半平面
struct Line
{
	Point P;    // 直线上任意一点
	Vector v;   // 方向向量
	double ang; // 极角,即从x正半轴旋转到向量v所需要的角(弧度)
	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;
	}
};

// 点p在有向直线L的左边(线上不算)
bool OnLeft(const Line& L, const Point& p)
{
	return Cross(L.v, p - L.P) > 0;
}

// 二直线交点,假定交点惟一存在
Point GetLineIntersection(const Line& a, const 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;
}

const double eps = 1e-6;

// 半平面交主过程
vector<Point> HalfplaneIntersection(vector<Line>& L)
{
	int n = L.size();
	sort(L.begin(), L.end()); // 按极角排序

	int first, last;         // 双端队列的第一个元素和最后一个元素的下标
	vector<Point> p(n);      // p[i]为q[i]和q[i+1]的交点
	vector<Line> q(n);       // 双端队列
	vector<Point> ans;       // 结果

	q[first = last = 0] = L[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] = GetLineIntersection(q[last - 1], q[last]);
	}
	while (first < last && !OnLeft(q[first], p[last - 1])) last--; // 删除无用平面
	if (last - first <= 1) return ans; // 空集
	p[last] = GetLineIntersection(q[last], q[first]); // 计算首尾两个半平面的交点

	// 从deque复制到输出中
	for (int i = first; i <= last; i++) ans.push_back(p[i]);
	return ans;
}
int n, x, y;
Point P[maxn];
bool check(int m)
{
	vector<Line>l;
	for (int i = 0; i < n; i++)
		l.push_back(Line(P[(i + m + 1) % n], P[i] - P[(i + m + 1) % n]));
	return HalfplaneIntersection(l).empty();
}
int main(int argc, char const *argv[])
{
	while (~scanf("%d", &n) && n)
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d", &x, &y);
			P[i] = Point(x, y);
		}
		if (n == 3) printf("1\n");
		else
		{
			int L = 1, R = n - 3, M;
			while (L < R)
			{
				M = L + (R - L) / 2;
				if (check(M)) R = M;
				else L = M + 1;
			}
			printf("%d\n", L);
		}
	}
	return 0;
}



这货需要nlogn的半平面交,具体解析来自于http://blog.csdn.net/acm_cxlove/article/details/7915167

首先需要明白的是一个问题,对于摧毁一定数量的塔防,怎样的方案是使得剩下的保护范围最小。
结论是摧毁连续多个顶点,这样是最优的,可以尝试证明一下。
对于5个顶点的多边形,删除两个顶点,可以尝试连续两个顶点,以及间隔一个顶点。
由于原多边形是凸边形,所以还是比较容易得到连续顶点最优,同理可得其它情况。
题目要求的是使对方尽可能多的摧毁至少需要摧毁的塔防,联系复杂度等等问题
二分答案,然后判断是否存在一个区域,保证能受保护。
对于每一次二分,枚举删除连续的顶点,形成新的边界,通过半平面交判断是否存在可行区域。
注意:边界上的点是不受保护的,所以只需要判断多边形的核的面积即可。
           当剩余的点在2个以及以下是,是肯定可行的。避免处理麻烦。
再看一看题目的范围,5W个顶点,半平面交至少肯定是要用nlgn的算法,其实听了晴天哥哥的说明,才知道,这题
二分+半平面交的nlgnlgn的算法都要卡掉,顿时吓尿了,难道有o(n)的半平面交算法???
多亏晴天哥哥的指导,其实zzy的半平面交算法是将所有向量按极角排序之后,维护了一个双端队列,排序部分达到nlgn的复杂度,其实后面只需要o(n)。然后再看这题,原先给的凸多形是有序的,而之后我们的连线的极角也是循环有序的,线性扫描一遍,找到最小的极角,便可以依次得到有序的向量,O(n)的线性sort,晴天哥哥太神了。
具体细节就要看每个人的习惯了,我将原来的顺序调整为逆序,半平面交的算法是针对向量的左侧,而极角是顺时针有序。

你可能感兴趣的:(uvalive 4992 半平面交)