# 洛谷题目链接:[[USACO07FEB]新牛棚Building A New Barn](https://www.luogu.org/problemnew/show/P2874)

题目描述

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?

给出平面上n个不相邻的点,要求到这n个点的曼哈顿距离之和最小的点的个数ans2,和这个最小距离ans1。

输入输出格式

输入格式:

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.

输入输出样例

输入样例#1:

4

1 -3

0 1

-2 1

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\)个点.要求出\(ans2\)个点使得这\(ans2\)个点到所有点的曼哈顿距离之和,这\(ans2\)个点不能是原平面直角坐标系中给出的点.两点间曼哈顿距离的公式为\(\left|x-x_i\right|+\left|y-y_i\right|\).

既然要到所有点的曼哈顿距离之和最小,那么可以先设答案点坐标为\((x,y)\).可以得到这样一个式子:

\[ans1=\sum{(\left|x-x_i\right|+\left|y-y_i\right|)}
\]

显然我们是要求出一个\((x,y)\)使得\(ans1\)最小,因为\(x\),\(y\)互不影响,所以可以分开处理,那么根据我们的数学知识,可以得知使\(ans1\)最小的值就是\(x\)序列的中位数,同理\(y\)也是序列中的中位数.

这样我们就求出了\(ans1\),但是因为题目的限制,如果\(n\)为奇数时,\(x\)和\(y\)直接求出的中位数有可能是原图中给出的点.所以这时我们要对这个点的上下左右进行判断,对上下左右求一遍最小值.

如果\(n\)为偶数时,那么在\(x[n/2],x[n/2+1],y[n/2],y[n/2+1]\)这四个点所围成的矩形中的所有点都是满足条件的. 先计算出这个矩形中包含的点的个数,然后再将原图中包含的点都一个个删掉.

#include<bits/stdc++.h>
using namespace std;
const int inf=2147483647;
const int N=10000+5; int n, x[N], y[N], ans1 = inf, ans2 = 0;
int dir[]={0,1,0,-1,0}; struct node{
int x, y;
}p[N]; int gi(){
int ans = 0 , f = 1; char i = getchar();
while(i<'0'||i>'9'){if(i=='-')f=-1;i=getchar();}
while(i>='0'&&i<='9'){ans=ans*10+i-'0';i=getchar();}
return ans * f;
} int main(){
cin >> n;
for(int i=1;i<=n;i++) x[i] = gi(), y[i] = gi();
for(int i=1;i<=n;i++) p[i].x = x[i], p[i].y = y[i];
sort(x+1 , x+n+1); sort(y+1 , y+n+1);
if(n & 1){
int a = x[n/2+1], b = y[n/2+1], sum = 0;
for(int i=1;i<=n;i++)
sum += abs(a-x[i])+abs(b-y[i]);
ans1 = sum; ans2 = 1;
for(int i=1;i<=n;i++)
if(p[i].x == a && p[i].y == b) ans1 = inf;
for(int i=0;i<4;i++){
int nx = a+dir[i], ny = b+dir[i+1], sum = 0;
for(int i=1;i<=n;i++)
sum += abs(nx-x[i])+abs(ny-y[i]);
if(sum < ans1) ans1 = sum, ans2 = 1;
else if(sum == ans1) ans2++;
}
}
else{
int x1 = x[n/2], x2 = x[n/2+1];
int y1 = y[n/2], y2 = y[n/2+1], sum = 0;
for(int i=1;i<=n;i++)
sum += abs(x1-x[i])+abs(y1-y[i]);
ans1 = min(ans1 , sum);
ans2 = (x2-x1+1)*(y2-y1+1);
for(int i=1;i<=n;i++)
if(x1<=p[i].x && p[i].x<=x2 && y1<=p[i].y && p[i].y<=y2) ans2--;
}
printf("%d %d\n",ans1,ans2);
return 0;
}

[USACO07FEB]新牛棚Building A New Barn的更多相关文章

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

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

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

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

  3. 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 ...

  4. 【BZOJ】1696: [Usaco2007 Feb]Building A New Barn新牛舍(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1696 原题要求min(sum{|x-xi|+|y-yi|}),且一定要看题:“没有两头牛的吃草位置是 ...

  5. bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 ——中位数排序

    Description 经过多年的积蓄,农夫JOHN决定造一个新的牛舍.他知道所有N(2 <= N <= 10,000)头牛的吃草位置,所以他想把牛舍造在最方便的地方. 每一头牛吃草的位置 ...

  6. BZOJ1696: [Usaco2007 Feb]Building A New Barn新牛舍

    n<=10000个点(xi,yi),找到一个不同于给出的所有点的点,使得该点到所有点的曼哈顿距离最小并找出这样的点的个数. 第一眼看上去这不是中位数嘛,奇数一个点偶数一片,然后找一下这篇区域有几 ...

  7. BZOJ 1696 [Usaco2007 Feb]Building A New Barn新牛舍 数学

    题意:链接 方法:数学+模拟 解析: 首先这类问题不是第一次见了,所以直接知道拿x的中位数.y的中位数. 这题就是讨论情况很的烦. 题中有个限制,给出待求和的点不能选取. 所以假设奇数个点,求出x中位 ...

  8. [USACO17JAN]Building a Tall Barn建谷仓

    题目描述 Farmer John is building a brand new, NNN -story barn, with the help of his KKK cows ( 1≤N≤K≤101 ...

  9. 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 b ...

随机推荐

  1. 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django

    近期开始学习基于Linux平台的Django开发,想配置一台可以发布的服务器,经过近一个月的努力,终于掌握了基于Apache和mod-wsgi插件的部署模式,自己也写了一个教程,一是让自己有个记录,二 ...

  2. 转载:BUG定位

    1.web前端 Web前端就是通常说的网页.互联网公司的前端一般包含如下内容:JavaScript.ActionScript.CSS.HTML(..ML).Flash.交互式设计.视觉设计 web前端 ...

  3. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

  4. abtest-system后台系统设计与搭建

    本文来自网易云社区 作者:刘颂 1 项目背景: 2017年5月:客户端提出增加https&dns以及双cdn业务功能 后台配合实现使用disconf配置 针对不同的域名或者请求配置不同的htt ...

  5. bug单的提交

    顶头信息 所属产品,所属项目,所属模块,影响版本,当前指派,bug类型:代码错误,界面优化,设计缺陷,性能问题,标准规范,其他,安全相关.bug标题,严重程度,优先级 缺陷描述 bug描述,预置条件, ...

  6. redis-Windows下安装与操作

    Redis windows下安装 1.安装 (1)windows把redisbin_x32安装包放在电脑任意的盘里 (2)通过cmd找到对应目录:  D\redisbin_x32 (3)开始安装 D\ ...

  7. c# 把List<T>转成DataTable对象,批量导入Sqlserver库

    /// <summary> /// Sqlbulkcopies the specified SMS.批量插入到数据库 /// </summary> /// <param ...

  8. PAT 甲级 1036 Boys vs Girls(20)

    https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016 This time you are aske ...

  9. Linux挂载Win共享文件夹 一

    1, Win下选中文件夹,属性设置为共享,红圈中的为共享路径,挂载时//ip/共享路径 2, 3,Linux需要安装这两个软件 rpm -qa | grep samba-client rpm -qa ...

  10. Elasticsearch1.x 和Elasticsearch2.x 拼音分词插件lc-pinyin安装教程

    Elasticsearch1.x 基于lc-pinyin和ik分词实现 中文.拼音.同义词搜索 https://blog.csdn.net/chennanymy/article/category/60 ...