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

7 篇文章 0 订阅
6 篇文章 0 订阅

Description

铁人三项有 3 3 段:游泳、自行车和赛跑,给出每个选手每个项目的速度vi,uiwi,每个项目的长度不确定,求每个人是否有可能获得冠军。

Solution

非常巧妙的一道题。
设总长度为 1 1 ,游泳长度为x,自行车长度为 y y ,那么i强于 j j 的条件是:

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

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

A=(1vj1wj)(1vi1wi) A = ( 1 v j − 1 w j ) − ( 1 v i − 1 w i )

B=(1uj1wj)(1ui1wi) B = ( 1 u j − 1 w j ) − ( 1 u i − 1 w i )

C=1wj1wi C = 1 w j − 1 w i

这对应了一个半平面,检查半平面交即可。
注意还要加上 x>0,y>0,1xy>0 x > 0 , y > 0 , 1 − x − y > 0

eps 106 10 − 6 改成 1011 10 − 11 就A掉了?

Source

/****************************
 * Au: Hany01
 * Prob: [LA2218] [POJ1755] Triathlon
 * Date: Feb 14th, 2018
 * Email: hany01@foxmail.com
****************************/

#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, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

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

const int maxn = 200;

const double eps = 1e-11, epd = 10000.;

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

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0): 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)) > 0; }
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 + 1, L + 1 + n);
    int head, tail; Point p[maxn]; Line q[maxn];
    q[head = tail = 1] = L[1];
    For(i, 2, n) {
        while (head < tail && !OnLeft(p[tail - 1], L[i])) -- tail;
        while (head < tail && !OnLeft(p[head], L[i])) ++ head;
        q[++ tail] = L[i];
        if (!dcmp(Cross(q[tail].v, q[tail - 1].v))) {
            -- tail;
            if (OnLeft(L[i].P, q[tail])) q[tail] = L[i];
        }
        if (head < tail) p[tail - 1] = LineIntersection(q[tail - 1], q[tail]);
    }
    while (head < tail && !OnLeft(p[tail - 1], q[head])) -- tail;
    if (head + 1 >= tail) return 0;
    return 1;
}

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, 1, n) scanf("%lf%lf%lf", &v[i], &u[i], &w[i]);
        For(i, 1, n) {
            register int cnt = 0, mark = 1;
            For(j, 1, n) if (i != j) {
                if (thorough(j, i)) { mark = 0; 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, 0); else P = Point(0, -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(0, 0), Vector(0, -1));
                L[++ cnt] = Line(Point(0, 0), Vector(1, 0));
                L[++ cnt] = Line(Point(0, 1), Vector(-1, 1));
                mark = HalfplaneIntersection(L, cnt);
            }
            if (mark) puts("Yes"); else puts("No");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值