题目传送门

  

题目描述

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. 2015/8/26 Python基础(1):基本规则及赋值

    Python有如下的基本规则: #后表示注释 \n是行分隔符 \是继续上一行,将过长语句分开 :分号将两个语句连接在一行中 :冒号将代码头和体分开 代码块用缩进块的方式体现 不同缩进深度分隔不同的代码 ...

  2. 【设计模式】 模式PK:装饰模式VS适配器模式

    1.概述 装饰模式和适配器模式在通用类图上没有太多的相似点,差别比较大,但是它们的功能有相似的地方:都是包装作用,都是通过委托方式实现其功能.不同点是:装饰模式包装的是自己的兄弟类,隶属于同一个家族( ...

  3. 【BZOJ】1552/3506 [Cerc2007]robotic sort

    [算法]splay [题解] splay维护序列,用权值(离散化)作为编号.每次找第i小的话直接找对应编号splay即可. 但是这样splay没有下传翻转标记?直接暴力找到路径然后从根到改结点push ...

  4. php trait 变量类型为数组时 不能被父类子类同时use

    直接上代码 --------------------------- trait T1 { public static $a=1; public static $b= []; public static ...

  5. Remmarguts' Date(POJ2449+最短路+A*算法)

    题目链接:http://poj.org/problem?id=2449 题目: 题意:求有向图两点间的k短路. 思路:最短路+A*算法 代码实现如下: #include <set> #in ...

  6. bzoj 1296 DP

    对于每一行做DP预处理,w[i][j]代表这一行前i个刷j次的最大价值,那么w[i][j]=max(w[i][j],w[k][j-1]+sum[k+1][i]),sum[i][j]为i-j段刷一次最多 ...

  7. 4.0docker部署

    设置容器的端口映射 -P  :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...

  8. 分类算法:决策树(C4.5)(转)

    C4.5是机器学习算法中的另一个分类决策树算法,它是基于ID3算法进行改进后的一种重要算法,相比于ID3算法,改进有如下几个要点: 1)用信息增益率来选择属性.ID3选择属性用的是子树的信息增益,这里 ...

  9. php中的__call()函数重载

    <?php #调用类中没有的方法时, 会自动调用__call方法重载 #第一个参数是调用时的方法名, 第二个参数为参数组成的数组 class Cat{ public function Hello ...

  10. Intel call指令

    转载:http://blog.ftofficer.com/2010/04/n-forms-of-call-instructions/ 最近有一个需求,给你个地址,看看这个地址前面是不是一个CALL指令 ...