zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",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
    ",sum(x1,y1),ans);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    干掉:“请停用以开发者模式运行的扩展程序”
    电脑分区
    tomcat进行远程debug
    工作流审核到最后一步迟迟不能完成
    DataGrid首次进入页面时,不加载任何数据[转]
    用Excel进行个人敏捷项目看板管理
    cnblogs的编辑器BUG反馈:发布或编辑文章完成后,页面是卡死状态的
    程序员如何优雅地挣零花钱?
    SQL Server导入数据报错"无法在只读列“Id”中插入数据",几百个表怎么批量启用'启用标识插入'选项
    SQL Server 各版本发布时间、开发代号及下载地址
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9842165.html
Copyright © 2011-2022 走看看