题目描述

草原上住着一群小松鼠,每个小松鼠都有一个家。时间长了,大家觉得应该聚一聚。但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理。

每个小松鼠的家可以用一个点x,y表示,两个点的距离定义为点(x,y)和它周围的8个点(x-1,y)(x+1,y),(x,y-1),(x,y+1).(x-1,y+1),(x-1,y-1),(x+1,y+1),(x+1,y-1)距离为1。

输入输出格式

输入格式:

第一行是一个整数N,表示有多少只松鼠。接下来N行,第i行是两个整数x和y,表示松鼠i的家的坐标

输出格式:

一个整数,表示松鼠为了聚会走的路程和最小是多少。

输入输出样例

输入样例#1:
复制

  1. 6
  2. -4 -1
  3. -1 -2
  4. 2 -4
  5. 0 2
  6. 0 3
  7. 5 -2
输出样例#1: 复制

  1. 20
输入样例#2: 复制

  1. 6
  2. 0 0
  3. 2 0
  4. -5 -2
  5. 2 -2
  6. -1 2
  7. 4 0
输出样例#2: 复制

  1. 15

说明

样例解释

在第一个样例中,松鼠在第二只松鼠家(-1,-2)聚会;在第二个样例中,松鼠在第一只松鼠家(0.0)聚会。

数据范围

30%的数据,0 ≤ N ≤ 1000

100%的数据,0 ≤ N ≤ 100000; −10^9 ≤ x, y ≤ 10^9

首先我们要求的是 切比雪夫距离。

也就是 dis=max( |dx|,|dy|);

看样子可能不太好求解;

想办法转换为 曼哈顿距离;

在(x,y)坐标系中进行变换----> ( (x+y)/2,(x-y)/2 );

可以发现原坐标系中的 切比雪夫距离 就是新坐标系中的 曼哈顿距离 ;

(推一下即可);

那么我们考虑用 曼哈顿距离求解:

∑Mdis(i,k) 即该值最小;

将其变为有序方便处理(不妨设为升序);

即 Mdis(1,i)+Mdis(2,i)+...+Mdis(n,i)

现以X坐标为例:

即 x[ i ]-x[ 1 ]+x[ i ]-x[ 2 ]+...+x[ i+1 ]-x[ i ]+...x[ n ]-x[ i ]

= i*x[ i ]- sum[ i ]+sum[ n ]-sum[ i ]+(n-i)*x[ i ];

那么就可以用前缀和进行维护了;

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<deque>
  15. #include<stack>
  16. #include<functional>
  17. #include<sstream>
  18.  
  19. //#include<cctype>
  20. //#pragma GCC optimize("O3")
  21. using namespace std;
  22. #define maxn 300005
  23. #define inf 0x3f3f3f3f
  24. #define INF 9999999999
  25. #define rdint(x) scanf("%d",&x)
  26. #define rdllt(x) scanf("%lld",&x)
  27. #define rdult(x) scanf("%lu",&x)
  28. #define rdlf(x) scanf("%lf",&x)
  29. #define rdstr(x) scanf("%s",x)
  30. typedef long long ll;
  31. typedef unsigned long long ull;
  32. typedef unsigned int U;
  33. #define ms(x) memset((x),0,sizeof(x))
  34. const long long int mod = 1e9 + 7;
  35. #define Mod 1000000000
  36. #define sq(x) (x)*(x)
  37. #define eps 1e-3
  38. typedef pair<int, int> pii;
  39. #define pi acos(-1.0)
  40. //const int N = 1005;
  41. #define REP(i,n) for(int i=0;i<(n);i++)
  42.  
  43. inline ll rd() {
  44. ll x = 0;
  45. char c = getchar();
  46. bool f = false;
  47. while (!isdigit(c)) {
  48. if (c == '-') f = true;
  49. c = getchar();
  50. }
  51. while (isdigit(c)) {
  52. x = (x << 1) + (x << 3) + (c ^ 48);
  53. c = getchar();
  54. }
  55. return f ? -x : x;
  56. }
  57.  
  58. ll gcd(ll a, ll b) {
  59. return b == 0 ? a : gcd(b, a%b);
  60. }
  61. ll sqr(ll x) { return x * x; }
  62.  
  63. /*ll ans;
  64. ll exgcd(ll a, ll b, ll &x, ll &y) {
  65. if (!b) {
  66. x = 1; y = 0; return a;
  67. }
  68. ans = exgcd(b, a%b, x, y);
  69. ll t = x; x = y; y = t - a / b * y;
  70. return ans;
  71. }
  72. */
  73.  
  74. ll qpow(ll a, ll b, ll c) {
  75. ll ans = 1;
  76. a = a % c;
  77. while (b) {
  78. if (b % 2)ans = ans * a%c;
  79. b /= 2; a = a * a%c;
  80. }
  81. return ans;
  82. }
  83.  
  84. int n;
  85. int x[maxn], y[maxn];
  86. ll ans, tmp, sumx[maxn], sumy[maxn];
  87. struct node {
  88. ll x, y;
  89. }a[maxn];
  90.  
  91. int main()
  92. {
  93. //ios::sync_with_stdio(0);
  94. rdint(n);
  95. for (int i = 1; i <= n; i++) {
  96. int xx, yy;
  97. rdint(xx); rdint(yy);
  98. x[i] = a[i].x = xx + yy; y[i] = a[i].y = xx - yy;
  99. }
  100. sort(x + 1, x + 1 + n); sort(y + 1, y + 1 + n);
  101. for (int i = 1; i <= n; i++)
  102. sumx[i] = sumx[i - 1] + x[i], sumy[i] = sumy[i - 1] + y[i];
  103. ans = 100000000000000000;
  104. for (int i = 1; i <= n; i++) {
  105. int pos = lower_bound(x + 1, x + 1 + n, a[i].x) - x;
  106. tmp = sumx[n] - sumx[pos] - a[i].x*(n - pos) + pos * a[i].x - sumx[pos];
  107. pos = lower_bound(y + 1, y + 1 + n, a[i].y) - y;
  108. tmp += sumy[n] - sumy[pos] - a[i].y*(n - pos) + a[i].y*pos - sumy[pos];
  109. ans = min(ans, tmp);
  110. }
  111. cout << ans / 2 << endl;
  112. return 0;
  113. }

