uva 11168 Airport(训练指南)

思路:特殊情况,点在一条直线上,求凸包的时候可以检查出来,n等于1的时候是个特殊情况。

求点到直线的距离,因为点在直线Ax + By + C = 0同侧。所以对于任意n个点中的一个点 (X0,  Y0) , Ax0 + By0 + C 应该正负号相同。

用直线的一般式就可以用O(1)的时间求一条直线上的距离。

儿童节第二题,哈哈。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
int n;

struct point {
    double x;
    double y;
    point (double a=0, double b = 0):x(a), y(b){}
};
typedef point Vector;

point operator + (const point &a, const point &b) {
    return point(a.x+b.x, a.y+b.y);
}
point operator - (const point &a, const point &b) {
    return point(a.x-b.x, a.y-b.y);
}

double det(const point &a, const point &b) {
    return a.x*b.y - a.y*b.x;
}

struct polygon_convex {
    vector <point> P;
    polygon_convex(int Size = 0) {
        P.resize(Size);
    }
};
const double eps = 1e-10;
int dcmp(double x) {
    if(fabs(x)<eps) return 0;
    if(x > 0) return 1;
    return -1;
}

bool comp_less(const point &a, const point &b) {
    return dcmp(a.x-b.x)<0 || (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)<0);
}
bool cmpx(const point &a, const point &b) {
    if(dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0) return true;
    return false;
}
polygon_convex convex_hull(vector<point> a) {
    polygon_convex  res(2*a.size()+5);
    sort(a.begin(), a.end(), comp_less);
    a.erase(unique(a.begin(), a.end(), cmpx), a.end());
    int m = 0;
    for(int i = 0; i < int(a.size()); ++i) {
        while(m>1 && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0)
            --m;
        res.P[m++] = a[i];
    }
    int k = m;
    for(int i = int(a.size())-2; i >= 0; --i) {
        while(m>k && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0)
            --m;
        res.P[m++] = a[i];
    }
    res.P.resize(m);
    if(a.size()>1) res.P.resize(m-1);
    return res;
}

vector <point> tmp;
double sumx = 0, sumy = 0;
double ans = 0;

void init() {
    sumx = 0, sumy = 0;
    tmp.clear();
    ans = 1e100;
}
//Ax + By + C = 0;
double get(double A, double B, double C) {
    double k = fabs(A*sumx + B*sumy + n*C); //刚开始也没考虑到。
    double v = sqrt(A*A + B*B);//v != 0;
    return k/v;
}

double getDist(const point &a, const point &b) {
    double A = a.y-b.y;
    double B = b.x-a.x;
    double C = a.x*b.y - a.y*b.x;
    return get(A, B, C);
}

int main()
{
    int counter = 0;
    int T;
    point t;
    scanf("%d", &T);
    while(T--) {
        init();
        scanf("%d", &n);
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf", &t.x, &t.y);
            sumx += t.x; sumy += t.y;
            tmp.push_back(t);
        }
        polygon_convex tres = convex_hull(tmp);
        int Size = (int)tres.P.size();
        printf("Case #%d: ", ++counter);
        if(Size == 2 || Size == 1) { //刚开始wa一次,看了看题目n>0.又把n=1考虑一下。
            printf("0.000\n");
            continue;
        }
        for(int i = 0; i < Size; i++) {
           double temp = getDist(tres.P[i], tres.P[(i+1)%Size]);
           ans = min(ans, temp);
        }
        printf("%.3lf\n", ans/n);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值