题目链接:

3170: [Tjoi 2013]松鼠聚会

Time Limit: 10 Sec  Memory Limit: 128 MB

Description

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

Input

第一行给出数字N,表示有多少只小松鼠。0<=N<=10^5
下面N行,每行给出x,y表示其家的坐标。
-10^9<=x,y<=10^9

Output

表示为了聚会走的路程和最小为多少。

Sample Input

6
-4 -1
-1 -2
2 -4
0 2
0 3
5 -2

Sample Output

20
 
题意:
 
给出n个点,选出一个点,求其他点到这一点的切比雪夫距离和的最小值;
 
思路:
 
切比雪夫距离可以转化成曼哈顿距离,把原来的坐标系旋转45度,
原来的距离为max{|x1-x2|,|y1-y2|};
旋转后的坐标可以表示为(x1-y1,x1+y1)
2*max{|x1-x2|,|y1-y2|}=|x1-y1-(x2-y2)|+|x1+y1-(x2+y2)|;
曼哈顿距离和的最小值可以x,y排序前缀和;这样就解决了;
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+10;
const int maxn=1e3+20;
const double eps=1e-12; struct node
{
int id;
LL x,y;
}po[N];
int cmp(node a,node b)
{
return a.x<b.x;
}
int cmp1(node a,node b)
{
return a.y<b.y;
}
LL pre[N],nex[N],sumx[N],sumy[N];
int main()
{
int n;
LL x,y;
read(n);
For(i,1,n)
{
read(x);read(y);
po[i].x=x-y;
po[i].y=x+y;
po[i].id=i;
}
sort(po+1,po+n+1,cmp);
for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(i-1)*(po[i].x-po[i-1].x);
for(int i=n;i>0;i--)nex[i]=nex[i+1]+(n-i)*(po[i+1].x-po[i].x);
for(int i=1;i<=n;i++)sumx[po[i].id]=pre[i]+nex[i];
sort(po+1,po+n+1,cmp1);
for(int i=1;i<=n;i++)pre[i]=pre[i-1]+(i-1)*(po[i].y-po[i-1].y);
for(int i=n;i>0;i--)nex[i]=nex[i+1]+(n-i)*(po[i+1].y-po[i].y);
for(int i=1;i<=n;i++)sumy[po[i].id]=pre[i]+nex[i];
LL ans=inf;
for(int i=1;i<=n;i++)ans=min(ans,sumx[i]+sumy[i]);
cout<<ans/2<<endl;
return 0;
}

  

bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)的更多相关文章

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

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

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

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

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

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

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

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

  5. [Tjoi 2013]松鼠聚会

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

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

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

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

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

  8. 3170: [Tjoi 2013]松鼠聚会

    题目大意 给定n个点,找到一个点使这个点到其他所有点的切比雪夫距离之和最小. 题解 我们知道切比雪夫距离和曼哈顿距离的转化公式 \(1\)表示切比雪夫距离,\(2\)表示曼哈顿距离 我们有: \(x_ ...

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

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

随机推荐

  1. java入门基础知识点总结

    JavaScript他是一种描述性语言,其实他并不难学,只要用心学,一定会学好,我相信大家在看这篇文章的时候,一定也学过HTML吧,使用JavaScript就是为了能和网页有更好的交互,下面切入主题. ...

  2. html button自动提交表单问题

    在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit,所以在chrome中,需要使用<button type="butt ...

  3. [CLK Framework] CLK.Threading.PortableTimer - 跨平台的Timer类别

    [CLK Framework] CLK.Threading.PortableTimer - 跨平台的Timer类别 问题情景 开发应用程式的时候,免不了需要加入一些定时执行的设计,例如说:定时更新画面 ...

  4. linux 查看占用内存/CPU最多的进程

    可以使用一下命令查使用内存最多的5个进程 ps -aux | sort -k4nr | head -n 5 或者 top (然后按下M,注意大写) 可以使用一下命令查使用CPU最多的5个进程 ps - ...

  5. 操作iframe

    iframe是在页面中嵌套的子页,当前页面(这里称为父页)和嵌套页面(这里称为子页)可以相互控制: 当父页控制子页用contentWindow,用法为 对象.contentWindow.documen ...

  6. JavaScript基础10——node对象

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 被混淆的C#类库的反编译

    今天看公司以前的代码,用的是.NRT Reactor v4.4.7.5进行的混淆,直接使用.NET Reflector v8.5.0.179 是无法查看的,提示:Invalid number of d ...

  8. 穷举法破解 zebrone1.1

    系统 : Windows xp 程序 : zebrone1.1 程序下载地址 :http://pan.baidu.com/s/1boqVcU7 要求 : 编写注册机 使用工具 :OD 可在看雪论坛中查 ...

  9. i++是否原子操作

    i++是否原子操作 不是原子操作.理由: 1.i++分为三个阶段: 内存到寄存器 寄存器自增 回内存 这三个阶段中间都可以被中断分离开.  2.++i首先要看编译器是怎么编译的, 某些编译器比如VC在 ...

  10. IOS 网络浅析-(十三 SDWebImage 实用技巧)

    IOS 网络浅析-(十三 SDWebImage 实用技巧) 首先让我描述一下为了什么而产生的实用技巧.(在TableView.CollectionView中)当用户所处环境WiFi网速不够快(不能立即 ...