uvalive 2218 半平面交

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 10;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
Point poly[maxn];
typedef Point Vector;
typedef vector<Point> Polygon;
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;
	}
};
Vector operator +(Vector A, Vector B)//
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point 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);
}
double Cross(Vector A, Vector B)//
{
	return A.x * B.y - A.y * B.x;
}
bool OnLeft(Line L, Point p)
{
	return Cross(L.v, p - L.P) > 0;
}
const double eps = 1e-10;
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;
}
Point GetIntersection(Line a, Line b)
{
	Vector u = a.P - b.P;
	double t = Cross(b.v, u);
	return a.P + a.v * t;
}
double Dot(Vector A, Vector B)//
{
	return A.x * B.x + A.y * B.y;
}
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;
}
vector<Line> L;
int V[maxn], U[maxn], W[maxn];
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 main(int argc, char const *argv[])
{
	int n;
	while (scanf("%d", &n) == 1 && n)
	{
		for (int i = 0; i < n; i++)
			scanf("%d%d%d", &V[i], &U[i], &W[i]);
		for (int i = 0; i < n; i++)
		{
			int lc = 0, ok = 1;
			double k = 10000;
			for (int j = 0; j < n; j++) if (i != j)
				{
					if (V[i] <= V[j] && U[i] <= U[j] && W[i] <= W[j]) {ok = 0; break;}
					if (V[i] >= V[i] && U[i] >= U[j] && W[i] >= W[i]) continue;
					double a = (k / V[j] / -k / W[j]) - (k / V[i] - k / W[i]);
					double b = (k / U[j] - k / W[j]) - (k / U[i] - k / W[i]);
					double c = k / W[j] - k / W[i];
					Point P;
					Vector v(b, -a);
					if (fabs(a) > fabs(b)) P = Point(-c / a, 0);
					else P = Point(0, -c / b);
					L[lc++] = Line(P, v);
				}
			if (ok)
			{
				L[lc++] = Line(Point(0, 0), Vector(0, -1));
				L[lc++] = Line(Point(0, 0), Vector(1, 0));
				L[lc++] = Line(Point(0, 1), Vector(-1, 1));
				vector<Point>p = HalfplaneIntersection(L);
				if (p.empty()) ok = 0;
			}
			if (ok) printf("Yes\n"); else printf("No\n");
		}
	}
	return 0;
}



书上有代码提示:



一场运动比赛有三个阶段,给出了每个选手在每个阶段的平均速度,可以自行设计每场比赛的长度,让某个选手获胜,判断每个选手是否有可能获胜。 


将题目转换为解不等式,Ax+By+C的形式,A=(1/vj-1/wj)-(1/vi-1/wi) B = (1/uj-1/wj)-(1/ui-1/wi) C = (1/wj-1/wi)


就可以用半平面交来解决,对于每个i都有其他n-1个j的半平面和x>0 y>0 (1-x-y)>0这三个半平面,如果这些半平面都有交集,说明i有可能打败其他选手,否则不可能。

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