1696: [Usaco2007 Feb]Building A New Barn新牛舍

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 378   Solved: 168
[ Submit][ Status][ Discuss]

Description

经过多年的积蓄,农夫JOHN决定造一个新的牛舍。他知道所有N(2 <= N <= 10,000)头牛的吃草位置,所以他想把牛舍造在最方便的地方。 每一头牛吃草的位置是一个整数点(X_i, Y_i) (-10,000 <= X_i <= 10,000; -10,000 <= Y_i <= 10,000)。 没有两头牛的吃草位置是相邻的。 JOHN决定把牛舍造在一个没有牛吃草的整数点上。如果牛舍在(X, Y),在(X_i, Y_i)的牛到牛舍的距离是|X-X_i|+|Y-Y_i|。 JOHN把牛舍造在哪儿才能使所有牛到牛舍的距离和最低?

Input

第1行: 一个数,N。

第2~N+1行:第i+1行 包含第i头牛的位置(X_i, Y_i)。

Output

第1行: 两个数,最小距离和和所有可能达到这个距离和的牛舍位置的数目。

Sample Input

4
1 -3
0 1
-2 1
1 -1

输入解释:

一共有4头牛,位置分别为(1, -3), (0, 1), (-2, 1), 和(1, -1).

Sample Output

10 4

输出解释:
最小距离和是10,可以在牛舍位于 (0, -1), (0, 0), (1, 0), (1, 1)时达到。

对于这种题自然要先想到中位数。。不过一开始傻傻的没考虑不能建在牛身上。。。
如果是奇数,建在中间一个数的位置,偶数在中间两个的任意位置都行
不过偶数舍去区间内刚好有牛的点,如果奇数情况且最优解在牛身上
那么次优解一定在上下左右产生

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<map>
#include<cmath>
using namespace std;const int maxn = 1e4 + 10;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};struct C{int x,y;
}cow[maxn];int x[maxn],y[maxn],n,i,j,a1,a2;int main()
{#ifndef ONLINE_JUDGE#ifndef YZYfreopen(".in","r",stdin);freopen(".out","w",stdout);#elsefreopen("yzy.txt","r",stdin);#endif#endifcin >> n;for (i = 1; i <= n; i++) {scanf("%d%d",&cow[i].x,&cow[i].y);x[i] = cow[i].x; y[i] = cow[i].y;}sort (x + 1,x + n + 1);sort (y + 1,y + n + 1);if (n & 1){int tmp = (n / 2) + 1;for (i = 1; i <= n; i++){if (cow[i].x == x[tmp] && cow[i].y == y[tmp]){int Min = 2E9;for (int l = 0; l < 4; l++){int xx = x[tmp] + dx[l];int yy = y[tmp] + dy[l];int now = 0;for (i = 1; i <= n; i++) now += abs(cow[i].x - xx) + abs(cow[i].y - yy);if (now < Min) Min = now,a2 = 1;else if (now == Min) ++a2;}printf("%d %d\n",Min,a2);return 0;}else a1 += abs(cow[i].x - x[tmp]) + abs(cow[i].y - y[tmp]);}printf("%d 1\n",a1);}else{int t1 = n / 2,t2 = t1 + 1;a2 = (x[t2] - x[t1] + 1) * (y[t2] - y[t1] + 1);for (i = 1; i <= n; i++){a1 += abs(cow[i].x - x[t1]) + abs(cow[i].y - y[t1]);int X = cow[i].x,Y = cow[i].y;if (x[t1] <= X && X <= x[t2] && y[t1] <= Y && Y <= y[t2]) --a2;}printf("%d %d\n",a1,a2);}return 0;
}

1696: [Usaco2007 Feb]Building A New Barn新牛舍相关推荐

  1. bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍

    1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 481  Solv ...

  2. BZOJ[1696][Usaco2007 Feb]Building A New Barn新牛舍 贪心

    题目链接http://www.lydsy.com/JudgeOnline/problem.php?id=1696 分情况讨论: ①如果有奇数个牛,横纵坐标分别取中位数,如果该位置有牛就讨论上下左右(没 ...

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

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

  4. [BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved ...

  5. bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘(BFS)

    1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 550  Solved: 1 ...

  6. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路(最小生成树)

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1709  Solved ...

  7. BZOJ 1631: [Usaco2007 Feb]Cow Party【最短路】

    1631: [Usaco2007 Feb]Cow Party [题目描述] 传送门 题解 正向建边,从X开始刷一趟SPFA,然后反向建边,从X刷一趟SPFA,最后两次的答案加和就可以了. 代码如下 # ...

  8. bzoj 1632: [Usaco2007 Feb]Lilypad Pond(BFS)

    1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 688  Solved: 230 [S ...

  9. 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典(DP)

    1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 633  Solved ...

最新文章

  1. C#进阶系列——WebApi 身份认证解决方案:Basic基础认证
  2. 数据库基本概念 - 表、字段、sql语句
  3. 付费用户减少一成,阅文集团还能否为自己“造血”?
  4. Hortworks Hadoop 2.4.2安装、配置
  5. 屏幕Screen类文件编写
  6. dj鲜生-23-模板抽离-继承的小结
  7. 转一篇矩阵方面有趣的文章
  8. 多多客id是什么意思_【拼多多】关于多多客比价行为规范解释说明
  9. 【知识积累】Edge vs Fog Computing 边缘计算和雾计算的相同与不同
  10. matlab去高斯噪声,[转]MATLAB信号中高斯噪声
  11. 凯撒移位密码python_凯撒密码python拓展程序
  12. 计算机储存容量5mb,笔记本电脑的硬盘上 8455MB(CYL 16383,H16,S63) 640GB (LBA 1,250,263,728Sectors) 分别表示什么意思?...
  13. 默默学Sharding-Sphere(二)
  14. keil遇到FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘解决方法
  15. DeepFlow Large displacement optical flow with deep matching
  16. 爬虫3 requests基础2 代理 证书 重定向 响应时间
  17. Dennard Scaling
  18. 2020年移动发展趋势
  19. Hadoop实战系列之MapReduce 分析 Youtube视频数据
  20. python色卡_python matplotlib:plt.scatter() 大小和颜色参数详解

热门文章

  1. git detached HEAD解决方案(亲测)
  2. 做生意做业务,不想跟这样的女人打交道
  3. java数组从小到大排序_JAVA数组从小到大排序代码
  4. c++——ifndef的作用
  5. 显示异常之黑白屏黑屏不黑
  6. Jenkins: ERROR: Exception when publishing, exception message [Exec timed out or was interrupted aft
  7. 设备树 DTS DTB
  8. Phillip and Trains
  9. Linux3.6.7在OK6410下的移植
  10. 蓝桥杯 ALGO-200 算法训练 预测身高