POJ - 1775 Triathlon(半平面交 + 线性规划)

链接 Triathlon

题意

n n n 个选手参加三个比赛,给出每个选手三个比赛的速度,裁判可以任意调整每个比赛的路程,问能不能让选手 i i i 胜出,可以则输出 Y e s Yes Yes

思路

设每段路程为 x , y , z x,y,z x,y,z ,那么每个选手比赛花费的时间 T = x u i + y v i + z w i T = \frac{x}{u_i} + \frac{y}{v_i} +\frac{z}{w_i} T=uix+viy+wiz

那么如果要想然 i i i 选手胜出,则

T i − T j = x u i + y v i + z w i − ( x u j + y v j + z w j ) < 0 T_i - T_j = \frac{x}{u_i} + \frac{y}{v_i} +\frac{z}{w_i}- (\frac{x}{u_j} + \frac{y}{v_j} +\frac{z}{w_j}) <0 TiTj=uix+viy+wiz(ujx+vjy+wjz)<0
因为每段路程都大于 0 0 0,所以我们将这个式子除 z z z,变成二元不等式,然后求可行域,用半平面交判断即可;

a x + b y + c < 0 ax + by + c < 0 ax+by+c<0

难点在于将直线表达式换成向量,当然也可以不用向量做,直接用直线表达式做;

换成向量的话,给出一个图比较好理解做法;
在这里插入图片描述
这是 a x + b y < 0 ax + by < 0 ax+by<0的向量情况,要得到题中的情况,我们只需要平移即可;
这样我们就可以得到向量坐标,然后半平面交即可;

AC代码

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>

#define x first
#define y second

using namespace std;

const double eps = 1e-10;
const int N = 10010;
typedef pair<double, double> PDD;
const double pi = 3.1415926535898;

int T;
int n, cnt;
double u[N], v[N], w[N];

struct Line
{
    PDD st, ed;
    Line(){}
    Line(PDD a, PDD b)
    {
        st = a, ed = b;
    }
} line[N];


PDD operator -(PDD a, PDD b)
{
    return make_pair(a.x - b.x, a.y - b.y);
}

PDD operator *(PDD a, double u)
{
    return make_pair(a.x * u, a.y * u);
}

PDD operator +(PDD a, PDD b)
{
    return make_pair(a.x + b.x, a.y + b.y);
}

int sign(double x)
{
    if (fabs(x) < eps) return 0;
    if (x < 0) return -1;
    return 1;
}

int dcmp(double x, double y)
{
    if (fabs(x - y) < eps) return 0;
    if (x < y) return -1;
    return 1;
}

double cross(PDD a, PDD b)
{
    return a.x * b.y - a.y * b.x;
}

double area(PDD a, PDD b, PDD c)
{
    return cross(b - a, c - a);
}

PDD get_intersect(PDD p, PDD v, PDD q, PDD w)
{
    PDD u = p - q;
    double t = cross(w, u) / cross(v, w);
    return make_pair(p.x + v.x * t, p.y + v.y * t);
}

double point_product(PDD a, PDD b)
{
    return a.x * b.x + a.y * b.y;
}

double get_len(PDD a, PDD b)
{
    double dx = a.x - b.x;
    double dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
}

double get_angle(Line a)
{
    return atan2(a.ed.y - a.st.y, a.ed.x - a.st.x);
}

bool cmp(Line a, Line b)
{
    double A = get_angle(a);
    double B = get_angle(b);

    if (!dcmp(A, B)) return area(a.st, a.ed, b.st) < 0;

    return A < B;
}

PDD get_intersect(Line a, Line b)
{
    return get_intersect(a.st, a.ed - a.st, b.st, b.ed - b.st);
}

bool on_right(Line a, Line b, Line c)
{
    PDD o = get_intersect(b, c);
    return sign(area(a.st, a.ed, o)) <= 0;
}

bool half_plane_intersection()
{
    int q[N];
    sort(line, line + cnt, cmp);

    int hh = 0, tt = -1;
    for (int i = 0; i < cnt; i ++)
    {
        if (i && !dcmp(get_angle(line[i]), get_angle(line[i - 1]))) continue;

        while (hh + 1 <= tt && on_right(line[i], line[q[tt]], line[q[tt - 1]])) tt --;
        while (hh + 1 <= tt && on_right(line[i], line[q[hh]], line[q[hh + 1]])) hh ++;

        q[++ tt] = i;
    }

    while (hh + 1 <= tt && on_right(line[q[hh]], line[q[tt]], line[q[tt - 1]])) tt --;
    while (hh + 1 <= tt && on_right(line[q[tt]], line[q[hh]], line[q[hh + 1]])) hh ++;

    q[++ tt] = q[hh];
    return tt - hh >= 3;
}

bool solve(int id)
{
    cnt = 0;
    double x1, y1, x2, y2;

    for (int j = 1; j <= n; j ++)
    {
        if (j == id) continue;
        double a = (u[j] - u[id]) / (u[id] * u[j]);
        double b = (v[j] - v[id]) / (v[id] * v[j]);
        double c = (w[j] - w[id]) / (w[id] * w[j]);
        if (!sign(a))
        {
            if (!sign(b))
            {
                if (sign(c) >= 0) return false;
                continue;
            }
            x1 = sign(b);
            x2 = 0;
            y2 = y1 = - c / b;
        }
        else
        {
            if (!sign(b))
            {
                x2 = x1 = - c / a;
                y2 = 0;
                y1 = -sign(a);
            }
            else
            {
                x1 = sign(b);
                y1 = -(c + a * x1) / b;
                x2 = 0;
                y2 = - c / b;
            }
        }
        line[cnt ++] = Line(make_pair(x1, y1), make_pair(x2, y2));
    }

    line[cnt ++] = Line(make_pair(0.0, 0.0), make_pair(100000.0, 0.0));
    line[cnt ++] = Line(make_pair(100000.0, 0.0), make_pair(100000.0, 100000.0));
    line[cnt ++] = Line(make_pair(100000.0, 100000.0), make_pair(0.0, 100000.0));
    line[cnt ++] = Line(make_pair(0.0, 100000.0), make_pair(0.0, 0.0));
    
    return half_plane_intersection();
}

int main(){

    while (~scanf("%d", &n))
    {
        for (int i = 1; i <= n; i ++) cin >> u[i] >> v[i] >> w[i];

        for (int i = 1; i <= n; i ++)
        {
            if (solve(i)) puts("Yes");
            else puts("No");
        }
    }
    

    return 0;
}

——END

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半碗无糖蓝莓冻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值