题目描述

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

Solution:

  一道数学+贪心的题目,思路正确,代码调了我一下午。

  首先,我们知道两点曼哈顿距离公式:$d=|x1-x2|+|y1-y2|$

  设$S$为题中所求的某个点$(x,y)$到$n$个点的曼哈顿距离之和,即$S=\sum{d}$

  那么有$S=\sum{|x-xi|}+\sum{|y-yi|}$

  容易发现,$x,y$互不影响求值,于是,我们可以将本题中的横纵坐标分开各自计算。

  这样本题就变成了一道中学数学题:求$|x-x1|+|x-x2|+…|x-xn|\;min$,和求$|y-y1|+|y-y2|+…|y-yn|\;min$

  由中学的数学知识可知直接求中位数就行了(可以分类讨论,也可以几何意义解决)。

  那么,我们直接对$x,y$各自排序:

   1、当$n$为奇数时,取$x[n/2+1]$和$y[n/2+1]$,由于题意中不能选给出的点,所以判断:

    若该点为给出的点,则枚举它的上下左右四个点上能求得的最小的$S$,更新$ans1$,然后统计$ans$当且仅当这$4$个点的$S=ans1$;

    若该点不为给出的点,则直接将$ans1$赋为$S$,$ans2$赋为$1$。

   2、当$n$为偶数时,取$x[n/2],x[n/2+1]$和$y[n/2],y[n/2+1]$,显然这$(x[n/2+1]-x[n/2]+1)*(y[n/2+1]-y[n/2]+1)$个点到给定的$n$个点的曼哈顿距离和$S$相等(因为这个矩形中的点的横坐标在$x[n/2],x[n/2+1]$之间,纵坐标也同理),于是枚举矩形中每个点是否是给定的点,求一次$ans1$就$OK$了,先让$ans2$等于这个矩形的点数,每次更新减小就行了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define debug printf("%d %s\n",__LINE__,__FUNCTION__)
using namespace std;
const int N=1e5+;
int n,b[N],c[N],ans1=,ans2;
struct point{int x,y;}a[N];
int dx[]={,-,,},dy[]={,,,-};
il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=a*+x-,x=getchar();
return f?-a:a;
}
il int getsum(int x,int y){
int ans=;
for(int i=;i<=n;i++)ans+=abs(x-b[i])+abs(y-c[i]);
return ans;
}
int main()
{
n=gi();
for(int i=;i<=n;i++)a[i].x=b[i]=gi(),a[i].y=c[i]=gi();
sort(b+,b+n+);
sort(c+,c+n+);
if(n&){
int x=b[(n>>)+],y=c[(n>>)+];
for(int i=;i<;i++){
int xx=x+dx[i],yy=y+dy[i];
int sum=getsum(xx,yy);
if(sum<ans1)ans1=sum,ans2=;
else if(sum==ans1)++ans2;
}
}
else {
int x1=b[(n>>)+],x2=b[n>>],y1=c[(n>>)+],y2=c[n>>];
ans2=(x1-x2+)*(y1-y2+);
ans1=getsum(x1,y1);
for(int i=;i<=n;i++)
if(a[i].x>=x2&&a[i].x<=x1&&a[i].y>=y2&&a[i].y<=y1)ans2--;
}
cout<<ans1<<' '<<ans2;
return ;
}

P2874 [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. [USACO07FEB]新牛棚Building A New Barn

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

  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. filter-policy和AS-PATH-FILTER过滤BGP路由条目

    Filter-policy过滤BGP路由条目 一:根据项目需求搭建好拓扑图如下: 二:配置 1:对项目图做理论分析,首先RT1和RT2属于EBGP(不同自治系统之间的直连路由),而RT2和RT3属于I ...

  2. Sencha Visual Studio(IDE插件)

    Sencha Visual Studio(IDE插件) 首先从官网上下载Visual Studio插件,注意不是VSCode编辑器,下载完后安装打开Visual Studio提示你去注册,输入你的se ...

  3. Cython中def,cdef,cpdef的区别

    这是我的第一篇翻译,希望大家多多给出意见和建议. 如有转载,请注明出处. 原文来自:https://stackoverflow.com/questions/28362009/definition-of ...

  4. 爬虫之爬取斗鱼官网LOL部分主播的状态

    一个爬虫小程序 爬取主播的排名及观看人数 import re import requests import request class Spider(): url = 'https://www.dou ...

  5. Leecode刷题之旅-C语言/python-118杨辉三角

    /* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-t ...

  6. ctf题目writeup(8)

    2019.2.11 南京邮电的ctf平台: 地址http://ctf.nuptzj.cn/challenges# 他们好像搭新的平台了...我注册弄了好半天... 1. 签到题,打开网址: 查看一下页 ...

  7. VGA 时序标准

    VGA 显示器扫描方式从屏幕左上角一点开始,从左像右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT 对电子束进行消隐,每行结束时,用行同步信号进行同步:当扫描完所有的行 ...

  8. python基础之多线程

    概念 进程:进程就是一个程序在一个数据集上的一次动态执行过程 程序:代码 数据集:程序执行过程中需要的资源 进程控制块:完成状态保存的单元 线程:线程是寄托在进程之上,为了提高系统的并发性 线程是进程 ...

  9. Moodle 3.4中添加小组、大组、群

    Moodle在高中应用时经常要用到年级.班级和小组,我们可以用群.大组.小组来代替. 小组设置:网站首页-->现有课程-->右上角的设置按钮-->更多-->用户-->小组 ...

  10. 用Kettle的一套流程完成对整个数据库迁移 费元星

    原地址 :http://ainidehsj.iteye.com/blog/1735434 需求: 1.你是否遇到了需要将mysql数据库中的所有表与数据迁移到Oracle. 2.你是否还在使用kett ...