UVa 11168 Airport , 凸包

题意:

给出平面上n个点,找一条直线,使得所有点在直线的同侧,且到直线的距离之平均值尽量小。

先求凸包

易知最优直线一定是凸包的某条边,然后利用点到直线距离公式进行计算。

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

struct Point {
    int x, y;
    Point(int x=0, int y=0):x(x),y(y) {}
};

typedef Point Vector;

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 p) {
    return Vector(a.x*p, a.y*p);
}
Vector operator / (const Vector& a, double p) {
    return Vector(a.x/p, a.y/p);
}
bool operator < (const Point& p1, const Point& p2){
    return p1.x<p2.x ||(p1.x==p2.x&&p1.y<p2.y);
}

bool operator == (const Point& p1, const Point& p2){
    return p1.x == p2.x && p1.y == p2.y;
}

int Cross(const Vector& a, const Vector& b) {
    return a.x*b.y - a.y*b.x;
}

vector<Point> ConvexHull(vector<Point> p) {
    sort(p.begin(), p.end());
    p.erase( unique(p.begin(), p.end()), p.end());

    int n = p.size();
    int m = 0;
    vector<Point> ch(n+1);
    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--;
    ch.resize(m);
    return ch;
}

// 过两点p1, p2的直线一般方程ax+by+c=0
// (x2-x1)(y-y1) = (y2-y1)(x-x1)
void getLineGeneralEquation(const Point& p1, const Point& p2, double& a, double& b, double &c) {
  a = p2.y-p1.y;
  b = p1.x-p2.x;
  c = -a*p1.x - b*p1.y;
}

int main()
{
    int t, n, i, j;
    scanf("%d", &t);
    for(int cas=1; cas<=t; ++cas)
    {
        scanf("%d", &n);
        int x, y;
        vector<Point> P;
        double sumx = 0, sumy = 0;
        for(i=0; i<n; ++i)
        {
            scanf("%d%d", &x, &y);
            sumx += x;
            sumy += y;
            P.push_back(Point(x,y));
        }
        P = ConvexHull(P);
        int m = P.size();
        double ans = 1e9;
        if(m<=2) ans = 0;
        else
        for(i=0; i<m; ++i)
        {
                j = (i+1)%m;
                double A, B, C;
                getLineGeneralEquation(P[i], P[j], A, B, C);
                double tmp = fabs(A*sumx + B*sumy + C*n) / sqrt(A*A+B*B);
                ans = min(ans, tmp);
        }
        printf("Case #%d: %.3f\n", cas, ans/n);
    }
    return 0;
}

UVa 11168 Airport , 凸包,布布扣,bubuko.com

时间: 2024-10-13 23:35:36

UVa 11168 Airport , 凸包的相关文章

简单几何(数学公式+凸包) 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

UVA 11168(凸包算法)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34780 Problem 给N个点,画一条直线,所有点都在直线一侧(可以在直线上),且到达直线的平均距离最小(实际上就是总距离最小).输出平均距离,N有10000级别,点坐标挺大. Solution 首先,求一个凸包,枚举凸包点边,O(1)求出所有点到边的距离,维护最小值即可. 首先,为啥枚举凸包的边是对的? 其次,怎么O(1)求出,设直线返程为Ax+By+C=0, 点(x0

UVa 11168(凸包、直线一般式)

要点 找凸包上的线很显然 但每条线所有点都求一遍显然不可行,优化方法是:所有点都在一侧所以可以使用直线一般式的距离公式\(\frac{|A* \sum{x}+B* \sum{y}+C*n|}{\sqrt {A^2+B^2}}\)\(O(1)\)算出总距离 #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #i

Airport UVA - 11168

Airport UVA - 11168 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 10010; 4 const int inf = 0x3f3f3f3f; 5 const int eps = 1e-12; 6 7 struct Point { 8 double x, y; 9 Point (double x = 0, double y = 0) : x(x), y(y) {} 10 }; 11 ty

UVA 11374 Airport Express 机场快线 Dijistra+路径

题目链接:UVA 11374 Airport Express Airport Express Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Expr

uva 10065 (凸包+求面积)

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006 Problem A Useless Tile Packers Input: standard input Output: standard output Yes, as you have apprehended the Useless Tile Pac

uva 11374 Airport Express(最短路)

uva 11374 Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Commercial-Xpress

UVA 11374 - Airport Express(dijstra)

UVA 11374 - Airport Express 题目链接 题意:给定一些经济线,和一些商务线,商务线最多坐一次,每个线有一个时间,问最短时间 思路:从起点,终点各做一次dijstra,然后枚举商务线,就可以算出总时间,最后求出总时间最少 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; #define INF

UVA - 11374 Airport Express (Dijkstra模板+枚举)

Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Comm