POJ 1228 Grandpa's Estate (稳定凸包)

题目:传送门

题意:起初有很多点,可以确定一个凸包,现在只给你部分点,问你部分点确定的凸包是否一定和最初全部点确定的凸包一样。

思路:

题意就是问给你的 n 个点是否能唯一确定一个凸包。 这种凸包叫稳定凸包。这里有个博客讲得不错:

那么需要修改一下求凸包的模板,把那些共线的点也存起来,然后再判断是否每条线都至少有三个点。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#define LL long long
#define mem(i, j) memset(i, j, sizeof(i))
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define dep(i, j, k) for(int i = k; i >= j; i--)
#define pb push_back
#define make make_pair
#define INF INT_MAX
#define inf LLONG_MAX
#define PI acos(-1)
using namespace std;

const int N = 5e4 + 5;

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) : x(x), y(y) { } /// 构造函数
};

typedef Point Vector;
/// 向量+向量=向量, 点+向量=向量
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
///点-点=向量
Vector operator - (Point A, Point B) { return Vector(A.x - B.x, A.y - B.y); }
///向量*数=向量
Vector operator * (Vector A, int p) { return Vector(A.x * p, A.y * p); }
///向量/数=向量
Vector operator / (Vector A, int p) { return Vector(A.x / p, A.y / p); }

const int eps = 1e-10;
int dcmp(double x) {
    if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1;
}

bool operator < (const Point& a, const Point& b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}

bool operator == (const Point& a, const Point &b) {
    return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}

int Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } /// 点积
int Length(Vector A) { return sqrt(Dot(A, A)); } /// 计算向量长度
int Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } /// 向量A、B夹角
int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } /// 叉积

bool Onseg(Point p, Point a1, Point a2) { /// 判断点 p 是否在线段 a1a2 上(含端点)
    return Cross(a1 - p, a2 - p) == 0 && Dot(a1 - p, a2 - p) <= 0;
}

Point P[N], Q[N];

int ConvexHull(Point* p, int n, Point* ch) { /// 求凸包
    sort(p, p + n);
    int m = 0;
    rep(i, 0, n - 1) {
        while(m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) < 0) m--;
        ch[m++] = p[i];
    }
    int k = m;
    dep(i, 0, n - 2) {
        while(m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) < 0) m--;
        ch[m++] = p[i];
    }
    if(n > 1) m--;
    return m;
}

void solve() {
    int n; scanf("%d", &n);
    rep(i, 0, n - 1) scanf("%d %d", &P[i].x, &P[i].y);

    if(n <= 5) {
        puts("NO"); return ;
    }

    int cnt = ConvexHull(P, n, Q);

    rep(i, 0, cnt - 1) {
        if(Onseg(Q[i], Q[(i - 1 + cnt) % cnt], Q[(i + 1) % cnt]) == 0 && Onseg(Q[(i + 1) % cnt], Q[i], Q[(i + 2) % cnt]) == 0) {
            puts("NO"); return ;
        }
    }
    puts("YES"); return ;
}

int main() {

    int _; scanf("%d", &_);
    while(_--) solve();

    return 0;
}
一步一步,永不停息
原文地址:https://www.cnblogs.com/Willems/p/12432932.html