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=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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=0,sumy=0;for(int i=0;i<n;i++){double x,y;scanf("%lf%lf",&x,&y);p[i].x = (x+y)/2;p[i].y = (x-y)/2;sumx += p[i].x;sumy += p[i].y;p[i].id = i;}sort(p,p+n,cmp1);double tmp=0;for(int i=0;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=0;for(int i=0;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[0];for(int i=0;i<n;i++)Ans = min(Ans,ans[i]);printf("%.0lf\n",Ans);
}

BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离相关推荐

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

    3170: [Tjoi 2013]松鼠聚会 Description 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1. ...

  2. BZOJ3170: [Tjoi2013]松鼠聚会(切比雪夫距离转曼哈顿距离)

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1524  Solved: 803 [Submit][Status][Discuss] Descrip ...

  3. 洛谷P3964 [TJOI2013]松鼠聚会 切比雪夫距离转曼哈顿距离

    https://www.luogu.com.cn/problem/P3964 切比雪夫距离为max(|x-o.x|,|y-o.y|),曼哈顿距离为|x-o.x|+|y-o.y| 当我们要求n个点的到1 ...

  4. 松鼠搬家 ( 切比雪夫距离 到 曼哈顿距离 )

    题意:求切比雪夫距离 直接求不好求,可以转化成曼哈顿距离 切比雪夫: $$ d=max( | x_1-x_2 | , | y_1-y_2 | ) $$ 曼哈顿距离: $$ d=| x_1-x_2 | ...

  5. [BZOJ 3173] [TJOI 2013] 最长上升子序列(splay)

    根据题意很容易得到最原始的做法: #include<iostream> #include<cstdio> #include<cstring> #include< ...

  6. bzoj3170【TJOI2013】松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec   Memory Limit: 128 MB Submit: 965   Solved: 475 [ Submit][ ...

  7. #距离#JZOJ 3256 BZOJ 3170 洛谷 3964 松鼠聚会

    题目 分析 首先这个距离是切比雪夫距离,得把它转换成曼哈顿距离,也就是把(x,y)(x,y)(x,y)变成(x+y2,x−y2)(\frac{x+y}{2},\frac{x-y}{2})(2x+y​, ...

  8. P3964 [TJOI2013]松鼠聚会【切比雪夫距离】

    P3964 [TJOI2013]松鼠聚会 题意:给出nnn个点(xi,yi)(x_i,y_i)(xi​,yi​),找到某个点,使得所有点到该点的切比雪夫距离和最小. 一. A(x1,y1),B(x2, ...

  9. 算法笔记——曼哈顿距离,切比雪夫距离,曼哈顿距离之和 P3964 [TJOI2013]松鼠聚会

    P3964 [TJOI2013]松鼠聚会 题目描述 草原上住着一群小松鼠,每个小松鼠都有一个家.时间长了,大家觉得应该聚一聚.但是草原非常大,松鼠们都很头疼应该在谁家聚会才最合理. 每个小松鼠的家可以 ...

最新文章

  1. Java中的运算神器 BigDecimal,了解一下?
  2. 政企应用如何构筑安全合规的互联内容分发加速?
  3. 使用Axure RP原型设计实践05,了解公式
  4. CST导出farfield远场文件至txt存在的缺陷
  5. 何传启:第六次科技革命的三大“猜想
  6. linux上的web服务器搭建
  7. 使用TweenMax更方便的创建连续的运动。
  8. Java坦克大战代码
  9. 微信小程序底部导航栏中间突出
  10. 筹备酒吧之路——音响篇
  11. 矩阵和向量的求导法则
  12. OAuth2.0的refresh token
  13. java.io.IOException: Cannot create directory /home/app/hadoop-2.4.1/data/dfs/name/current
  14. AndroidStudio入门基础(一)——基础布局
  15. 微信小程序农历阳历日期选择器选中日期同时获取对应农/阳历日期 这个demo问题的修改
  16. AI工程师应聘要具备哪些能力?
  17. 基于参考辐射源/定标的校正算法
  18. Texstudio的下载
  19. Android加密之全盘加密(FDE)
  20. No.053<软考>《(高项)备考大全》【冲刺7】《软考之 119个工具 (5)》

热门文章

  1. JSON 数据重复 出现$ref
  2. 【从零开始】Python字符串的操作方法
  3. 利用CSS、JavaScript及Ajax实现图片预加载的三大方法
  4. Celery框架简单实例
  5. ExtJS4.2学习(10)分组表格控件--GroupingGrid(转)
  6. 在用dw.GetSqlSelect()获得到的Sql语句出现PBSELECT( VERSION的解决办法
  7. NSTimer不准确与GCDTimer详解
  8. bcp文件, 逗号文件
  9. 【笔记篇】C#笔记2
  10. 解决IE6、IE7、Firefox兼容最简单的CSS Hack