3170: [Tjoi 2013]松鼠聚会
题目大意
给定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]松鼠聚会的更多相关文章
- bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)
题目链接: 3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec Memory Limit: 128 MB Description 有N个小松鼠,它们的家用一个点x,y表 ...
- BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离
3170: [Tjoi 2013]松鼠聚会 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )
题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x ...
- Bzoj 3170[Tjoi 2013]松鼠聚会 曼哈顿距离与切比雪夫距离
3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1318 Solved: 664[Submit][Stat ...
- bzoj 3170: [Tjoi 2013]松鼠聚会
#include<cstdio> #include<iostream> #include<algorithm> #define M 100008 using nam ...
- bzoj 3170 Tjoi 2013 松鼠聚会 曼哈顿距离&&切比雪夫距离
因为曼哈顿距离很好求,所以要把每个点的坐标转换一下. 转自:http://blog.csdn.net/slongle_amazing/article/details/50911504 题解 两个点的切 ...
- BZOJ3170: [Tjoi 2013]松鼠聚会
3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 531 Solved: 249[Submit][Statu ...
- [Tjoi 2013]松鼠聚会
3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1318 Solved: 664[Submit][Stat ...
- 【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系
题目描述 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...
随机推荐
- Key-Value键值存储原理初识(NOSQL)
NO-Sql数据库:Not Only不仅仅是SQL 定义:非关系型数据库:NoSQL用于超大规模数据的存储.(例如谷歌或Facebook每天为他们的用户收集万亿比特的数据).这些类型的数据存储不需要固 ...
- Linux系统rootpassword改动
重新启动系统. 进入系统引导界面: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzMzOTg1MQ==/font/5a6L5L2T/fontsi ...
- hibernate QBC查询
HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <> Restrictions.ne() 不等于not equal > Restrict ...
- 【转】【selenium+Python WebDriver】之元素定位不到解决办法
感谢: 煜妃的<Python+Selenium定位不到元素常见原因及解决办法(报:NoSuchElementException)> ClassName定位报错问题:<[Python] ...
- MongoDB水平分片集群(转)
为何需要水平分片 1 减少单机请求数,将单机负载,提高总负载 2 减少单机的存储空间,提高总存空间. 下图一目了然: mongodb sharding 服务器架构 简单注解: 1 mongos 路由进 ...
- Python中的注解“@” 、Java 注解
https://blog.csdn.net/u013474436/article/details/75675113 https://blog.csdn.net/briblue/article/deta ...
- 引用变量的类型强转以及InstanceOf方法的使用
引用到的类: class Person{ String name; } class Student extends Person{ String sut_no; } class ClassMate e ...
- CSS定位细节
记住此三句话,很好的解决了关于浮动方面的问题: 1.浮动的元素对于没有设置浮动的元素来说是不存在的,浮动的元素将会覆盖没有浮动的元素 如图:先让d1浮动在left ,d2位置不浮动,d1覆盖了d2之上 ...
- C#中GroupBox控件的使用(转)
GroupBox(框架)控件是C#中用来组织其他控件形成一个控件组,它的使用方法为[工具箱]->[所有Windows窗体](或者是[容器]列表中)->[GroupBox],拖拽到窗体界面中 ...
- 7-10 括号匹配(25 分) 【STL】
7-10 括号匹配(25 分) 给定一串字符,不超过100个字符,可能包括括号.数字.字母.标点符号.空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配. 输入格式: 输入在一行中给出一行字 ...