格林深瞳编程赛 A.点与线 凸包 线段求交

题目链接
题意: 给出二维平面上的两个点集,求是否能有一条直线将两个点集分成两个部分。
快速确定两个点集的可分性,先分别对两个点集求凸包,再判断两个凸包上的线段是否有相交,这里点的数量比较少,直接遍历即可,如果发现有相交,两点集不可分。
(这里用int来存点的时候,线段跨立实验时这题的数据范围会爆int,gg卡了半天。)
(我觉得这里其实还要判断一个点集有没有完全被另一个点集包围的情况,这种情况凸包的线段可能也不会有相交发生,但判题的数据可能没有考虑这部分情况,删了特判的代码也过了。)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;
const int N = 5e2 + 5;

struct Pt{
    int x, y;
    Pt() {}
    Pt(int x, int y) : x(x), y(y) {}
    friend Pt operator - (const Pt &a, const Pt &b) {
        return Pt(a.x - b.x, a.y - b.y);
    }
}t[N], a[N], b[N];

inline int det(const Pt &a, const Pt &b) {
    return a.x * b.y - a.y * b.x;
}

inline bool cmp(const Pt &a, const Pt &b) {
    int flag = det(b - t[0], a - t[0]);
    return flag < 0 || (flag == 0 && a.x < b.x);
}

inline bool SgIntersect(const Pt &a, const Pt &b, const Pt &s, const Pt &t) {
    int x1min = min(a.x, b.x), x2min = min(s.x, t.x),
        x1max = max(a.x, b.x), x2max = max(s.x, t.x),
        y1min = min(a.y, b.y), y2min = min(s.y, t.y),
        y1max = max(a.y, b.y), y2max = max(s.y, t.y);
    if(max(x1min, x2min) > min(x1max, x2max) &&
       max(y1min, y2min) > min(y1max, y2max))
        return false;
    // 注意此处直接换成int可能会爆
    return 1LL * det(a - s, t - s) * det(t - s, b - s) >= 0 &&
        1LL * det(s - a, b - a) * det(b - a, t - a) >= 0;
}

int convex_hull(Pt *res, Pt *a, int n) {
    int k = 0;
    for(int i = 1; i < n; i ++)
        if(a[k].y > a[i].y || (a[k].y == a[i].y && a[k].x > a[i].x))
            k = i;
    if(k != 0)
        swap(a[0], a[k]);
    sort(a + 1, a + n, cmp);
    res[0] = a[0], res[1] = a[1];
    int m = 1;
    for(int i = 2; i < n; i ++) {
        while(m && det(res[m] - res[m - 1], a[i] - res[m -1]) <= 0)
            m --;
        res[++ m] = a[i];
    }
    return ++ m;
}

inline int nxt(int x, int n) {
    return (x + 1) % n;
}

// void print(const Pt &a) {
//     cout << a.x << ' ' << a.y << endl;
// }

int main() {
    ios::sync_with_stdio(false);
    int n;
    while(cin >> n) {
        int ax1 = INT_MAX, ay1 = INT_MAX, ax2 = INT_MIN, ay2 = INT_MIN;
        for(int i = 0; i < n; i ++) {
            cin >> t[i].x >> t[i].y;
            ax1 = min(t[i].x, ax1), ay1 = min(t[i].y, ay1);
            ax2 = max(t[i].x, ax2), ay2 = max(t[i].y, ay2);
        }
        int a_len = convex_hull(a, t, n);
        int bx1 = INT_MAX, by1 = INT_MAX, bx2 = INT_MIN, by2 = INT_MIN;
        for(int i = 0; i < n; i ++) {
            cin >> t[i].x >> t[i].y;
            bx1 = min(t[i].x, bx1), by1 = min(t[i].y, by1);
            bx2 = max(t[i].x, bx2), by2 = max(t[i].y, by2);
        }
        int b_len = convex_hull(b, t, n);
        bool flag = true;
        int cnt = 0;
        for(int i = 0; i < a_len; i ++) {
            for(int j = 0; j < b_len; j ++)
                if(SgIntersect(a[i], a[nxt(i, a_len)], b[j], b[nxt(j, b_len)])) {
                    // cout << "------------" << endl;
                    // print(a[i]), print(a[nxt(i, a_len)]), print(b[j]), print(b[nxt(j, b_len)]);
                    // cout << "------------" << endl;
                    cnt ++;
                    flag = false;
                    break;
                }
            if(!flag) break;
        }
        // 特判包容情况
        if(flag)
            if(ax1 >= bx1 && ax2 <= bx2 && ay1 >= by1 && ay2 <= by2 ||
               bx1 >= ax1 && bx2 <= ax2 && by1 >= ay1 && by2 <= ay2)
                flag = false;
        cout << (flag ? "True" : "False") << endl;
    }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值