HDU 4311 Meeting point-1 简单几何
来源:互联网 发布:淘宝商城男装牛仔裤 编辑:程序博客网 时间:2024/04/19 13:52
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.
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)
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
26202056
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)
题意:给你一些点,找到一点使其他点到该点的曼哈顿距离最小,(曼哈顿距离是指只有垂直和竖直方向)
思路:直接暴力求解,遍历各个点,依次把他们作为所求点,再找最小值即可,data[i].x,data[i].y为第i个点的坐标。data[i].sum存储其他点到该点的曼哈顿距离,找最小值即可,那只要确定个点到该点的曼哈顿距离就行了
data[i].sum=(i-1)*data[i].x-(data[1].x~data[i-1].x累加)+(data[i+1].x~data[n].x累加)-(n-i)*data[i].x
这是水平方向的距离
竖直方向同理
然后找到最小值即可:
ac代码
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;struct point{ long long x; long long y; long long sum;}data[100001];bool cmpx(point a,point b){ return a.x<b.x;}bool cmpy(point a,point b){ return a.y<b.y;}int main(){ int t,d; scanf("%d",&t); while(t--) { scanf("%d",&d); for(int i=1;i<=d;i++) scanf("%lld%lld",&data[i].x,&data[i].y); sort(data+1,data+d+1,cmpx); long long sum1=0; for(int i=1;i<=d;i++) { data[i].sum=(i-1)*data[i].x-sum1; sum1+=data[i].x; } sum1=0; for(int i=d;i>=1;i--) { data[i].sum+=sum1-(d-i)*data[i].x; sum1+=data[i].x; } sort(data+1,data+d+1,cmpy); sum1=0; for(int i=1;i<=d;i++) { data[i].sum+=(i-1)*data[i].y-sum1; sum1+=data[i].y; } sum1=0; for(int i=d;i>=1;i--) { data[i].sum+=sum1-(d-i)*data[i].y; sum1+=data[i].y; } long long ans=data[1].sum; for(int i=2;i<=d;i++) ans=min(ans,data[i].sum); printf("%lld\n",ans); } return 0;}
当时做这道题时一直不过,后来才发现犯了个zz的错误,在结构体外面定义了一个sum[100]储存结果但是忽略了一旦排序后,就不再对应原来那个数的储存值了
- HDU 4311 Meeting point-1 简单几何
- HDU 4311 Meeting point-1
- hdu 4311 Meeting point-1
- hdu 4311 Meeting point-1
- HDU 4311 Meeting point-1
- HDU 4311Meeting point-1
- hdu 4311 Meeting point-1 #manhattan距离
- hdu - 4311 - Meeting point-1 - 想法题
- hdu 4311 Meeting point-1(3级)
- hdu 4311 - Meeting point-1(预处理)
- HDU 4311 Meeting point-1 (思维)
- HDU 4311——Meeting point-1(暴力)
- HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2 曼哈顿距离 与 切比雪夫距离
- HDOJ-4311 Meeting point-1
- hdu 4312 Meeting point-2
- 水题:meeting point-1
- Meeting point-1
- HDU 4311&2 Meeting point-1&2(曼哈顿距离&&切比雪夫距离)
- 每个计算机工程师都应该知道的数字列表
- 优化iOS程序性能的25个方法
- 获取urllib2.urlopen失败时的错误页面
- Hive 1.2.1 安装测试
- tensorflow安装的bug
- HDU 4311 Meeting point-1 简单几何
- peer启动过程源码分析
- 30本必读科普
- 编写bootloader(未完)
- 不用再写RecyclerView的Adapter了,一个Adapter搞定
- 三种三栏网页宽度自适应布局方法
- 【Java学习之代码学习】 Prog35_矩阵对角线元素之和
- 大型分布式网站架构技术总结
- 《戏说春秋》第七关 三令五申