判断凸包是否相交 (UVA10256、HDU6590)

探讨如何通过计算几何方法判断两组点在二维平面上是否可通过一条直线完全分离,涉及凸包算法及线段交点判断,适用于支持向量机等分类模型的数据预处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

小明是一个OI/ACM编程爱好者,总喜欢独立思考一些问题。最近他开始转型AI了,在学习机器学习基础。有一天,他翻开书,看到了分类模型之支持向量机这一节。小明不想直接照着书看答案,小明想如果是我自己要想一个方法来区分两堆给定位置的点是否可以被一个线性模型分开,我有什么好办法么?为了简化问题,先只考虑二维平面的情况吧。

假设在二维平面内给定了N个红色的点和N个蓝色的点,它们的二维坐标都是已知的。请问是否存在一条直线,使得所有红色的点和所有蓝色的点分别出现在直线的两侧?(题目保证:如果两组点可分,则存在分割线,且分割线跟红点和蓝点之间都会存在一定的间隔;如果两组点不可分,则红点和蓝点会有一定程度的交织。)

[背景:数据的线性可分性,经典分类模型之支持向量机(SVM)]

Input Format

输入由 3 行构成,第一行是一个正整数 N ,表示平面内存在 N 个红色点和 N 个蓝色点; 第二行和第三行分别是 N 个红色点的坐标和 N 个蓝色点的坐标,每个坐标是两个整数,因此这两行各有 2N 个整数。两个整数之间均以空格隔开。

每次的输入是5组数据,一共15行。

每一组数据的第一行是样本的个数N。

第二行是红色的N个点,x1 y1 x2 y2 x3 y3.....xN yN。一共2N个整型数,以空格隔开。

第三行是蓝色的N个点,与上述红点格式一样。一共2N个整型数,以空格隔开。

Constraints

对于样本的数量N: 0< N<=500

对于样本的坐标: 0<=x<=10000 0<=y<=10000

100%的test case中样本数量N不超过500.

Output Format

对于每一组数据,输出字符串"True"或者"False",一行一个,一共5行输出。

如果存在这样的直线,可以完美分类所有的样本点,输出"True",否则输出"False"。

Sample Input 0

2
0 0 1 1
1 0 0 1
2
0 0 0 1
1 0 1 1
2
1 1 0 1
0 0 1 0
3
0 1 0 2 1 2
1 1 0 3 0 0
3
0 0 1 1 2 2
3 5 4 6 5 7

Sample Output 0

False
True
True
False
True

步骤:

  1. 判断凸包的点是否存在相互包含情况,即判断点是否在凸包内。可以O(lg(n))判断,此处为O(n)写法。
  2. 判断凸包边是否相交,O(n^2)复杂度。

类似题目:UVA10256HDU6590

代码:

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1005;

int n;
int sgn(long long x) {return (x > 0) - (x < 0);}

struct P {
    long long d[3];
    long long& operator[](int x) {
        return d[x];
    }
    P () {}
    P (long long a, long long b, long long c) {
        d[0] = a; d[1] = b; d[2] = c;
    }
};

struct node {
    P x;
    int y;
} p[N];

P operator+(P a, P b) { // a+b
    P c;
    for (int i = 0; i < 3; i++)
        c[i] = a[i] + b[i];
    return c;
}

P operator-(P a, P b) { // a-b
    P c;
    for (int i = 0; i < 3; i++)
        c[i] = a[i] - b[i];
    return c;
}

P operator*(int a, P b) { // a*b
    P c;
    for (int i = 0; i < 3; i++)
        c[i] = a * b[i];
    return c;
}

bool operator<(P a, P b) { return a[1] < b[1] || (a[1] == b[1] && a[2] < b[2]);}
long long operator*(P a, P b) { return a[1] * b[2] - a[2] * b[1];}
long long operator^(P a, P b) { return a[1] * b[1] + a[2] * b[2];}
long long det(P a, P b, P c) { return (b - a) * (c - a);}

