UVA–11168 Airport[凸包]

TIM截图20171004104647

题意:

做一条直线使得所有的点都在直线的一侧,并且让每个点到直线的距离的和最小,输出平均的最小距离。

思路:

做个凸包,枚举凸包的每个线,所有的点都在凸包的同侧所以所有的点满足$Ax+By+C$同号,将所有的点的x,y坐标值相加带入到点到直线的距离公式里。注意常数要乘上n。

#include "bits/stdc++.h"
using namespace std;
const int maxn = 10000 + 10;
const double eps=1e-8;
const double PI = acos(-1.0);
struct Point {
    double x, y;
    Point(double x = 0.0, double y = 0.0):x(x), y(y) {}
};
typedef Point Vector;
Point operator + (Point A, Point B) {
    return Point(A.x+B.x, A.y+B.y);
}
Point operator - (Point A, Point B) {
    return Point(A.x-B.x, A.y-B.y);
}
Point operator * (Point A, double p) {
    return Point(A.x*p, A.y*p);
}
Point operator / (Point A, double p) {
    return Point(A.x/p, A.y/p);
}
bool operator < (const Point& a, const Point& b) {
    return a.x<b.x || (a.x==b.x && a.y<b.y);
}
int dcmp(double x) {
    if (fabs(x)<eps) return 0;return x<0?-1:1;
}
bool operator == (const Point& a, const Point &b) {
    return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y)==0;
}
double Dot(Point A, Point B) {
    return A.x*B.x+A.y*B.y;
}
double Cross(Point A, Point B) {
    return A.x*B.y - A.y*B.x;
}
double Length(Point A) {return sqrt(Dot(A,A));}
Vector Normal(Vector A) {return Vector(-A.y, A.x)/Length(A);}
double Angle(Vector A, Vector B) {
    return acos(Dot(A,B)/Length(A)/Length(B));
} 
Vector Rotate(Vector A, double rad) {
    return Vector(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));
} 
double PolygonArea(Point* p, int n) {
    double area = 0.0;
    for (int i = 1; i < n-1; i++) 
        area += Cross(p[i]-p[0],p[i+1]-p[0]);
    return area/2.0;
}
Point p[maxn], ch[maxn];
bool cmp(Point a, Point b) {
    if (dcmp(a.x - b.x) == 0) return dcmp(a.y - a.y) <= 0;
    return dcmp(a.x - b.x) < 0;
}
int ConvexHull(Point* p, int n, Point* ch) {
    sort(p, p + n); 
    int m = 0;
    for (int i = 0; i < n; i++) {
        while (m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }
    int k = m; 
    for (int i = n - 2; i >= 0; i--) {
        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;
}
double min(double x, double y) {
    return dcmp(x-y)<=0? x: y;
}
int main(int argc, char const *argv[])
{
    int T;
    int Kcase = 0;
    scanf("%d", &T);
    while (T--) {
        int N;
        scanf("%d", &N);
        double sumx = 0, sumy = 0;
        for (int i = 0; i < N; i++) {
            scanf("%lf%lf", &p[i].x, &p[i].y);
            sumx += p[i].x; sumy += p[i].y;
        }
        double minn = 0x3f3f3f3f;
        double A, B, C;
        int cnt = ConvexHull(p, N, ch);
        for (int i = 1; i <= cnt; i++) {
            int i1 = (i==cnt)? 0: i, i2 = i-1;
            A = (ch[i1].x - ch[i2].x);
            if (A == 0) {A = 1.0; B = 0; C = -ch[i2].x;}
            else {A = (ch[i1].y-ch[i2].y)/A, B = -1.0; C = ch[i2].y - A*ch[i2].x;}
            minn = min(minn, fabs(A*sumx + B*sumy + N*C)/sqrt(A*A + B*B));
        }
        printf("Case #%d: %.3lf\n", ++Kcase, minn/N);
    }   
    return 0;
}
posted @ 2017-10-04 11:02  zprhhs  阅读(239)  评论(0编辑  收藏  举报
Power by awescnb