Meeting point-1
Problem Description
It has been ten years since TJU-ACM established. And in this year all the retired TJU-ACMers want to get together to celebrate the tenth anniversary. Because the retired TJU-ACMers may live in different places around the world, it
may be hard to find out where to celebrate this meeting in order to minimize the sum travel time of all the retired TJU-ACMers.
There is an infinite integer grid at which N retired TJU-ACMers have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, only 4 adjacent cells are reachable in 1 unit of time.
Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1).
Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
Input
The first line is an integer T represents there are T test cases. (0<T <=10)
For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0<n<=100000), the following n lines each contains two integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <= 10^9)
Output
For each test case, output the minimal sum of travel times.
Sample Input
4
6
-4 -1
-1 -2
2 -4
0 2
0 3
5 -2
6
0 0
2 0
-5 -2
2 -2
-1 2
4 0
5
-5 1
-1 3
3 1
3 -1
1 -1
10
-1 -1
-3 2
-4 4
5 2
5 -4
3 -1
4 3
-1 -2
3 4
-2 2
Sample Output
26
20
20
56
Hint
In the first case, the meeting point is (-1,-2); the second is (0,0), the third is (3,1) and the last is (-2,2)
阿弥陀佛 这个题真心没思路 一开始 居然能这样过 按x排序 从中间往2边找250个 就可以了 就能找到那个点
以后要是出不了题 就大胆的试试
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
struct point{long long x,y;}a[MAXN];
bool cmp(point A,point B)
{
if(A.x<B.x) return true;
if(A.x==B.x&&A.y<B.y) return true;
return false;
}
long long ABS(long long A)
{
if(A<0) A=-A;
return A;
}
long long work(int k,int n)
{
int i;
long long ans=0;
for(i=0;i<n;i++)
{
ans+=ABS(a[k].x-a[i].x);
ans+=ABS(a[k].y-a[i].y);
}
return ans;
}
int main()
{
int test,n,s,e,i;
long long ans,t;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
s=n/2-250;e=n/2+250;
if(s<0) s=0;
if(e>n-1) e=n-1;
ans=1;ans<<=60;
for(i=s;i<=e;i++)
{
t=work(i,n);
if(ans>t) ans=t;
}
cout<<ans<<endl;
}
return 0;
}
分享到:
相关推荐
教程来自HDU 的ACM教案,非常好的一个资料
教程来自HDU 的ACM教案,非常好的一个资料
可拆卸核心板滤波电容电源指示灯排针复位F103--R10不焊F207--R9不焊第19引脚F103--C4焊0欧姆,C3不焊Tuesday, August 31
ACM HDU 2000->2099 解题报告 ACM HDU 2000->2099 解题报告 ACM HDU 2000->2099 解题报告
排母,核心板接口ADC 电位器扩展接口,预留模拟量Tuesday, August 31, 2021Tuesday, August 31, 2021Tuesday
基础算法类 ACM 入门 杭电OJ 11页题目题解,算法入门的时候刷题可以参考 搜集整理起来了比单个去搜题解方便快捷
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
ACM题库,一些题目和答案,以及解题报告,传上来共享
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
第一章 功能简介本章介绍HDU-EID-V2开发板的功能,使大家对该开发板的功能及特点有个基本了解。1. 处理器( MCU)HDU-EID-V2 开发板的处理器
杭电ACM2000-2099题的解题报告
HDU 里面的2000~2099道题目的源码。谢谢支持
杭电OnlineJudge 200-2099的解题报告
很多经典的杭电oj与poj习题的ac代码与详解!全部ac,决对没有错误的代码!
HDU 1010-2500解题报告,ACMer可以借鉴一下
很适合ACM初学者的文档, 题目,代码,解体思路一应俱全
hdu2000-2014ac代码,虽然只有几道,但都是简单的