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 描 ...
随机推荐
- 【js字符串当做数组来使用】浪费一晚【想出了3个解决方案】
数据库的所有数据都打成字符串发到前端. 不必把它的类型也强制转成int这类的,页面负责字符串的展示 这样做可以修改页面的数据 response.setHeader("Content-type ...
- C# winform 打开主界面并关闭登录界面
在winform 界面编程中,我们有时候要在主界面打开之前先显示登录界面,当登录界面用户信息校验正确后才打开主界面,而这时登陆界面也完成使命该功成身退了. 目前有两种方法可实现: 方法1. 隐藏登录界 ...
- Custom Grid Columns - FireMonkey Guide
原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...
- 使用JAVA实现的一个简单IOC注入实例
https://blog.csdn.net/echoshinian100/article/details/77977823 欲登高而望远,勿筑台于流沙 RSS订阅 原 使用JAVA实现的一个简单IOC ...
- LeetCode——300. Longest Increasing Subsequence
一.题目链接:https://leetcode.com/problems/longest-increasing-subsequence/ 二.题目大意: 给定一个没有排序的数组,要求从该数组中找到一个 ...
- win10下btcd的安装和简单配置
btcd btcd github地址 1. 安装 1) 安装go的包管理工具glide glide github地址 $ go get -u github.com/Masterminds/glide ...
- ARM 编译产生.map之RO RW ZI
RO: 程序中的指令和常量,存储在常量和代码区,因为是不能改变的,所以—READ ONLY; RW: 程序中的已初始化全局变量和静态变量,存储在堆中,因为是变量,所以——READ AND WRITE ...
- python函数嵌套定义
python的这个特性是很特别的,与C#和C++都不一样.请看下面的例子 def outFun(): def innerFun_0():#1.在内部定义一个函数 print("i am fi ...
- WordPress版微信小程序2.2.0版发布
2017年8月12日WordPress版微信小程序2.2.0版通过了微信的审核正式发布,此版本的更新以完善功能为主.主要更新的功能是:站内链接,猜你喜欢,热点文章. WordPress版微信小程序开放 ...
- linux服务nfs与dhcp篇
nfs复习: 1.简介:用于liunx与linux之间的文件传输系统 2.下载nfs-utils和rpcbind 3.打开配置文件/etc/exports——文件名(目录名)共享给予的ip地址(rw) ...