hdu4311(排序)
来源:互联网 发布:mac没有任何来源 编辑:程序博客网 时间:2024/05/29 08:30
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.
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)
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
46-4 -1-1 -22 -40 20 35 -260 02 0-5 -22 -2-1 24 05-5 1-1 33 13 -11 -110-1 -1-3 2-4 45 25 -43 -14 3-1 -23 4-2 2
Sample Output
26202056
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)
题意:给出很多点,求一个点到所有点的最短距离。
用排序来解决,很巧妙。
#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;typedef long long ll;struct data{ long long x,y; int zb;}a[100010];bool cmpx(data a,data b){ if(a.x!=b.x) return a.x<b.x; return a.y<b.y;}bool cmpy(data a,data b){ if(a.y!=b.y) return a.y<b.y; return a.x<b.x;}long long j(long long &a,long long &b){ if(a>b) return a-b; return b-a;}int n;long long sumy[100010],sumx[100010];int main(){ int t; cin>>t; while(t--) { sumx[0]=sumy[0]=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmpy); for(int i=1;i<=n;i++) { a[i].zb=i; sumy[i]=sumy[i-1]+a[i].y; } sort(a+1,a+n+1,cmpx); for(int i=1;i<=n;i++) sumx[i]=sumx[i-1]+a[i].x; long long ans=999999999999999999; for(int i=1;i<=n;i++) { long long t=0; t+=(i-1)*a[i].x-sumx[i-1]+sumx[n]-sumx[i]-(n-i)*a[i].x; int j=a[i].zb; //cout<<j<<':'<<endl; // cout<<t<<' '; t+=(j-1)*a[i].y-sumy[j-1]+sumy[n]-sumy[j]-(n-j)*a[i].y; //cout<<(j-1)*a[i].y-sumy[j-1]+sumy[n]-sumy[j]-(n-j)*a[i].y<<endl; if(ans>t)ans=t; } cout<<ans<<endl; } return 0;}
0 0
- hdu4311(排序)
- HDU4311(排序+二分)
- hdu4311
- hdu4311
- 某个点到其他点的曼哈顿距离之和最小(HDU4311)
- hdu4311 曼哈顿距离
- hdu4311 Meeting point-1 观察题
- HDU4311 Meeting point-1(曼哈顿距离)
- 排序(希尔排序)
- 排序(堆排序)
- 排序(希尔排序,堆排序,归并排序,快速排序)
- 排序(插入排序,希尔排序,归并排序,快速排序)
- 交换排序(冒泡排序、快速排序)
- 排序之一(插入排序、归并排序)
- 1202 排序(冒泡排序,快速排序)
- 单链表排序(快速排序、归并排序)
- 选择排序、锦标赛排序、堆排序)
- 排序-插入排序(希尔排序)
- 炮弹攻击
- (1)线性查找和二分搜索
- spring异常:FileNotFoundException: Could not open ServletContext resource [/WEB-INF/XX-servlet.xml
- 原码,补码,反码详解
- 由主线程更新UI所想到的Window和RootViewImpl的创建过程
- hdu4311(排序)
- 203. Remove Linked List Elements
- xcoj 1203 F.铺设电缆
- (转)C# 温故而知新:Stream篇(三)
- ZOJ 3913 球缺
- curl with c_ares 编译
- Web Session
- POJ3278 Catch That Cow
- 用java实现实时监控表中的数据,如果有新添加的数据,就通知外部服务器查询这条新增的数据。