题目传送门

  

题目描述

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


  分析:由题意可以得出,ans2=∑(|x-x[i]|+|y-y[i]|),那么很显然越是靠中的点ans2就会越小,那么就要分情况来考虑,如果n是奇数,那么就直接由各个点的中位点来算(也就是排序以后得到一个x[n/2+1],y[n/2+1]),但是要求不能有已经给出的点,所以要在(x,y+1),(x+1,y),(x-1,y),(x,y-1)四个点,即上下左右每个点进行计算和判断。如果n是偶数,那么排序以后得到的是一个2*2的矩阵,那么就在这个矩阵中对每个点进行计算判断。具体看代码。

  Code:

//It is made by HolseLee on 21st Apr 2018
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+;
int n,ans,cnt;
int dx[]={,,,-};
int dy[]={,-,,};
struct Node{
int x,y;
}a[N];
bool cmpx(Node a,Node b)
{return a.x<b.x;}
bool cmpy(Node a,Node b)
{return a.y<b.y;}
inline int Abs(int x)
{return x>?x:-x;}
bool judge(int x,int y)
{
for(int i=;i<=n;i++)
if(a[i].x==x&&a[i].y==y)
return false;
else return true;
}
int getans(int x,int y)
{
int ret=;
for(int i=;i<=n;i++)
ret+=(Abs(a[i].x-x)+Abs(a[i].y-y));
return ret;
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
int x,y;
for(int i=;i<=n;i++)
cin>>a[i].x>>a[i].y;
if(n%==){
sort(a+,a+n+,cmpx);
x=a[n/+].x;
sort(a+,a+n+,cmpy);
y=a[n/+].y;
ans=N<<;cnt=;
for(int i=;i<;i++){
int X=x+dx[i],Y=y+dy[i];
int num=getans(X,Y);
if(num<ans)ans=num,cnt=;
else if(num==ans)cnt++;
}
}
else{
sort(a+,a+n+,cmpx);
int xs=a[n/].x,xe=a[n/+].x;
sort(a+,a+n+,cmpy);
int ys=a[n/].y,ye=a[n/+].y;
cnt=(xe-xs+)*(ye-ys+);
for(int i=;i<=n;i++){
if(a[i].x>=xs&&a[i].y>=ys&&a[i].x<=xe&&a[i].y<=ye)
cnt--;
ans+=(Abs(a[i].x-xs)+Abs(a[i].y-ys));
}
}
cout<<ans<<" "<<cnt<<"\n";
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 wants the ...

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

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

  3. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  4. 洛谷 [P2701] 巨大的牛棚

    首先,本题是一道最大子矩阵问题,且m,n较小,可以使用DP做, 与 洛谷 [P1387]最大正方形 做法相同. #include <iostream> #include <cstdi ...

  5. 洛谷 P2872 [USACO07DEC]道路建设Building Roads 题解

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  6. 洛谷P4301 [CQOI2013]新Nim游戏

    P4301 [CQOI2013]新Nim游戏 题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴. ...

  7. 洛谷 P4301 [CQOI2013]新Nim游戏 解题报告

    P4301 [CQOI2013]新Nim游戏 题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴. ...

  8. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...

  9. 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...

随机推荐

  1. Struts2入门(1)-第一个Struts2程序

    目录结构 C:\WorkSpace\java\StrutsTest\src\main C:\WorkSpace\java\StrutsTest\src\test C:\WorkSpace\java\S ...

  2. 2015/9/9 Python基础(10):文件和输入输出

    文件对象文件对象不仅可以用来访问普通的磁盘文件,而且也可以访问其它任何类型抽象层面上的“文件”.一旦设置了合适的“钩子”,你就可以访问文件类型接口的其它对象,就好像访问的是普通文件一样.文件对象的处理 ...

  3. 用python爬校花网

    import requests import re import hashlib,time def get_index(url): response=requests.get(url) if resp ...

  4. Linux 中使用 dd 测试磁盘性能

    翻译自 : Linux I/O Performance Tests using dd 基本说明 dd 可以用来做简单的低级别复制文件. 这样做, 一般都是可一直直接访问设备文件. 需要说明的是, 错误 ...

  5. 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

    题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...

  6. fileinput 小计(显示历史上传图片)

    今天又需要,要求在选中某条记录后显示历史上传图片 上传控件是fileinput.js 想法:界面有上传图片的控件,重新加载控件,并加入历史上传图片地址 实现代码: var filepathArray ...

  7. 一些达成共识的JavaScript编码风格约定【转】

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要.与其他 ...

  8. 在AndroidStudio中导入开源库 或者jar

    方法一: 先点击Androidstudio中的Project Structure,如图 图1 到如下界面 图2 然后点击+号 图3 选择Library dependency 图4 输入你要的jar包, ...

  9. 【转】ps命令详解

    原文地址:http://apps.hi.baidu.com/share/detail/32573968 有 时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程 ...

  10. deepin 快捷键

    从此脱离鼠标