描述

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)矩形的大小-矩形中已经存在的点的个数
代码
 #include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std; const int maxn=;
int n;
struct p
{
int x,y;
}a[maxn];
bool cmp1(p a,p b){return a.x<b.x;}
bool cmp2(p a,p b){return a.y<b.y;}
bool check(int xx,int yy)
{
for(int i=;i<n;i++)if(a[i].x==xx&&a[i].y==yy)return ;
return ;
}
int sum(int xx,int yy)
{
int ans=;
for(int i=;i<n;i++)
ans+=abs(xx-a[i].x)+abs(yy-a[i].y);
return ans;
}
int dx[]={,,,-};
int dy[]={,-,,};
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)scanf("%d%d",&a[i].x,&a[i].y);
if(n%==)
{
int mid=n/;
sort(a,a+n,cmp1);
int xx=a[mid].x;
sort(a,a+n,cmp2);
int yy=a[mid].y;
if(check(xx,yy))printf("%d 1\n",sum(xx,yy));
else
{
int minn=1e9,ans=;
for(int i=;i<;i++)
{
int val=sum(xx+dx[i],yy+dy[i]);
if(val<minn)minn=val,ans=;
else if(val==minn)ans++;
}
printf("%d %d\n",minn,ans);
}
}
else
{
int mid=n/;
sort(a,a+n,cmp1);
int x1=a[mid-].x,x2=a[mid].x;
sort(a,a+n,cmp2);
int y1=a[mid-].y,y2=a[mid].y;
int ans=(x2-x1+)*(y2-y1+);
for(int i=;i<n;i++)
if(x1<=a[i].x&&a[i].x<=x2&&y1<=a[i].y&&a[i].y<=y2)
ans--;
printf("%d %d\n",sum(x1,y1),ans);
}
return ;
}

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

  1. POJ 1118 求平面上最多x点共线

    题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...

  2. HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...

  3. [USACO07FEB]新牛棚Building A New Barn

    洛谷题目链接:[USACO07FEB]新牛棚Building A New Barn 题目描述 After scrimping and saving for years, Farmer John has ...

  4. Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学

    1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 394  Solve ...

  5. P2874 [USACO07FEB]新牛棚Building A New Barn

    题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wants the ...

  6. (模板)hdoj1007(分治求平面最小点对)

    题目链接:https://vjudge.net/problem/HDU-1007 题意:给定n个点,求平面距离最小点对的距离除2. 思路:分治求最小点对,对区间[l,r]递归求[l,mid]和[mid ...

  7. 洛谷P2874 [USACO07FEB]新牛棚Building A New Barn [贪心]

    题目传送门 题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wan ...

  8. 洛谷 P1257 平面上的最接近点对 题解

    P1257 平面上的最接近点对 题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的. 输入格式 第一行:n:2≤n≤10000 接下来n行:每行两 ...

  9. POJ C程序设计进阶 编程题#4:寻找平面上的极大点

    编程题#4:寻找平面上的极大点 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描 ...

随机推荐

  1. ios-屏幕适配(代码)

    由于第一个项目中98%的界面都用到UITableView,所以适配仅判断此.知道手工敲代码的繁复,遂传一部分,如果有更优的方法,欢迎提出. 如下图,图中提到的宏定义是在prefix.pch预编绎文件里 ...

  2. Nginx源码结构及如何处理请求

    一.源码结构   1:下载安装包后,解压,可以看到目录结构,其中src目录下放的是源码       2:src源码目录下,可以看到这几个目录     mail:mail目录中存放了实现Nginx服务器 ...

  3. win10+vs2015编译caffe的cpu debug版本、部署matcaffe

    一.编译caffe 1.安装python-3.5.2-amd64.exe https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe ...

  4. XShell停止滚屏,禁止滚动

     Ctrl+S:锁定当前屏幕  Ctrl+Q:解锁当前屏幕 Ctrl+Alt+]  进入命令输入状态

  5. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  6. 涂抹mysql笔记-数据库中的权限体系

    涂抹mysql笔记-数据库中的权限体系<>能不能连接,主机名是否匹配.登陆使用的用户名和密码是否正确.mysql验证用户需要检查3项值:用户名.密码和主机来源(user.password. ...

  7. oracle体系结构理解

    体系结构相关内容每次看遍书,过段时间就忘了..无奈用自己理解的方式记录之. 1.commit与写盘与否没有关系,也就是说修改数据(insert update delete)后并提交数据,这条被修改的数 ...

  8. C#获取当前路径的七种方法

    //1.获取模块的完整路径. string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; // ...

  9. android 开发 View _14 MotionEvent和事件处理详解,与实践自定义滑动条View

    转载https://blog.csdn.net/huaxun66/article/details/52352469 MotionEvent MotionEvent对象是与用户触摸相关的时间序列,该序列 ...

  10. C++学习基础十六-- 函数学习笔记

    C++ Primer 第七章-函数学习笔记 一步一个脚印.循序渐进的学习. 一.参数传递 每次调用函数时,都会重新创建函数所有的形参,此时所传递的实参将会初始化对应的形参. 「如果形参是非引用类型,则 ...