题目大意

给定n个点,找到一个点使这个点到其他所有点的切比雪夫距离之和最小。

题解

我们知道切比雪夫距离和曼哈顿距离的转化公式

\(1\)表示切比雪夫距离,\(2\)表示曼哈顿距离

我们有:

\(x_1 = x_2 - y_2,y_1 = x_2 + y_2\)

\(x_2 = \frac{x_1 + y_1}{2},y_2 = \frac{x_1 - y_1}{2}\)

所以现在转化成曼哈顿距离了

所以我们直接枚举点即可

什么?你问我怎么计算距离和?

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 100010;
struct Node{
ll x,y;
int id;
}G[maxn];
inline bool cmp1(const Node &a,const Node &b){return a.x < b.x;}
inline bool cmp2(const Node &a,const Node &b){return a.y < b.y;}
ll a[maxn],b[maxn];
int main(){
int n;read(n);
for(int i=1,x,y;i<=n;++i){
read(x);read(y);
G[i].x = (1LL*x+y);
G[i].y = (1LL*x-y);
}sort(G+1,G+n+1,cmp1);
for(int i=1;i<=n;++i){
a[i] = a[i-1] + G[i].x;
G[i].id = i;
}sort(G+1,G+n+1,cmp2);
for(int i=1;i<=n;++i)
b[i] = b[i-1] + G[i].y;
ll ans = 1LL<<60;
for(int i=1;i<=n;++i){
ll x = (a[n] - a[G[i].id]) - (a[G[i].id] - a[G[i].id-1])*(n - G[i].id);
x += (a[G[i].id] - a[G[i].id - 1])*(G[i].id - 1) - (a[G[i].id-1]);
ll y = (b[n] - b[i]) - G[i].y*(n - i);
y += G[i].y*(i-1) - b[i-1];
if(ans > x+y) ans = x+y;
}printf("%lld\n",ans>>1);
getchar();getchar();
return 0;
}

3170: [Tjoi 2013]松鼠聚会的更多相关文章

  1. bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)

    题目链接: 3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MB Description 有N个小松鼠,它们的家用一个点x,y表 ...

  2. BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  3. BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )

    题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x ...

  4. Bzoj 3170[Tjoi 2013]松鼠聚会 曼哈顿距离与切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  5. bzoj 3170: [Tjoi 2013]松鼠聚会

    #include<cstdio> #include<iostream> #include<algorithm> #define M 100008 using nam ...

  6. bzoj 3170 Tjoi 2013 松鼠聚会 曼哈顿距离&&切比雪夫距离

    因为曼哈顿距离很好求,所以要把每个点的坐标转换一下. 转自:http://blog.csdn.net/slongle_amazing/article/details/50911504 题解 两个点的切 ...

  7. BZOJ3170: [Tjoi 2013]松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 531  Solved: 249[Submit][Statu ...

  8. [Tjoi 2013]松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  9. 【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系

    题目描述 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...

随机推荐

  1. 软件工程第2次作业——Visual Studio 2017下基于C/C++的VSTS单元测试实践

    Write one minute, test all day long. 环境确定 IDE:Microsoft Visual Studio 2017 Community 语言:C++ 单元测试工具:V ...

  2. nyist oj 37 回文字符串 (动态规划经典)

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串.从左到右读和从右到左读是全然一样的.比方"aba".当 ...

  3. Go开发常见陷阱

    Go作为一种简便灵巧的语言,深受开发者的喜爱.但对于初学者来说,要想轻松驾驭它,还得做好细节学习工作. 初学者应该注意的地方: 大括号不能独立成行. 未使用变量错误——对于全局变量和函数参数变量,是可 ...

  4. Content encoding error问题解决方法

    A few people have been experiencing the following error. UPDATE: The reason for it happening is beca ...

  5. Android-理解window和windowmanager

    1.window和windowmanager的关系 window是一个抽象类,具体实现为phoneWindow.创建一个window可以通过windowmanager来实现. window的具体实现在 ...

  6. tps 与 事务平均响应时间关系对答(转)

    问者:每秒处理的事务数和事务的平均响应时间 怎么个关系,有关系吗 kaku21:举个例子:一个高速路 有10个入口,每个入口每秒钟只能进1辆车,请问1秒钟最多能进几辆车?? 问者:10 kaku21: ...

  7. 【BZOJ4568】[Scoi2016]幸运数字 倍增+线性基

    [BZOJ4568][Scoi2016]幸运数字 Description A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念 ...

  8. mvn deploy

    mvn:deploy在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享. 以将ojdbc14传到nexus中的thirdparty为例 一 配置 ...

  9. Ajax学习笔记(2)--load()方法

    <head runat="server"> <title></title> <script src="http://localh ...

  10. QQ登录集成到自己网站php代码(转载)

    我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创 ...