TZOJ 1689 Building A New Barn(求平面上有几个其它点求到n个点的曼哈顿距离最小)

描述

After scrimping and saving for years, Farmer John has decided to build a new barn. He wants the barn to be highly accessible, and he knows the coordinates of the grazing spots of all N (2 ≤ N ≤ 10,000 cows. Each grazing spot is at a point with integer coordinates (XiYi) (-10,000 ≤ Xi ≤ 10,000; -10,000 ≤ Yi ≤ 10,000). The hungry cows never graze in spots that are horizontally or vertically adjacent.

The barn must be placed at integer coordinates and cannot be on any cow's grazing spot. The inconvenience of the barn for any cow is given the Manhattan distance formula | X - Xi | + | Y - Yi|, where (XY) and (XiYi) are the coordinates of the barn and the cow's grazing spot, respectively. Where should the barn be constructed in order to minimize the sum of its inconvenience for all the cows?

输入

Line 1: A single integer: N 
Lines 2..N+1: Line i+1 contains two space-separated integers which are the grazing location (XiYi) of cow i

输出

Line 1: Two space-separated integers: the minimum inconvenience for the barn and the number of spots on which Farmer John can build the barn to achieve this minimum.

样例输入

4
1 -3
0 1
-2 1
1 -1

样例输出

10 4

提示

The minimum inconvenience is 10, and there are 4 spots that Farmer John can build the farm to achieve this: (0, -1), (0, 0), (1, 0), and (1, 1).
题意
给你n个二维平面上的点,求平面上有几个其它点到n个点的曼哈顿距离最小,并输出最小值
题解
曼哈顿距离∑ | X - Xi | + | Y - Yi|,X和Y互不影响可以单独考虑
分别为x和y排序,然后X和Y就是中位数
当中位数唯一:
1.n个点中存在(X,Y),判断(X,Y)上下左右四个点即可
2.否则,只有一个点(X,Y)
当中位数不唯一:
1.答案为(X1-X2)(Y1-Y2)矩形的大小-矩形中已经存在的点的个数
代码
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5 
 6 const int maxn=10005;
 7 int n;
 8 struct p
 9 {
10     int x,y;
11 }a[maxn];
12 bool cmp1(p a,p b){return a.x<b.x;}
13 bool cmp2(p a,p b){return a.y<b.y;}
14 bool check(int xx,int yy)
15 {
16     for(int i=0;i<n;i++)if(a[i].x==xx&&a[i].y==yy)return 0;
17     return 1;
18 }
19 int sum(int xx,int yy)
20 {
21     int ans=0;
22     for(int i=0;i<n;i++)
23         ans+=abs(xx-a[i].x)+abs(yy-a[i].y);
24     return ans;
25 }
26 int dx[]={0,0,1,-1};
27 int dy[]={1,-1,0,0};
28 int main()
29 {
30     scanf("%d",&n);
31     for(int i=0;i<n;i++)scanf("%d%d",&a[i].x,&a[i].y);
32     if(n%2==1)
33     {
34         int mid=n/2;
35         sort(a,a+n,cmp1);
36         int xx=a[mid].x;
37         sort(a,a+n,cmp2);
38         int yy=a[mid].y;
39         if(check(xx,yy))printf("%d 1\n",sum(xx,yy));
40         else
41         {
42             int minn=1e9,ans=0;
43             for(int i=0;i<4;i++)
44             {
45                 int val=sum(xx+dx[i],yy+dy[i]);
46                 if(val<minn)minn=val,ans=1;
47                 else if(val==minn)ans++;
48             }
49             printf("%d %d\n",minn,ans);    
50         }
51     }
52     else
53     {
54         int mid=n/2;
55         sort(a,a+n,cmp1);
56         int x1=a[mid-1].x,x2=a[mid].x;
57         sort(a,a+n,cmp2);
58         int y1=a[mid-1].y,y2=a[mid].y;
59         int ans=(x2-x1+1)*(y2-y1+1);
60         for(int i=0;i<n;i++)
61             if(x1<=a[i].x&&a[i].x<=x2&&y1<=a[i].y&&a[i].y<=y2)
62                 ans--;
63         printf("%d %d\n",sum(x1,y1),ans);
64     }
65     return 0;
66 }

转载于:https://www.cnblogs.com/taozi1115402474/p/9842165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值