3170: [Tjoi 2013]松鼠聚会

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=3170

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

HINT

 

题意

题解:

题目给的切比雪夫距离,转化成曼哈顿距离就好了

然后利用前缀和统计一下就行了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100010
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //§&szlig;§é§à§é¨f§3
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct node
{
double x,y;
int id;
}p[maxn];
double ans[maxn];
bool cmp1(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.y<b.y;
} int main()
{
int n=read();
double sumx=,sumy=;
for(int i=;i<n;i++)
{
double x,y;
scanf("%lf%lf",&x,&y);
p[i].x = (x+y)/;
p[i].y = (x-y)/;
sumx += p[i].x;
sumy += p[i].y;
p[i].id = i;
}
sort(p,p+n,cmp1);
double tmp=;
for(int i=;i<n;i++)
{
ans[p[i].id]+=(i)*p[i].x - tmp;
ans[p[i].id]+=(sumx-tmp)-(n-i)*p[i].x;
tmp+=p[i].x;
}
sort(p,p+n,cmp2);
tmp=;
for(int i=;i<n;i++)
{
ans[p[i].id]+=(i)*p[i].y - tmp;
ans[p[i].id]+=(sumy-tmp)-(n-i)*p[i].y;
tmp+=p[i].y;
}
double Ans = ans[];
for(int i=;i<n;i++)
Ans = min(Ans,ans[i]);
printf("%.0lf\n",Ans);
}

BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离的更多相关文章

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

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

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

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

  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]松鼠聚会

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

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

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

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

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

  7. [Tjoi 2013]松鼠聚会

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

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

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

  9. BZOJ 3170 松鼠聚会(切比雪夫距离转曼哈顿距离)

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

随机推荐

  1. Vagrant搭建Ubuntu-JavaEE开发环境——Tomcat+JDK+MySQL+dubbo+测试

    Vagrant搭建(Tomcat8+JDK7+MySQL5+dubbo) JDK 1.下载jdk 2.解压JDK tar -xzvf jdk-7u79-linux-x64.tar.gz 3.设置环境变 ...

  2. Windows Phone 离主流系统还很远

    调查机构 Kantar Worldpanel 在本月发布全球智能手机份额报告.报告显示,五月份除德国和澳大利亚出现下滑,Windows Phone 的市场份额在不少国家都实现增长. 英国,4.1% 升 ...

  3. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  4. Android UI详解之Fragment加载

    使用Fragment的原因: 1. Activity间的切换不流畅 2. 模块化Activity,方便做局部动画(有时为了到达这一点要把多个布局放到一个activity里面,现在可以用多Fragmen ...

  5. C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作

    C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作 http://blog.csdn.net/jglie/article/details/7394256 十一.上下左右移动光标位 p ...

  6. 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(7)

    检索文档(Retrieving documents) 我们已经有文档存储在我们的实例.现在,让我们尝试检索它们: curl -XGET http://localhost:9200/blog/artic ...

  7. IOS UIScrollView中 使用 touch 无法响应的问题

    添加一个 Category  然后在使用到 UIScrollView 的文件里面 导入这个头文件 就可以 // //  UIScrollView+UITouch.m //  alarm // //  ...

  8. hive UDAF

    java 程序 package com.ibeifeng.udaf; import org.apache.hadoop.hive.ql.exec.UDAF; import org.apache.had ...

  9. [Hive - LanguageManual] DML: Load, Insert, Update, Delete

    LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...

  10. Jquery类级别与对象级别插件开发

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...