LA 2218 Triathlon(半平面交)

来源:互联网 发布:安卓好用的看书软件 编辑:程序博客网 时间:2024/06/10 07:25

题目传送门


题目大意

  铁人三项比赛分为连续的三段:游泳、自行车和赛跑。现在每个单项比赛的长度还没定,但已知各选手在每项比赛中的平均速度(假定该速度和赛程长度无关), 所以你可以设计每项比赛的长度,让其中某个特定的选手获胜。你的任务是判断有哪些选手可以获得冠军(并列冠军不算)。
  注意,3个单项比赛的长度均不能为0。
  具体数据范围和输入输出格式请参见题目。


题目分析

我们较难发现其实这是一道半平面交题。
设比赛的总长度为1,其中游泳长度为x,自行车长度为y,赛跑就是1-x-y,则选手i打败选手j的充要条件是

xvi+yui+1xywi < xvj+yuj+1xywj

我们可以将此式整理成Ax+By+C>0的形式,这是一条有向直线对应的半平面,其中

A=(1vj1wj)(1vi1wi)
B=(1uj1wj)(1ui1wi)
C=1wj1wi

对于每个选手i,有n-1个半平面(每一个代表一个选手被i所打败),加上x>0,y>0,1-x-y>0的约束后共有n+2个半平面,如果所有半平面交非空则代表有解(半平面交中的任何一点所对应的方案都可使选手i获胜),否则无解。算法时间复杂度O(n2logn),对于n<=100绰绰有余。

本题的坑点在卡精度,所以要特别特别小心精度误差的处理。
Tip1:由于A,B,C都很小,同乘一个10000(根据题目范围)。
Tip2:特殊处理一个选手三个速度同小于等于或同大于另一个选手的情况,减少处理次数,对减少误差有帮助。
Tip3:在将直线的一般式转点向式时,为了处理误差,稍稍判断一下(见code)
Tip4:请遵循上面三个tips,否则WA风险很高。
以下代码将点和向量同用Point结构体存储。


代码

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#define Eps 1e-10#define N 111using namespace std;const double K = 10000;int n, cnt;double v[N], u[N], w[N];struct Point{    double x, y;    Point() {}    Point(double x, double y):x(x), y(y) {}     friend Point operator + (Point A, Point B){return Point(A.x + B.x, A.y + B.y);}    friend Point operator - (Point A, Point B){return Point(A.x - B.x, A.y - B.y);}    friend Point operator * (double A, Point B){return Point(A * B.x, A * B.y);}    friend Point operator / (Point A, double B){return Point(A.x / B, A.y / B);}}p[N];struct Line{    Point P, v;    double ang;    Line() {}    Line(Point P, Point v):P(P), v(v) {ang = atan2(v.y, v.x);}    bool operator < (const Line& L) const{return ang < L.ang;}}L[N], q[N];double Det(Point A, Point B){return A.x * B.y - A.y * B.x;}bool Onleft(Point A, Line B){    return Det(A - B.P, B.v) < -Eps;}Point Cross(Line A, Line B){    Point p1 = A.P, q1 = A.P + A.v;    Point p2 = B.P, q2 = B.P + B.v;    double x = Det(p2 - p1, q2 - p1), y = Det(q2 - q1, p2 - q1);    return (x * q1 + y * p1) / (x + y);}bool HalfplaneI(){    sort(L+1, L+cnt+1);    int head, tail;    q[head = tail = 1] = L[1];    for(int i = 2; i <= cnt; i++){      while(head < tail && !Onleft(p[tail-1], L[i]))  tail --;      while(head < tail && !Onleft(p[head], L[i]))  head ++;      q[++tail] = L[i];      if(fabs(Det(q[tail].v, q[tail-1].v)) < Eps){        tail --;        if(Onleft(L[i].P, q[tail]))  q[tail] = L[i];      }      if(head < tail)  p[tail-1] = Cross(q[tail-1], q[tail]);    }    while(head < tail && !Onleft(p[tail-1], q[head]))  tail --;    p[tail] = Cross(q[tail], q[head]);    return tail - head > 1;}int main(){    while(~ scanf("%d", &n)){      for(int i = 1; i <= n; i++)  scanf("%lf%lf%lf", &v[i], &u[i], &w[i]);      for(int i = 1; i <= n; i++){        bool f = true;        cnt = 0;        for(int j = 1; j <= n; j++){          if(i == j)  continue;          if(v[i] <= v[j] && u[i] <= u[j] && w[i] <= w[j]){f = false;  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 pp = fabs(A) > fabs(B) ? Point(-C/A, 0) : Point(0, -C/B);          L[++cnt] = Line(pp, Point(B, -A));        }        if(!f)  printf("No\n");        else{          L[++cnt] = Line(Point(0, 0), Point(0, -1));          L[++cnt] = Line(Point(0, 0), Point(1, 0));          L[++cnt] = Line(Point(1, 0), Point(-1, 1));          if(HalfplaneI())  printf("Yes\n");          else  printf("No\n");        }      }    }    return 0;} 

这里写图片描述

原创粉丝点击