uvalive 2218

来源:互联网 发布:软件职业学校招生 编辑:程序博客网 时间:2024/05/16 08:48

题意:一场运动比赛有三个阶段,给出了每个选手在每个阶段的平均速度,可以自行设计每场比赛的长度,让某个选手获胜,判断每个选手是否有可能获胜。
题解:把题意转化为一个不等式,设比赛长度是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[N];int X[N], Y[N], Z[N];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[N];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[n];    Line *q = new Line[n];    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;}
0 0
原创粉丝点击