您的位置:首页 > 其它

poj2187

2016-07-23 15:34 351 查看
Beauty Contest

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34176 Accepted: 10583
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

求点集最远点对的距离,裸旋转卡壳,但求凸包后暴力可过

#include <iostream>
#include <string>
#include <algorithm>

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>

#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)

using namespace std;
const double eps = 1e-8;
const int MAXN = 1e5 + 5;

struct Point{
double x, y;
Point(){}
Point(const double &a, const double &b) : x(a), y(b) {}
Point operator - (const Point &k)
{ return Point( this->x - k.x, this->y - k.y);}
double operator ^ (const Point &k)
{ return this->x * k.y - this->y * k.x;}
double dis2(const Point &k)
{ return (this->x - k.x) * (this->x - k.x) + (this->y - k.y) * (this->y - k.y);}
bool operator < (const Point &k)
{
if(this->x == k.x)
return this->y < k.y;
return this->x < k.x;
}

};
Point lib[MAXN], ans[MAXN], p0;

bool cmp(Point &a, Point &b)
{
if (fabs((a - p0) ^ (b - p0)) >= eps )
return ((a - p0) ^ (b - p0)) > 0;
return p0.dis2(a) < p0.dis2(b);
}

int getHull(int n, Point ans[])
{
Point a, b;
int len = 0;
stack<Point> sta;

sta.push(lib[0]);
sta.push(lib[1]);

for(int i = 2; i < n; ++i)
{
b = sta.top();
sta.pop();
a = sta.top();
while( sta.size() > 1 && (( lib[i] - b) ^ ( lib[i] - a)) >= 0)
{
b = a;
sta.pop();
a = sta.top();
}

sta.push(b);
sta.push(lib[i]);
}

while(!sta.empty())
{
ans[len++] = sta.top();
sta.pop();
}
return len;
}

void solve()
{
int n;
while(scanf("%d", &n) == 1)
{
int mini = 0;
scanf("%lf%lf", &(lib->x), &(lib->y));
for(int i = 1; i < n; ++i)
{
scanf("%lf%lf", &lib[i].x, &lib[i].y);
if( lib[i] < lib[mini])
mini = i;
}

p0 = lib[mini];
lib[mini] = lib[0];
lib[0] = p0;

sort(lib + 1, lib + n, cmp);

int length = getHull(n, ans);
int a, b = 2, c, d;
double s1(0), s2(1), answer = 0;

for(;b < length - 1; ++b)
{
a = (b + 1);
s1 = fabs( (ans[b] - ans[0]) ^ (ans[b] - ans[1]));
s2 = fabs( (ans[a] - ans[0]) ^ (ans[a] - ans[1]));
if(s2 > s1)
break;
}

for(int i = 0; i < length; ++i)
{
a = (i + 1) % length;
c = (b + 1) % length;
d = (b - 1 + length) % length;

s1 = fabs( (ans[b] - ans[i]) ^ (ans[b] - ans[a]));
s2 = fabs( (ans[c] - ans[i]) ^ (ans[c] - ans[a]));

if(s2 - s1 > eps)
b = c;

answer = MAX( answer, MAX( ans[i].dis2( ans[b] ), ans[a].dis2( ans[b] )));
answer = MAX( answer, MAX( ans[i].dis2( ans[c] ), ans[a].dis2( ans[c] )));
answer = MAX( answer, MAX( ans[i].dis2( ans[d] ), ans[a].dis2( ans[d] )));
}
printf("%.0lf\n", answer);
}
}

int main()
{
solve();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: