Flash Mob

这是一道编程题,要求找出一个点,使得该点到给定的所有点的距离之和最小。解题思路是确定这个点位于所有点的中间位置,通过比较找到这个最优解。

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

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1944

题目大意:找出一个使其到给出的所有 的点距离最短

题目思路:点肯定存在在所有点的中间位置,从中间位置比较找出该点

题目:

Flash Mob

Time Limit: 1000MS Memory limit: 65536K

题目描述

Jumping Jack is in charge of organizing a flash mob. The members of the flash mob move around
town all day and part of the appeal of this group is they come together to do their thing whenever the mood strikes Jack. When the mood does strike, Jack sends a text message to the members to meet at a
particular intersection in town in exactly one hour. The streets of the town run only north-south or east-west and are evenly spaced, forming a perfect grid like a sheet of graph paper. Due to the spontaneity, Jack wants to minimize the inconvenience and so picks an intersection to minimize the total distance
traveled by the flash mob’s members. Fortunately Jack has the locations of all the members via the
GPS on their cell phones. Your job is to find the meeting location given all the members’ locations.
Each intersection will be given by a pair of non-negative integers; the first coordinate is the east-west street and the second coordinate is the north-south street. The location of each flash mob member will be an intersection. Members can travel only north-south or east-west between intersections.
For example, suppose there are 5 mob members at locations (3, 4), (0, 5), (1, 1), (5, 5), and (5, 5). Then if Jack summons them all to location (3, 5), the total number of blocks traveled by the mob members
would be 14. Jack could do no better – but sometimes the ‘best’ location may not be unique.
 

输入

Input for each test case will be a series of integers on one or more lines. The first integer, n (1 ≤ n ≤ 1000), indicates the number of mob members. There follow n pairs of integers indicating the location (intersection) of each member. The location coordinates are both between 0 and 10^6, inclusive. More than one member may be at the same intersection. A line containing 0 will follow the last test case.

输出

Output one line for each test case in the format given below. The ordered pair is the coordinates of the location in the city where the total distance traveled (in blocks) is minimal. If there is more than one such location, output the one with the smallest first coordinate. If there is more than one ‘best’ location with the smallest first coordinate, output the one of those with the smallest second coordinate. 
The total number of blocks traveled by all the mob members follows the location.

示例输入

5 3 4 0 5 1 1 5 5 5 5
4 100 2 100 2 100 2 1 20000
0

示例输出

Case 1: (3,5) 14
Case 2: (100,2) 20097
代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
struct node
{
    int x;
    int y;
} point[1005];
int main()
{
    int n;
    int num=0;
    int x,y;
    while(scanf("%d",&n))
    {
        if(n==0)
            break;
        num++;
        x=0;
        y=0;
        for(int i=0; i<n; i++)
        {
            cin>>point[i].x>>point[i].y;
            x+=point[i].x;
            y+=point[i].y;
        }
        x/=n;
        y/=n;
        int ansx,ansy,dansx,dansy;
        int tmp,d;
        dansx=0;
        for(int i=0; i<n; i++)
        {
            dansx+=fabs(point[i].x-x);
        }
        ansx=x;
        tmp=x;
        while(true)
        {
            tmp--;
            d=0;
            for(int i=0; i<n; i++)
            {
                d+=fabs(point[i].x-tmp);
            }
            if(d>dansx)
                break;
            dansx=d;
            ansx=tmp;
        }
        tmp=x;
        while(true)
        {
            tmp++;
            d=0;
            for(int i=0; i<n; i++)
            {
                d+=fabs(point[i].x-tmp);
            }
            if(d>=dansx)
                break;
            dansx=d;
            ansx=tmp;
        }
        dansy=0;
        for(int i=0; i<n; i++)
        {
            dansy+=fabs(point[i].y-y);
        }
        ansy=y;
        tmp=y;
        while(true)
        {
            tmp--;
            d=0;
            for(int i=0; i<n; i++)
            {
                d+=fabs(point[i].y-tmp);
            }
            if(d>dansy)
                break;
            dansy=d;
            ansy=tmp;
        }
        tmp=y;
        while(true)
        {
            tmp++;
            d=0;
            for(int i=0; i<n; i++)
            {
                d+=fabs(point[i].y-tmp);
            }
            if(d>=dansy)
                break;
            dansy=d;
            ansy=tmp;
        }
        printf("Case %d: (%d,%d) %d\n",num,ansx,ansy,dansx+dansy);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值