直线距离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;
}

文章结束给大家分享下程序员的一些笑话语录: 问路
有一个驾驶热气球的人发现他迷路了。他降低了飞行的高度,并认出了地面 上的一个人。他继续下降高度并对着那个人大叫,“打扰一下,你能告诉我我 在哪吗?”
下面那个人说:“是的。你在热气球里啊,盘旋在 30 英尺的空中”。
热气球上的人说:“你一定是在 IT 部门做技术工作”。
“没错”,地面上的人说到,“你是怎么知道的?”
“呵呵”,热气球上的人说,“你告诉我的每件事在技术上都是对的,但对都没 有用”。
地面上的人说,“你一定是管理层的人”。
“没错”,热气球上的人说,“可是你是怎么知道的?”
“呵呵”,地面上的那人说到,“你不知道你在哪里,你也不知道你要去哪,你 总希望我能帮你。你现在和我们刚见面时还在原来那个地方,但现在却是我 错了”。

--------------------------------- 原创文章 By
直线和距离
---------------------------------

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3112930.html