天天看點

【LA2218】【POJ1755】 Triathlon(半平面交)

Description

鐵人三項有 3 3 段:遊泳、自行車和賽跑,給出每個選手每個項目的速度vi,uiwivi,uiwi,每個項目的長度不确定,求每個人是否有可能獲得冠軍。

Solution

非常巧妙的一道題。

設總長度為 1 1 ,遊泳長度為xx,自行車長度為 y y ,那麼ii強于 j j 的條件是:

xvi+yui+1−x−ywi<xvj+yuj+1−x−ywjxvi+yui+1−x−ywi<xvj+yuj+1−x−ywj

将其化成 Ax+By+C>0 A x + B y + C > 0 的形式,則:

A=(1vj−1wj)−(1vi−1wi) A = ( 1 v j − 1 w j ) − ( 1 v i − 1 w i )

B=(1uj−1wj)−(1ui−1wi) B = ( 1 u j − 1 w j ) − ( 1 u i − 1 w i )

C=1wj−1wi C = 1 w j − 1 w i

這對應了一個半平面,檢查半平面交即可。

注意還要加上 x>0,y>0,1−x−y>0 x > 0 , y > 0 , 1 − x − y > 0 。

eps

從 10−6 10 − 6 改成 10−11 10 − 11 就A掉了?

Source

/****************************
 * Au: Hany01
 * Prob: [LA2218] [POJ1755] Triathlon
 * Date: Feb 14th, 2018
 * Email: [email protected]
****************************/

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<list>
#include<cfloat>
#include<cassert>
#include<map>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define rep(i , j) for (int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif

inline void File() {
#ifdef hany01 
    freopen("la2218.in" , "r" , stdin);
    freopen("la2218.out" , "w" , stdout);
#endif
}

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b,  : ; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b,  : ; }

inline int read() {
    register char c_; register int _ , __;
    for (_ =  , __ =  , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-')  __ = -;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << ) + (_ << ) + (c_ ^ );
    return _ * __;
}

const int maxn = ;

const double eps = , epd = ;

inline int dcmp(double x) { if (fabs(x) < eps) return ; return x <  ? - : ; }

struct Point
{
    double x, y;
    Point(double x = , double y = ): x(x), y(y) {}
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector 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); }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector v) { return atan2(v.y, v.x); }
struct Line
{
    Point P; Vector v; double ang;
    Line() {}
    Line(Point P, Vector v): P(P), v(v) { ang = Angle(v); }
};
bool operator < (const Line& A, const Line&B) { return A.ang < B.ang; }
inline bool OnLeft(Point P, Line L) { return dcmp(Cross(L.v, P - L.P)) > ; }
Point LineIntersection(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;
}

inline int HalfplaneIntersection(Line* L, int n)
{
    sort(L + , L +  + n);
    int head, tail; Point p[maxn]; Line q[maxn];
    q[head = tail = ] = L[];
    For(i, , n) {
        while (head < tail && !OnLeft(p[tail - ], L[i])) -- tail;
        while (head < tail && !OnLeft(p[head], L[i])) ++ head;
        q[++ tail] = L[i];
        if (!dcmp(Cross(q[tail].v, q[tail - ].v))) {
            -- tail;
            if (OnLeft(L[i].P, q[tail])) q[tail] = L[i];
        }
        if (head < tail) p[tail - ] = LineIntersection(q[tail - ], q[tail]);
    }
    while (head < tail && !OnLeft(p[tail - ], q[head])) -- tail;
    if (head +  >= tail) return ;
    return ;
}

double v[maxn], u[maxn], w[maxn];
int n;
Line L[maxn];

inline bool thorough(int i, int j) { return v[i] >= v[j] && u[i] >= u[j] && w[i] >= w[j]; }

int main()
{
    File();
    while (scanf("%d", &n) != EOF && n) {
        For(i, , n) scanf("%lf%lf%lf", &v[i], &u[i], &w[i]);
        For(i, , n) {
            register int cnt = , mark = ;
            For(j, , n) if (i != j) {
                if (thorough(j, i)) { mark = ; break; }
                if (thorough(i, j)) continue;
                double a = epd / v[j] - epd / w[j] - (epd / v[i] - epd / w[i]), b = epd / u[j] - epd / w[j] - (epd / u[i] - epd / w[i]), c = epd / w[j] - epd / w[i];
                register Point P;
                if (fabs(a) > fabs(b)) P = Point(-c / a, ); else P = Point(, -c / b);
                L[++ cnt] = Line(P, Vector(b, -a));
            }
            if (mark) { //Pay Attention: x > 0 and y > 0 and - x - y + 1 > 0 !!!
                L[++ cnt] = Line(Point(, ), Vector(, -));
                L[++ cnt] = Line(Point(, ), Vector(, ));
                L[++ cnt] = Line(Point(, ), Vector(-, ));
                mark = HalfplaneIntersection(L, cnt);
            }
            if (mark) puts("Yes"); else puts("No");
        }
    }
    return ;
}