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个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...
随机推荐
- 将C#文档注释生成.chm帮助文档(转)
由于最近需要把以前的一个项目写一个文档,但一时又不知道写成怎样的,又恰好发现了可以生成chm的工具,于是乎我就研究了下,感觉还不错,所以也给大家分享下.好了,不多废话,下面就来实现一下吧. 生成前的准 ...
- 存储过程清理N天前数据
CREATE OR REPLACE PROCEDURE APICALL_LOG_INTERFACE_CLEAN ( CLEANDAY IN Number --天数 ) AS v_cleanDay nu ...
- html用jquery获取屏幕宽度与滚动条的关系
当内容高度超过屏幕高度时,获取的屏幕宽度不包括滚动条.即使是浮动,也要显式设置高度,才会全屏. 未超过时,获取的宽度包括滚动条.
- javascript onclick中post提交
对post提交进行封装: function post(URL, PARAMS) { var temp = document.createElement("form"); temp. ...
- android菜鸟学习笔记10----Intent及<intent-filter>
关于Bundle: 注意到Activity的onCreate()方法的签名是protected void onCreate(Bundle savedInstanceState),其参数是一个Bundl ...
- apache vhosts 虚拟主机设置
编辑vhosts文件:/alidata/server/httpd-2.4.10/conf/extra/httpd-vhosts.conf <VirtualHost *:80> <Lo ...
- enumerate next eval reload 内置函数的用法
enumerate next eval reload 内置函数的用法 #enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用 ...
- JDBC通用方法实现
在一些测试项目中会用到纯粹的jdbc操作数据库,下面提供统一的方法实现. import java.sql.CallableStatement; import java.sql.Connection; ...
- 使用log4j将不同级别的日志信息输出到不同的文件中
使用log4j.xml xml格式的配置文件可以使用filter. 例如想只把log4j的debug信息输出到debug.log.error信息输出到error.log,info信息输出到info.l ...
- mysql父子查询
https://segmentfault.com/a/1190000007531328