lightoj 1203(凸包)

题意:有n个点,问从哪个点用最小的角度可以看到其他所有的点,输出最小角度。
题解:把n个点求凸包,凸包内部的点角度都是360,所以结果就是凸包的最小内角。

#include 
#include 
#include 
#include 
#include 
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1);

double Sqr(double x) { return x * x; }
int dcmp(double x) { if (fabs(x) < eps) return 0; return x > 0 ? 1 : -1; }

struct Point {
    double x, y;
    Point(double a = 0, double b = 0): x(a), y(b) {}
};

typedef Point Vector;
typedef vector Polygon;

Vector operator + (const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); }
Vector operator - (const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y); }
Vector operator * (const Vector& a, double b) { return Vector(a.x * b, a.y * b); }
Vector operator / (const Vector& a, double b) { return Vector(a.x / b, a.y / b); }
bool operator == (const Vector& a, const Vector& b) { return !dcmp(a.x - b.x) && !dcmp(a.y - b.y); }
bool operator < (const Vector& a, const Vector& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
double Dot(const Vector& a, const Vector& b) { return a.x * b.x + a.y * b.y; }
double Length(const Vector& a) { return sqrt(Dot(a, a)); }
double Cross(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; }
double Angle(const Vector& a, const Vector& b) { return acos(Dot(a, b) / Length(a) / Length(b)); }
double angle(Vector v) { return atan2(v.y, v.y); }

int ConvexHull(Point* P, int cnt, Point* res) {
    sort(P, P + cnt);
    cnt = unique(P, P + cnt) - P;
    int m = 0;
    for (int i = 0; i < cnt; i++) {
        while (m > 1 && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    int k = m;
    for (int i = cnt - 2; i >= 0; i--) {
        while (m > k && Cross(res[m - 1] - res[m - 2], P[i] - res[m - 2]) <= 0)
            m--;
        res[m++] = P[i];
    }
    if (cnt > 1) m--;
    return m;
}
const int N = 1e5 + 5;
int n;
Point P[N], res[N];

int main() {
    int t, cas = 1;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &P[i].x, &P[i].y);
        int cnt = ConvexHull(P, n, res);    
        if (cnt < 3) {
            printf("Case %d: 0\n", cas++);
            continue;
        }
        res[cnt] = res[0];
        res[cnt + 1] = res[1];
        double ans = 2 * PI;
        for (int i = 1; i <= cnt; i++)
            ans = min(ans, Angle(res[i - 1] - res[i], res[i + 1] - res[i]));
        printf("Case %d: %.10lf\n", cas++, ans * 180 / PI);
    }
    return 0;
}

你可能感兴趣的:(ACM-几何)