struct L {
    P a, b;
    L () {}
    L (P x, P y) {a = x; b = y;}
};
bool onSeg(P p, L s) {return sgn(det(p, s.a, s.b)) == 0 && sgn((s.a - p) ^ (s.b - p)) <= 0;}

vector<P> convex(vector<P> p) {
    vector<P> ans, S;
    for (int i = 0; i < p.size(); i++) {
        while (S.size() >= 2
                && sgn(det(S[S.size() - 2], S.back(), p[i])) <= 0)
                    S.pop_back();
        S.push_back(p[i]);
    }
    ans = S;
    S.clear();
    for (int i = (int)p.size() - 1; i >= 0; i--) {
        while (S.size() >= 2
                && sgn(det(S[S.size() - 2], S.back(), p[i])) <= 0)
                    S.pop_back();
        S.push_back(p[i]);
    }
    for (int i = 1; i + 1 < S.size(); i++)
        ans.push_back(S[i]);
    return ans;
}

bool PointInPolygon(P p, vector<P> poly) {
    int cnt = 0;
    for (int i = 0; i < poly.size(); i++) {
        if (onSeg(p, L(poly[i], poly[(i + 1) % poly.size()]))) return true;
        int k = sgn(det(poly[i], poly[(i + 1) % poly.size()], p));
        int d1 = sgn(poly[i][2] - p[2]);
        int d2 = sgn(poly[(i + 1) % poly.size()][2] - p[2]);
        if (k > 0 && d1 <= 0 && d2 > 0) cnt++;
        if (k < 0 && d2 <= 0 && d1 > 0) cnt--;
    }
    if (cnt != 0) return true;
    return false;
}

bool SegmentIntersection(L l1, L l2) {
    long long c1 = det(l1.a, l1.b, l2.a), c2 = det(l1.a, l1.b, l2.b);
    long long c3 = det(l2.a, l2.b, l1.a), c4 = det(l2.a, l2.b, l1.b);
    if (sgn(c1) * sgn(c2) < 0 && sgn(c3) * sgn(c4) < 0) return true;
    if (sgn(c1) == 0 && onSeg(l2.a, l1)) return true;
    if (sgn(c2) == 0 && onSeg(l2.b, l1)) return true;
    if (sgn(c3) == 0 && onSeg(l1.a, l2)) return true;
    if (sgn(c4) == 0 && onSeg(l1.b, l2)) return true;
    return false;
}

bool ConvexHullDivide(vector<P> p1, vector<P> p2) { // O(n^2)
    for (int i = 0; i < p1.size(); i++) 
        if (PointInPolygon(p1[i], p2))
            return false;
    for (int i = 0; i < p2.size(); i++)
        if (PointInPolygon(p2[i], p1))
            return false;
    for (int i = 0; i < p1.size(); i++)
        for (int j = 0; j < p2.size(); j++)
            if (SegmentIntersection(L(p1[i], p1[(i + 1) % p1.size()]), L(p2[j], p2[(j + 1) % p2.size()])))
                return false;
    return true;
}

bool check() {
    vector<P> p1, p2;
    for (int i = 1; i <= n; i++) {
        if (p[i].y == 1) p1.push_back(p[i].x);
        else p2.push_back(p[i].x);
    }
    vector<P> c1, c2;
    c1 = convex(p1); c2 = convex(p2);
    if (ConvexHullDivide(c1, c2)) return true;
    return false;
}

int main() {
    while(~scanf("%d", &n)){
        for(int i=1; i<=n; i++) cin >> p[i].x[1] >> p[i].x[2], p[i].y = 1, p[i].x[0] = 1;
        for(int i=1; i<=n; i++) cin >> p[i+n].x[1] >> p[i+n].x[2], p[i+n].y = -1, p[i].x[0] = 1;
        n <<= 1;
        if(!check()) puts("False");
        else puts("True");
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值