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);
}

最新文章

  1. WCF安全3-Transport与Message安全模式
  2. C#创建桌面快捷方式 和 开机启动
  3. CMake编译linux C++
  4. seajs模块压缩问题
  5. WPF之Binding的三种简单写法
  6. c++(重载、覆盖、隐藏)
  7. 吐槽一下Activiti用户手册和一本书
  8. Android IPC通信和AIDL技术应用
  9. JS中实现A*算法寻路
  10. ArcGIS 网络分析[8] ArcObjects二次开发之底层网络分析开发
  11. C# System.Windows.Forms.NumericUpDown 控件全选其中文字
  12. 打造Linux回收站
  13. 面试:Handler 的工作原理是怎样的?
  14. 【Unity Shaders】Transparency —— 透明的cutoff shader
  15. unity 常用插件 2
  16. js 限制输入框只能输入数字的问题
  17. c 字符数组与字符串
  18. go语言之进阶篇Ticker的使用
  19. 一辈子只有1次成为BAT的机会,你如何把握?
  20. OpenTLD在VS2012和opencv246编译通过

热门文章

  1. 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载
  2. ajax请求总是不成功?浏览器的同源策略和跨域问题详解
  3. Oracle EM 不能访问
  4. 前言:关于nagios监控
  5. bzoj 3505 [Cqoi2014]数三角形(组合计数)
  6. 解决A program file was not specified in the launch configuration.问题
  7. SqlServer修改数据库文件及日志文件存放位置
  8. SpringMVC+Spring+Hibernate+Maven+mysql整合
  9. DBMS_ERRLOG记录DML错误日志(二)
  10. UVALive 7464 Robots (贪心)