uvalive 2218
2015-07-29 16:34
417 查看
题意:一场运动比赛有三个阶段,给出了每个选手在每个阶段的平均速度,可以自行设计每场比赛的长度,让某个选手获胜,判断每个选手是否有可能获胜。
题解:把题意转化为一个不等式,设比赛长度是1,如果i要战胜j,x、y分别是第一阶段和第二阶段的比赛长度:
(x / ui + y / vi + (1-x-y) / wi) < (x / uj + y / vj + (1-x-y) / wj)
可以转化为Ax + By + C > 0的形式,也就可以用半平面交来解决,对于每个i都有其他n-1个j的半平面和x>0 y>0 (1-x-y)>0这三个半平面,如果这些半平面都有交集,说明i有可能打败其他选手,否则不可能。
题解:把题意转化为一个不等式,设比赛长度是1,如果i要战胜j,x、y分别是第一阶段和第二阶段的比赛长度:
(x / ui + y / vi + (1-x-y) / wi) < (x / uj + y / vj + (1-x-y) / wj)
可以转化为Ax + By + C > 0的形式,也就可以用半平面交来解决,对于每个i都有其他n-1个j的半平面和x>0 y>0 (1-x-y)>0这三个半平面,如果这些半平面都有交集,说明i有可能打败其他选手,否则不可能。
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const double PI = acos(-1); const int N = 105; const double INF = 1e9; const double eps = 1e-7; struct Point { double x, y; Point(double x = 0, double y = 0): x(x), y(y) {} }poly ; int X , Y , Z ; typedef Point Vector; 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; } }L ; int n; double Sqr(double x) { return x * x; } Point operator + (Point A, Point B) { return Point(A.x + B.x, A.y + B.y); } Point operator - (Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } Point operator * (Point A, double p) { return Point(A.x * p, A.y * p); } Point operator / (Point A, double p) { return Point(A.x / p, A.y / p); } //计算点积的正负 负值夹角为钝角 int dcmp(double x) { if (fabs(x) < 1e-9) return 0; return x < 0 ? -1 : 1; } bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator == (const Point& a, const Point& b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } //计算点积 double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } //计算叉积,也就是数量积 double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } //计算向量长度 double Length(Point A) { return sqrt(Dot(A, A)); } Vector Normal(Vector A) { double L = Length(A); return Vector(-A.y / L, A.x / L); } //向量A旋转rad弧度,rad负值为顺时针旋转 Vector Rotate(Vector A, double rad) { return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } //角度转化弧度 double torad(double deg) { return deg / 180.0 * PI; } //点p在有向直线L的左边(线上不算) bool OnLeft(Line L, Point P) { return Cross(L.v, P - L.p) > 0; } //求两直线的交点,前提交点一定存在 Point GetIntersection(Line a, 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; } //求半面交 int HalfplaneIntersection(Line* L, int n, Point* poly) { sort(L, L + n); int first = 0, rear = 0; Point *p = new Point ; Line *q = new Line ; q[first] = L[0]; for (int i = 1; i < n; i++) { while (first < rear && !OnLeft(L[i], p[rear - 1])) rear--; while (first < rear && !OnLeft(L[i], p[first])) first++; q[++rear] = L[i]; if (fabs(Cross(q[rear].v, q[rear - 1].v)) < eps) { rear--; if (OnLeft(q[rear], L[i].p)) q[rear] = L[i]; } if (first < rear) p[rear - 1] = GetIntersection(q[rear - 1], q[rear]); } while (first < rear && !OnLeft(q[first], p[rear - 1])) rear--; if (rear - first <= 1) return 0; p[rear] = GetIntersection(q[rear], q[first]); int m = 0; for (int i = first; i <= rear; i++) poly[m++] = p[i]; return m; } int main() { while (scanf("%d", &n) == 1) { for (int i = 0; i < n; i++) scanf("%d%d%d", &X[i], &Y[i], &Z[i]); double temp = 10000; for (int i = 0; i < n; i++) { int cnt = 0, flag = 1; for (int j = 0; j < n; j++) if (i != j) { if (X[i] <= X[j] && Y[i] <= Y[j] && Z[i] <= Z[j]) { flag = 0; break; } if (X[i] >= X[j] && Y[i] >= Y[j] && Z[i] >= Z[j]) continue; double A = (temp / Y[j] - temp / Z[j]) - (temp / Y[i] - temp / Z[i]); double B = (temp / X[j] - temp / Z[j]) - (temp / X[i] - temp / Z[i]); double C = temp / Z[j] - temp / Z[i]; L[cnt++] = Line(Point(0, -C / B), Vector(B, -A)); } if (flag) { //x > 0 y > 0 -x-y+1 > 0 L[cnt++] = Line(Point(0, 0), Vector(0, -1)); L[cnt++] = Line(Point(0, 0), Vector(1, 0)); L[cnt++] = Line(Point(0, 1), Vector(-1, 1)); if (!HalfplaneIntersection(L, cnt, poly)) flag = 0; } if (flag) printf("Yes\n"); else printf("No\n"); } } return 0; }
相关文章推荐
- bzoj1923: [Sdoi2010]外星千足虫
- LeetCode(120) Triangle
- autoit小贴士
- 判断实体的类型 相关操作
- Buffer中的flip()
- Delete Node in a Linked List
- javascript 继承
- Animator 动画器——参数解释
- c源文件宏展开
- 使用移位操作符实现无符号整数位反转
- ORACLE 毫秒变换为日期 日期转换毫秒
- 关键词抽取简介
- LeetCode_230 Kth Smallest Element in a BST
- Java final关键字
- snnu1120: 划分数(DP计数问题)
- POJ 1201 Intervals 差分约束
- [LeetCode] Set Matrix Zeroes
- c++ bitset类用法
- Java基础——内部类
- 线段树入门