[TJOI2013]松鼠聚会 BZOJ 3170的更多相关文章

  1. 【bzoj3170】[Tjoi2013]松鼠聚会

    3170: [Tjoi2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1670  Solved: 885[Submit][Statu ...

  2. BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离+前缀和

    BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离+前缀和 题意:有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点, ...

  3. [TJOI2013]松鼠聚会 曼哈顿距离

    [TJOI2013]松鼠聚会 luogu P3964 首先容易得到两点间距离是\(max(|x_1-x_2|, |y_1-y_2|)\)(即切比雪夫距离) 然后有个套路:原\((x,y)\)求曼哈顿距 ...

  4. [TJOI2013]松鼠聚会(枚举)

    [TJOI2013]松鼠聚会 题目描述 草原上住着一群小松鼠,每个小松鼠都有一个家.时间长了,大家觉得应该聚一聚.但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理. 每个小松鼠的家可以用一个点x, ...

  5. 洛谷P3964 [TJOI2013]松鼠聚会 [二分答案,前缀和,切比雪夫距离]

    题目传送门 松鼠聚会 题目描述 草原上住着一群小松鼠,每个小松鼠都有一个家.时间长了,大家觉得应该聚一聚.但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理. 每个小松鼠的家可以用一个点x,y表示, ...

  6. BZOJ.3170.[TJOI2013]松鼠聚会(切比雪夫距离转曼哈顿距离)

    题目链接 将原坐标系每个点的坐标\((x,y)\)变为\((x+y,x-y)\),则原坐标系中的曼哈顿距离等于新坐标系中的切比雪夫距离. 反过来,将原坐标系每个点的坐标\((x,y)\)变为\((\f ...

  7. BZOJ 3170 [Tjoi2013]松鼠聚会

    题解:切比雪夫距离转化为曼哈顿距离 枚举源点,横纵坐标互不影响,分开考虑,前缀和优化 横纵分开考虑是一种解题思路 #include<iostream> #include<cstdio ...

  8. 3170: [Tjoi2013]松鼠聚会

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1804  Solved: 968[Submit][Status][Discuss] Descript ...

  9. BZOJ3170: [Tjoi2013]松鼠聚会(切比雪夫距离转曼哈顿距离)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1524  Solved: 803[Submit][Status][Discuss] Descripti ...

随机推荐

  1. python_class21

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @version: 3.5 @author: morgana @lic ...

  2. 10-09C#语言基础

    10-09C#语言基础 第一课 一.新项目的建立:打开Visual studio2012,单击“文件→新建项目→模板isualC# Windows 控制台应用程序→确定”即可.   在新建的项目中,首 ...

  3. 问题:HttpContext.Current.Session;结果:Session与HttpContext.Current.Session到底有什么区别呢?

    我在做练习的时候遇到了这样一个问题,在母版页页面中写入登录和密码修改的js代码,在登录的方法中写 入 HttpContext.Current.Session.Add("UserPwd&quo ...

  4. DAY18-Django之form表单

    构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: <form action="/your-name/" method=" ...

  5. 转载-你应该知道的 RPC 原理

    在校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互联网公司就会发现,公司的系 ...

  6. 基于Flask框架的Python web程序的开发实战 <一> 环境搭建

    最近在看<Flask Web开发基于Python的Web应用开发实战>Miguel Grinberg著.安道译 这本书,一步步跟着学习Flask框架的应用,这里做一下笔记 电脑只安装一个P ...

  7. ubuntu16部署gitlab

    一.gitlab的安装 1. 安装依赖包 $ sudo apt-get update #如无ssh还需安装openssh-server $ sudo apt-get install postfix c ...

  8. sqlplus--spool命令参数详解

    sqlplus--SPOOL参数详解 Spool是Oracle快速导出数据的工具,是sqlplus的指令,不是sql语法里的东西 一.Spool常用的设置set arraysize 5000;  // ...

  9. var_dump — 打印变量的相关信息

    <?php $a = array( 1 , 2 , array( "a" , "b" , "c" )); var_dump ( $a ...

  10. Python 安装 django框架

    1.安装 pip install django 2.创建项目 d:/www/django文件夹下右键->打开dos窗口 输入: python C:\ProgramData\Miniconda3\ ...