LA 2218 Triathlon(半平面交)

Triathlon

【题目链接】Triathlon

【题目类型】半平面交

&题解:

做了2道了,感觉好像套路,都是二分答案,判断半平面交是否为空.
还有刘汝佳的代码总是写const +& 但是我今天试了6次,3次const +& 和3次直接参数传值,发现时间只差了5ms左右,所以我觉得以后不用总是写const +&,因为写的代码长了,但又没有加快多少.

&代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

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 Vector& A, const Vector& 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 ans = 0;
	for(int i = 1; i < n - 1; i++) {
		ans += Cross(p[i] - p[0], p[i + 1] - p[0]);
	}
	return ans / 2;
}

struct Line {
	Point p, 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;
	}
};

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), ans;
	vector<Line> que(n);
	que[first = last = 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++;
		que[++last] = L[i];
		if(fabs(Cross(que[last].v, que[last - 1].v)) < eps) {
			last--;
			//This is que[last] not que[i]
			if(OnLeft(que[last], L[i].p)) que[last] = L[i];
		}
		if(first < last) {
			p[last - 1] = GetLineIntersection(que[last - 1], que[last]);
		}
	}
	while(first < last && !OnLeft(que[first], p[last - 1])) last--;
	if(last - first <= 1) return ans;
	p[last] = GetLineIntersection(que[last], que[first]);
	for(int i = first; i <= last; i++)
		ans.push_back(p[i]);
	return ans;
}

const int maxn = 110;
int V[maxn], U[maxn], W[maxn];

int main() {
	//("E:1.in", "r", stdin);
	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 ok = 1;
			double k = 10000;
			vector<Line> L;
			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[j] && U[i] >= U[j] && W[i] >= W[j]) { 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.push_back(Line(p, v));
				}
			if(ok) {
				// x>0, y>0, x+y<1 ==> -x-y+1>0
				L.push_back(Line(Point(0, 0), Vector(0, -1)));
				L.push_back(Line(Point(0, 0), Vector(1, 0)));
				L.push_back(Line(Point(0, 1), Vector(-1, 1)));
				vector<Point> poly = HalfplaneIntersection(L);
				if(poly.empty()) ok = 0;
			}
			puts(ok ? "Yes" : "No");
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/s1124yy/p/6799966.html