uva11168 Airport(凸包)

本文介绍了一道UVA在线编程竞赛题目11168的解决方案,该题要求找到一条直线跑道的位置,使所有居民点到这条直线的平均距离最短。通过计算几何中的凸包算法来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uva11168

题目

就是说有很多的居民点,现要建一条直线跑道,使得每个居民点到跑道的平均距离最小。

思路

求凸包,注意1个点和2个点的情况。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define pi acos (-1)
using namespace std;
typedef long long ll;

const int maxn=10010;
const double INF = 1e30;

int tot;

struct point
{
    double x, y;
    point (double _x = 0, double _y = 0) : x(_x), y(_y) {}
    point operator - (point a) const
    {
        return point (x-a.x, y-a.y);
    }
    point operator + (point a) const
    {
        return point (x+a.x, y+a.y);
    }
    bool operator < (const point &a) const
    {
        return x < a.x || (x == a.x && y < a.y);
    }
} p[maxn];

const double eps = 1e-10;
int dcmp (double x)
{
    if (fabs (x) < eps)
        return 0;
    else return x < 0 ? -1 : 1;
}
double cross (point a, point b)
{
    return a.x*b.y-a.y*b.x;
}
double dot (point a, point b)
{
    return a.x*b.x + a.y*b.y;
}
double AngleToRad (double x)
{
    return x*pi/180;
}
point rotate (point a, double rad)
{
    return point (a.x*cos (rad)-a.y*sin (rad), a.x*sin (rad)+a.y*cos (rad));
}

double ConvexPolygonArea (point *p, int n)
{
    double area = 0;
    for (int i = 1; i < n-1; i++)
        area += cross (p[i]-p[0], p[i+1]-p[0]);
    return area/2.0;
}

int n, m;
double l;
point ch[maxn];

int ConvexHull (int n)
{
    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-1]) <= 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-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    if (n > 1)
        m--;
    return m;
}

double dis (point a, point b)
{
    double xx = a.x-b.x, yy = a.y-b.y;
    return sqrt (xx*xx + yy*yy);
}

int main()
{
    int T;
    scanf("%d",&T);
    int kase=1;
    while(T--)
    {
        tot=0;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            double x,y;
            scanf("%lf%lf",&x,&y);
            p[tot++]=point(x,y);
        }
        if(n==2||n==1)
        {
            printf("Case #%d: 0.000\n",kase++);
            continue;
        }
        m=ConvexHull(tot);
        double ans=INF;
        ch[m+1]=ch[1];
        for(int i=1; i<=m; i++)
        {
            double temp=0;
            double dist=dis(ch[i],ch[i+1]);
            for(int j=0; j<tot; j++)
            {
                temp+=cross (ch[i]-p[j], ch[i+1]-p[j])/dist;
            }
            ans=min(ans,temp);
        }
        printf("Case #%d: %.3lf\n",kase++,ans/(n*1.0));

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值