Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 23706 | Accepted: 7244 |
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.
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
* 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)
总结:比较赤裸裸的题目,给出平面上一些点求这些点中欧氏距离最远距离的平方,先求凸包,在用旋转卡壳求凸包上最远两个点的距离。
代码是测试上海交大的模版,其实不用求first和second.
讲解旋转卡壳的连接:http://www.cnblogs.com/xdruid/archive/2012/07/01/2572303.html
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#define next(i) ((i+1)%n)
using namespace std;
//2187 Accepted 3400K 235MS C++ 3015B 2013-05-25 20:14:31
const double eps = 1e-8;
int dcmp(double x) {
if(fabs(x) < eps) return 0;
if(x > 0) return 1;
return -1;
}
struct point {
double x, y;
point() {}
point(double a, double b) : x(a), y(b) {}
friend point operator - (const point a, const point b) {
return point(a.x-b.x, a.y-b.y);
}
};
double det(const point &a, const point &b) {
return a.x * b.y - a.y * b.x;
}
bool comp_less(const point &a, const point &b) {
return dcmp(a.x-b.x)<0 || (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)<0);
}
bool cmp(point &a, point &b) {
if(a.x==b.x && a.y==b.y) return true;
else return false;
}
double dist(const point &a, const point &b) {
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
struct polygon_convex {
vector <point> P;
polygon_convex(int Size = 0) {
P.resize(Size);
}
};
polygon_convex convex_hull(vector<point> a) {
polygon_convex res(2*a.size()+5);
sort(a.begin(), a.end(), comp_less);
///a.erase(unique(a.begin(), a.end()), a.end());
a.erase(unique(a.begin(), a.end(), cmp), a.end());
int m = 0;
for(int i = 0; i < (int)a.size(); ++i) {
while(m > 1 && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0)
--m;
res.P[m++] = a[i];
}
int k = m;
for(int i = int(a.size())-2; i >= 0; --i) {
while(m > k && dcmp(det(res.P[m-1]-res.P[m-2], a[i]-res.P[m-2]))<=0)
--m;
res.P[m++] = a[i];
}
res.P.resize(m);
if(a.size()>1) res.P.resize(m-1);
return res;
}
double convex_diameter(polygon_convex &a, int &First, int &Second) {
vector<point> &p = a.P;
int n = p.size();
double maxd = 0.0;
if(n==1) {
First = Second = 0;
return maxd;
}
int j = 1;
for(int i = 0; i < n; ++i) {
while(dcmp(det(p[next(i)]-p[i], p[j]-p[i])-det(p[next(i)]-p[i], p[next(j)]-p[i]))<0)
{
j = next(j);
}
double d = dist(p[i], p[j]);
if(d > maxd) {
maxd = d;
First = i;
Second = j;
}
d = dist(p[next(i)], p[next(j)]);
if(d>maxd) {
maxd = d;
First = i;
Second = j;
}
}
return maxd;
}
int main()
{
int n;
int first, second;
point tmp;
vector <point> v;
while(scanf("%d", &n) != EOF) {
v.clear();
for(int i = 0; i < n; i++) {
scanf("%lf%lf", &tmp.x, &tmp.y);
v.push_back(tmp);
}
double res = 0.0;
polygon_convex result;
result = convex_hull(v);
res = convex_diameter(result, first, second);
printf("%d\n", (int)res);
}
return 0;
}