poj2187:Beauty Contest(凸包+旋转卡壳)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxh759151483/article/details/82955940

Description

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) 

题目的意思就是给一些整数点,让你求距离最远的两个点的距离的平方。

距离最远的两个点一定是凸包上的两个点,所以直接用旋转卡壳算法求凸包上的两点最远距离就可以。这题基本可以算是个模板题,只要改一下dis,改成求距离的平方就可以,变量用long long。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 50005;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
struct point {
	ll x, y;
	point(ll x = 0, ll y = 0):x(x),y(y) {};
};
struct segment { //线段
	point a, b;
};

ll dis(point p1, point p2) {
	return (p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y);   //距离
}
ll cross(point p0,point p1,point p2) {
	return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
ll min(ll a, ll b, ll c) {
	return min(a, min(b, c));
}
ll max(ll a, ll b, ll c) {
	return max(a, max(b, c));
}

/*------------------------------------------------------------------------------------*/

point plist[maxn], pstack[maxn];
bool cmp(const point &p1, const point &p2) {
	ll tmp = cross(plist[0], p1, p2);
	if (tmp > 0)return true;
	else if (tmp == 0 && dis(plist[0], p1) < dis(plist[0], p2))return true;
	else return false;
}

void init(int n) {
	int k = 0;
	scanf("%lld%lld", &plist[0].x, &plist[0].y);
	for (int i = 1; i < n; i++) {
		scanf("%lld%lld", &plist[i].x, &plist[i].y);
		if ((plist[k].y > plist[i].y) || ((plist[k].y == plist[i].y) && (plist[k].x > plist[i].x)))
			k = i;
	}
	swap(plist[0], plist[k]);
	sort(plist+1,plist+n,cmp);
}

int Graham(int n) {
	if(n == 1)
		return 1;
	int top = 1;
	pstack[0] = plist[0];
	pstack[1] = plist[1];
	for (int i = 2; i < n; i++) {
		while (top>0 && cross(pstack[top - 1], pstack[top], plist[i]) <= 0)top--;
		pstack[++top] = plist[i];
	}
	return top + 1;
}

ll convex_minlen(int n) {
	if(n == 1)
		return 0;
	if(n == 2)
		return dis(pstack[0], pstack[1]);
	ll ans = 0;
	int k = 0;
	pstack[n] = pstack[0];
	for (int i = 0; i < n; i++) {
		while(cross(pstack[i], pstack[i + 1], pstack[k]) < cross(pstack[i], pstack[i + 1], pstack[k + 1])) {
			k = (k + 1) % n;
		}
		ll t1 = dis(pstack[i], pstack[k]);
		ll t2 = dis(pstack[i + 1], pstack[k]);
		ans = max(ans, t1, t2);
	}
	return ans;
}
ll convex_maxlen(int n) {
	if(n == 1)
		return 0;
	if(n == 2)
		return dis(pstack[0], pstack[1]);
	ll ans = 0;
	int k = 0;
	pstack[n] = pstack[0];
	for (int i = 0; i < n; i++) {
		while(cross(pstack[i], pstack[i + 1], pstack[k]) < cross(pstack[i], pstack[i + 1], pstack[k + 1])) {
			k = (k + 1) % n;
		}
		ll t1 = dis(pstack[i], pstack[k]);
		ll t2 = dis(pstack[i + 1], pstack[k]);
		ans = max(ans, t1, t2);
	}
	return ans;
}


int main() {
	int n;
	scanf("%d",&n);
	init(n);
	n = Graham(n);
	ll ans = convex_maxlen(n);
	printf("%lld\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sxh759151483/article/details/82955940