LK的旅行
时间限制:
2000 ms | 内存限制:
65535 KB
难度:
5
-
描述
-
LK最近要去某几个地方旅行,她从地图上计划了几个点,并且用笔点了出来,准备在五一假期去这几个城市旅行。现在希望你找出她点的所有的点中距离最远的两个点的距离是多少。各个景点可以认为是在一个平面上。
-
输入
- 第一行有一个整数0<n<10表示测试数据的组数随后的n组数据中,第一行有一个整数3<m<100000表示有m个旅游景点。随后的m行每行有两个整数,分别表示每一个点的x和y。景点坐标中可能有重复的,0<=x,y<=10000) 输出
- 每组数据输出距离最远的点对的距离的平方. 样例输入
-
1 4 0 0 1 1 0 1 1 0
样例输出
-
2
来源
- POJ 上传者
iphxer
思路:
这道题要求求出距离最远的点的距离的平方,用到凸包思想,可以想到距离最远的点必然是凸包上的两点;如若不然想通过算出所有点之间的距离在平方会TE。运用凸包思想相当于就是缩短了需要遍历的点。
代码:
#include<bits/stdc++.h> using namespace std; struct point {int x,y; }a[100005],s[100005]; int cmp(point a,point b) {if(a.x==b.x) return a.y<b.y;else return a.x<b.x; } double distance_1( point a, point b)//两点之间距离平方公式 {return (pow(a.x-b.x,2)+pow(a.y-b.y,2)); } bool judge(point a, point b, point c)//ab×ac叉乘 {int bax = b.x-a.x;int bay = b.y-a.y;int cax = c.x-a.x;int cay = c.y-a.y;if(bax*cay - bay*cax > 0)return true;return false; } int main() {int n,m;scanf("%d",&n);while(n--){scanf("%d",&m);for(int i=0;i<m;i++)scanf("%d %d",&a[i].x,&a[i].y);sort(a,a+m,cmp);double sum;//sum=distance_1(a[0],a[m-1]);// cout<<sum<<endl;int top=0;for(int i=0;i<m;i++){while(top>=2&&!judge(s[top-2],s[top-1],a[i]))top--;s[top++]=a[i];}int t=top-1;for(int i=m-1;i>=0;i--){while(top>=t+2&&!judge(s[top-2],s[top-1],a[i]))top--;s[top++]=a[i];}--top;sum=-1;for(int i=0;i<top;i++){for(int j=i;j<top;j++){double l=distance_1(s[i],s[j]);if(l>sum) sum=l;}}printf("%.0lf\n",sum);}return 0; }