zoukankan      html  css  js  c++  java
  • *[hackerrank]Jim Beam

    https://www.hackerrank.com/contests/infinitum-aug14/challenges/jim-beam

    学习了线段相交的判断法。首先是叉乘,叉乘的几何意义是有向的平行四边形的面积(除以2就是三角形的面积)。如果ABD和ABC正负相反,说明C和D在AB两侧,同样的,再判断A和B是否在CD两侧即可。当某三角形面积为0时,需要判断是否在线段上。

    #include <iostream>
    using namespace std;
    
    typedef long long LL;
    LL cross_product(LL ax, LL ay, LL bx, LL by, LL cx, LL cy) {
        return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
    }
    
    bool inside(LL ax, LL ay, LL bx, LL by, LL cx, LL cy) {
        return (min(ax, bx) <= cx && cx <= max(ax, bx)
            && min(ay, by) <= cy && cy <= max(ay, by));
    }
    
    bool intersect(LL ax, LL ay, LL bx, LL by, LL cx, LL cy) {
        LL abc = cross_product(ax, ay, bx, by, cx, cy);
        LL abd = cross_product(ax, ay, bx, by, 0, 0);
        LL cda = cross_product(cx, cy, 0, 0, ax, ay);
        LL cdb = cross_product(cx, cy, 0, 0, bx, by);
        if (((abc > 0 && abd < 0) 
            || (abc < 0 && abd > 0))
            && ((cda > 0 && cdb < 0)
            || (cda < 0 && cdb > 0)))
            return true;
        if (abc == 0 && inside(ax, ay, bx, by, cx, cy)) return true;
        if (abd == 0 && inside(ax, ay, bx, by, 0, 0)) return true;
        if (cda == 0 && inside(cx, cy, 0, 0, ax, ay)) return true;
        if (cdb == 0 && inside(cx, cy, 0, 0, bx, by)) return true;
    
        return false;
    }
    
    void solve() {
        int x1, y1, x2, y2, x, y;
        cin >> x1 >> y1 >> x2 >> y2 >> x >> y;
        if (intersect(x1, y1, x2, y2, x, y))
            cout << "NO" << endl;
        else
            cout << "YES" << endl;
    }
    
    int main() {
        int t;
        cin >> t;
        while (t--) {       
            solve();
        }
        return 0;
    }
    

      

  • 相关阅读:
    MySQL多实例的环境下,服务器端本地连接到指定实例的问题(sock方式连接)
    binlog2sql实现MySQL误操作的恢复
    MySQL 8.0 新增SQL语法对窗口函数和CTE的支持
    asp.net msbuild 发布
    vue aes
    c# Mono.Cecil IL方式 读MethodBody
    C# IL 生成EXE
    js 动态加载 jq
    mysql 得到指定时间段的日期
    powershell 开windows 端口
  • 原文地址:https://www.cnblogs.com/lautsie/p/3930788.html
Copyright © 2011-2022 走看看