• 简单几何(数学公式+凸包) UVA 11168 Airport


    题目传送门

    题意:找一条直线,使得其余的点都在直线的同一侧,而且使得到直线的平均距离最短。

    分析:训练指南P274,先求凸包,如果每条边都算一边的话,是O (n ^ 2),然而根据公式知直线一般式为Ax + By + C = 0.点(x0, y0)到直线的距离为:fabs(Ax0+By0+C)/sqrt(A*A+B*B)。

    所以只要先求出x的和以及y的和,能在O (1)计算所有距离和。

    两点式直线方程p1 (x1, y1),p2 (x2, y2)转换成一般式直线方程:A = y1 - y2, B = x2 - x1, C = -A * x1 - B * y1;

    /************************************************
    * Author        :Running_Time
    * Created Time  :2015/11/10 星期二 11:24:09
    * File Name     :UVA_11168.cpp
     ************************************************/
    
    #include <bits/stdc++.h>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int N = 1e5 + 10;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    const double EPS = 1e-10;
    const double PI = acos (-1.0);
    int dcmp(double x)  {
        if (fabs (x) < EPS) return 0;
        else    return x < 0 ? -1 : 1;
    }
    struct Point    {
        double x, y;
        Point () {}
        Point (double x, double y) : x (x), y (y) {}
        Point operator - (const Point &r) const {       //向量减法
            return Point (x - r.x, y - r.y);
        }
        Point operator * (double p) const {       //向量乘以标量
            return Point (x * p, y * p);
        }
        Point operator / (double p) const {       //向量除以标量
            return Point (x / p, y / p);
        }
        Point operator + (const Point &r) const {
            return Point (x + r.x, y + r.y);
        }
        bool operator < (const Point &r) const {
            return x < r.x || (x == r.x && y < r.y);
        }
        bool operator == (const Point &r) const {
            return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
        }
    };
    typedef Point Vector;
    Point read_point(void)  {
        double x, y;    scanf ("%lf%lf", &x, &y);
        return Point (x, y);
    }
    double dot(Vector A, Vector B)  {       //向量点积
        return A.x * B.x + A.y * B.y;
    }
    double cross(Vector A, Vector B)    {       //向量叉积
        return A.x * B.y - A.y * B.x;
    }
    double length(Vector V) {
        return sqrt (dot (V, V));
    }
    vector<Point> convex_hull(vector<Point> ps) {
        sort (ps.begin (), ps.end ());
        ps.erase (unique (ps.begin (), ps.end ()), ps.end ());
        int n = ps.size (), k = 0;
        vector<Point> qs (n * 2);
        for (int i=0; i<n; ++i) {
            while (k > 1 && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-2]) <= 0)    k--;
            qs[k++] = ps[i];
        }
        for (int t=k, i=n-2; i>=0; --i) {
            while (k > t && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-2]) <= 0)    k--;
            qs[k++] = ps[i];
        }
        qs.resize (k-1);
        return qs;
    }
    
    double sqr(double x)    {
        return x * x;
    }
    
    int main(void)    {
        int T, cas = 0;  scanf ("%d", &T);
        while (T--) {
            int n;  scanf ("%d", &n);
            vector<Point> ps;
            double x, y, sx = 0, sy = 0;
            for (int i=0; i<n; ++i) {
                scanf ("%lf%lf", &x, &y);
                sx += x;    sy += y;
                ps.push_back (Point (x, y));
            }
            vector<Point> qs = convex_hull (ps);
            if ((int) qs.size () <= 2)  {
                printf ("Case #%d: %.3f
    ", ++cas, 0.0);    continue;
            }
            double mn = 1e9;
            qs.push_back (qs[0]);
            for (int i=0; i<qs.size ()-1; ++i)    {
                double A = qs[i].y - qs[i+1].y;
                double B = qs[i+1].x - qs[i].x;
                double C = -A * qs[i].x - B * qs[i].y;
                double tmp = fabs (A * sx + B * sy + n * C) / sqrt (sqr (A) + sqr (B));
                if (mn > tmp)   mn = tmp;
            }
            printf ("Case #%d: %.3f
    ", ++cas, mn / n);
        }
    
       //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.
    ";
    
        return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    深入浅出Vue基于“依赖收集”的响应式原理(转)
    10道初级渗透测试面试题,测测你离职场有多远?
    CTF必备技能丨Linux Pwn入门教程——格式化字符串漏洞
    CTF必备技能丨Linux Pwn入门教程——利用漏洞获取libc
    CTF必备技能丨Linux Pwn入门教程——调整栈帧的技巧
    CTF必备技能丨Linux Pwn入门教程——ROP技术(下)
    用实力燃爆暑期丨i春秋渗透测试工程师线下就业班开课了!
    CTF必备技能丨Linux Pwn入门教程——ROP技术(上)
    CTF必备技能丨Linux Pwn入门教程——ShellCode
    CTF必备技能丨Linux Pwn入门教程——栈溢出基础
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4953133.html
Copyright © 2020-2023  润新知