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个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...
随机推荐
- vs2010+qt4编译出现error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject等错误
1.当vs2010编译qt时会出现以下错误: 1>------ 已启动全部重新生成: 项目: MyDialog, 配置: Debug Win32 ------ 1>生 ...
- Pentaho Work with Big Data(五)—— 格式化原始web日志
本演示样例说明怎样使用Pentaho MapReduce把原始web日志解析成格式化的记录. 一.向HDFS导入演示样例数据文件 将weblogs_rebuild.txt文件放到HDFS的/user/ ...
- MVC中的 @helper
ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写一个视图模板时,Razor将所需的字符和击键数减少到最小,并保证一个快速.通 ...
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- 让uboot的tftp支持上传功能
转载:http://blog.chinaunix.net/uid-20737871-id-2124122.html uboot下的tftp下载功能是非常重要和常见的功能.但是偶尔有些特殊需求的人需要使 ...
- CGI FASTCGI php-fpm
CGI(Common Gateway Interface) CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工 ...
- Cesium--气泡弹窗
参考资料 首先感谢以下博主们的帮助,本人刚接触Cesium不久,无奈只能拾人牙慧了. 由于cesium没有自带的点击弹出气泡的功能,所以需要自己去开发一个这样的功能,网络上资源很多,看到基本思路都一致 ...
- IDEA下使用Jetty进行Debug模式调试
过程例如以下: (1)找到选项卡中的 –Run– 然后找到 –Edit Configurations (2)点击下图中绿色的plus–找到Maven点进去 (3)依照下边的方式在Command lin ...
- fzu 2039 Pets (简单二分图 + (最大流 || 二分图))
Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There ar ...
- JS实现图片无缝滚动特效;附addEventListener()方法、offsetLeft和offsetWidth属性。
一:html部分 <body> <input id="btn1" type="button" value="向左"> ...