poj2187——求最远点对的距离的平方(求凸包+旋转卡壳)

题目链接:http://poj.org/problem?id=2187

Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'Miss Cow World'. As a result, Bessie will make a tour of N (2 <= N <= 50,000) farms around the world in order to spread goodwill between farmers and their cows. For simplicity, the world will be represented as a two-dimensional plane, where each farm is located at a pair of integer coordinates (x,y), each having a value in the range -10,000 ... 10,000. No two farms share the same pair of coordinates.

Even though Bessie travels directly in a straight line between pairs of farms, the distance between some farms can be quite large, so she wants to bring a suitcase full of hay with her so she has enough food to eat on each leg of her journey. Since Bessie refills her suitcase at every farm she visits, she wants to determine the maximum possible distance she might need to travel so she knows the size of suitcase she must bring.Help Bessie by computing the maximum distance among all pairs of farms.
 

Input

* Line 1: A single integer, N

* Lines 2..N+1: Two space-separated integers x and y specifying coordinate of each farm

Output

* Line 1: A single integer that is the squared distance between the pair of farms that are farthest apart from each other.

Sample Input

4
0 0
0 1
1 1
1 0

Sample Output

2

Hint

Farm 1 (0, 0) and farm 3 (1, 1) have the longest distance (square root of 2)

题目翻译:

经过一番挣扎,WNJXYK还是成功逃脱了,他来到了学校的后花园,后花园里有N个草堆 (2 <= N <= 50,000) ,每个草堆用(x,y)的坐标来表示(-10,000 <=x,y<= 10,000),当然两个草堆不可能在同一个坐标。WNJXYK决定躲在其中一个草堆里。

吸取了上题翻车的经验,WNJXYK决定先计算一下自己的躲法安不安全,于是他想知道这些草垛之间,距离最大的两个之间的距离是多少?

Input

第一行一个整数N
接下来N行,每行一对整数x,y,表示坐标

Output

一行,输出最远点对之间距离的平方

 

旋转卡壳模板题。

先用Graham算法求出凸包,然后用旋转卡壳求出最远点对的距离的平方。

关于旋转卡壳的知识可以参考:https://www.cnblogs.com/cjyyb/p/7260913.html

算是模板套模板(代码尾有测试数据)。

#include<iostream>
#include<cmath>
#include<algorithm> 
using namespace std; 
const int maxn = 5e4 + 5;
const double eps=1e-6;
struct Point {
    double x, y;
    Point(double x = 0, double y = 0):x(x),y(y){}
};
typedef Point Vector;
Point lst[maxn];
int stk[maxn], top;
Vector operator - (Point A, Point B){
    return Vector(A.x-B.x, A.y-B.y);
}
int sgn(double x){
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
double Cross(Vector v0, Vector v1) {
    return v0.x*v1.y - v1.x*v0.y;
}
double Dot(Point a,Point b){
	return a.x*b.x+a.y*b.y;
}
double Dis(Point p1, Point p2) { //计算 p1p2的 距离
    return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(Point p1, Point p2) { //极角排序函数 ,角度相同则距离小的在前面
    int tmp = sgn(Cross(p1 - lst[0], p2 - lst[0]));
    if(tmp > 0)
        return true;
    if(tmp == 0 && Dis(lst[0], p1) < Dis(lst[0], p2))
        return true;
    return false;
}
//点的编号0 ~ n - 1
//返回凸包结果stk[0 ~ top - 1]为凸包的编号
void Graham(int n) {
    int k = 0;
    Point p0;
    p0.x = lst[0].x;
    p0.y = lst[0].y;
    for(int i = 1; i < n; ++i) {
        if( (p0.y > lst[i].y) || ((p0.y == lst[i].y) && (p0.x > lst[i].x)) ) {
            p0.x = lst[i].x;
            p0.y = lst[i].y;
            k = i;
        }
    }
    lst[k] = lst[0];
    lst[0] = p0;
    sort(lst + 1, lst + n, cmp);
    if(n == 1) {
        top = 1;
        stk[0] = 0;
        return ;
    }
    if(n == 2) {
        top = 2;
        stk[0] = 0;
        stk[1] = 1;
        return ;
    }
    stk[0] = 0;
    stk[1] = 1;
    top = 2;
    for(int i = 2; i < n; ++i) {
        while(top > 1 && Cross(lst[stk[top - 1]] - lst[stk[top - 2]], lst[i] - lst[stk[top - 2]]) <= 0)
            --top;
        stk[top] = i;
        ++top;
    }
    return ;
}
double Dist2(Point p1, Point p2) { //计算距离的平方
    double ret=Dot(p1-p2,p1-p2);
    return ret;
}
double RotatingCalipers(Point* ch, int m) {//返回平面最大距离的平方
    if(m == 1) return 0.0;
    if(m == 2) return Dist2(ch[0], ch[1]);
    double ret = 0.0;
    ch[m] = ch[0];
    int j = 2;
    for(int i = 0; i < m; ++i) {
        while(Cross(ch[i + 1] - ch[i], ch[j] - ch[i]) < Cross(ch[i + 1] - ch[i], ch[j + 1] - ch[i]))
            j=(j+1)%m;
        ret=max(ret, max(Dist2(ch[j], ch[i]), Dist2(ch[j], ch[i + 1])));
    }
    return ret;
}
int main(){
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++)
			scanf("%lf%lf",&lst[i].x,&lst[i].y);
		Graham(n);
		for(int i=0;i<top;i++)
			lst[i]=lst[stk[i]];
		printf("%.0f\n",RotatingCalipers(lst,top));
	}
	
	return 0;
}
/*
data1:
2
-10000 10000
10000 -10000
Ans1: 800000000

data2:
8
5 15
15 10
0 0
10 0
-5 10
0 10
5 5
-1 6
Ans2:400

data3:
10
5 0
0 0
2 0
6 0
9 0
8 0
1 0
7 0
4 0
3 0
Ans3:81

data4:
10
0 0
10000 0
1 100
2 199
9999 100
9998 199
100 -900
200 -1799
9800 -1799
9900 -900
Ans4:100000000
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值