best meeting point

本文讨论了如何在无限网格上为N个人找到一个最优的会议地点,使得所有人到达该地点的总行程时间最小。通过将点映射到x轴和y轴,并分别找到中位数位置,最终确定会议地点。

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

There is an infinite integer grid at which N people have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, all 8 adjacent cells are reachable in 1 unit of time. eg: (x,y) can be reached from (x-1,y+1) in a single unit of time. Find a common meeting place which minimizes the sum of the travel times of all the persons.



这个题目可以参考之前做的加油站问题,只是一维简化版本

有n个加油站,选择中间的那个加油站,作为总加油站,其他加油站到它的距离最短


可以先把所有的点映射到x轴,找到一个点位于所有点的中位数位置

再把所有的点映射到Y轴,找到一个点位于所有点的中位数位置

这两条线的交点就是meeting place


struct POS 
{
        int x;
        int y;
};

bool CompX(POS a, POS b) { return a.x < b.x; }
bool CompY(POS a, POS b) { return a.y < b.y; }

POS FindMeetingPlace(POS a[], int n)
{
        assert(a && n>0);

        POS pos;
        sort(a, a+n, CompX);
        pos.x = a[n/2].x;

        sort(a, a+n, CompY);
        pos.y = a[n/2].y;

        return pos;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值