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 (Xi, Yi) (-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 (X, Y) and (Xi, Yi) 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 (Xi, Yi) 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
提示
#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个点的曼哈顿距离最小)的更多相关文章
- POJ 1118 求平面上最多x点共线
题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...
- HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...
- [USACO07FEB]新牛棚Building A New Barn
洛谷题目链接:[USACO07FEB]新牛棚Building A New Barn 题目描述 After scrimping and saving for years, Farmer John has ...
- 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 ...
- P2874 [USACO07FEB]新牛棚Building A New Barn
题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wants the ...
- (模板)hdoj1007(分治求平面最小点对)
题目链接:https://vjudge.net/problem/HDU-1007 题意:给定n个点,求平面距离最小点对的距离除2. 思路:分治求最小点对,对区间[l,r]递归求[l,mid]和[mid ...
- 洛谷P2874 [USACO07FEB]新牛棚Building A New Barn [贪心]
题目传送门 题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wan ...
- 洛谷 P1257 平面上的最接近点对 题解
P1257 平面上的最接近点对 题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的. 输入格式 第一行:n:2≤n≤10000 接下来n行:每行两 ...
- POJ C程序设计进阶 编程题#4:寻找平面上的极大点
编程题#4:寻找平面上的极大点 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描 ...
随机推荐
- 学习笔记TF033:实现ResNet
ResNet(Residual Neural Network),微软研究院 Kaiming He等4名华人提出.通过Residual Unit训练152层深神经网络,ILSVRC 2015比赛冠军,3 ...
- WPF中获取控件默认样式和模板XML
从微软官方找这个东西甚是困难,似乎根本没有提供.网上说因为版本问题,很难找到,但通过代码却可以轻易获得.经测试,生成的样式文件非常完美,完全不用修改即可应用. 代码如下: public static ...
- PythonStudy——名称空间 Name space
# 名称空间:就是名字与地址的对应关系,可以被Python解释器遍历查找,采用堆栈存储方式 # 三种名称空间# Built-in:内置名称空间:系统级,一个:随解释器执行而产生,解释器停止而销毁# G ...
- JVM的基本结构及其各部分详解(一)
1 java虚拟机的基本结构如图: 1)类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放于一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行时常量池信息,包括字 ...
- listagg乱码问题
listagg(to_char(ts.tsmc),',') within GROUP (order by xs.xh) ,先将字段to_char 以后,就可以解决
- docker登录运行中的容器的4方案
目前容器云非常的成熟,也有很多的使用案例,可以说不是什么高大上的东西了,可以说整个云计算也不是什么奢侈品,而是基础设施.但是如何使用,就成了必须的技术. 今天记录下,基于docker的容器登录技术. ...
- Hello ThreadPoolExecutor
ThreadPoolExecutor创建: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliv ...
- 报错:Exception in thread "main" com.typesafe.config.ConfigException$UnresolvedSubstitution
报错现象: 报错原因: pom文件中的jar包太高,可以降低jar包的版本号. 报错解决: 我将2.11换成了2.10,即可解决. <dependency> <groupId> ...
- VLAN中继协议
VTP(VLAN Trunking Protocol):是VLAN中继协议,也被称为虚拟局域网干道协议.作用是十几台交换机在企业网中,配置VLAN工作量大,使用VTP协议,把一台交换机配置成VTP S ...
- wepy 小程序云开发
小程序前段时间更新了云开发的功能,刚听到对时候觉得简直是个人开发者对福音,尤其是对我这样对后端不是很懂对前端傻傻,简直不能太方便,就试了下. 体验二维码: 源码:https://github.com/ ...