题解:CF166B Polygons

· · 题解

思路:

要判断一个多边形是否全部位于另一个多边形的内部可以对两个多边形所有点进行一次求凸包,如果凸包边上的点有属于 B 多边形的,那就说明 B 多边形不完全被 A 多边形包含。但一般求凸包时,向量要严格左偏斜才会继续加点,但这题要求 B 的点也不能在 A 的边上,所以求凸包时只要不右偏斜就把当前点加入才能判出答案,不然会错。

AC 代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, idx;
struct node {
    double x, y;
    int id;
} a[N], st[N];

bool cmp(node l, node r) {
    if (l.x != r.x) {
        return l.x < r.x;
    }

    return l.y < r.y;
}

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

double side(node p, node p1, node a) {
    node x = {p1.x - p.x, p1.y - p.y};
    node y = {a.x - p.x, a.y - p.y};
    return cross(x, y);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i].x >> a[i].y;
        a[i].id = 1;
    }

    cin >> m;

    for (int i = 1; i <= m; i++) {
        cin >> a[i + n].x >> a[i + n].y;
        a[i + n].id = 2;
    }

    n = n + m;
    sort(a + 1, a + 1 + n, cmp);
    st[1] = a[1], st[2] = a[2];
    idx = 2;

    for (int i = 3; i <= n; i++) {
//      cout << a[i].x << ' ' << a[i].y << '\n';

        while (idx > 1 && side(st[idx - 1], st[idx], a[i]) < 0) {
            idx--;
        }

        st[++idx] = a[i];
    }

    for (int i = 1; i <= idx; i++) {
//      cout << st[i].x << ' ' << st[i].y << '\n';

        if (st[i].id == 2) {
            cout << "NO";
            return 0;
        }
    }

    idx = 0;
    st[++idx] = a[n], st[++idx] = a[n - 1];

    for (int i = n - 2; i >= 1; i--) {
//      cout << a[i].x << ' ' << a[i].y << '\n';

        while (idx > 1 && side(st[idx - 1], st[idx], a[i]) < 0) {
            idx--;
        }

        st[++idx] = a[i];
    }

    for (int i = 1; i <= idx; i++) {
//      cout << st[i].x << ' ' << st[i].y << '\n';

        if (st[i].id == 2) {
            cout << "NO";
            return 0;
        }
    }

    cout << "YES";
    return 0;
}

//把所有的点进行围凸包,如果凸包边点全是A点,YES,否则NO
/*
5
4 3
2 -3
-1 -3
-1 0
2 2
5
-1 -2
-1 -1
2 1
3 0
2 -2
*/