您的位置:首页 > 其它

POJ 2187 Beauty Contest (凸包)

2016-08-05 22:58 330 查看
Beauty Contest

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 34699 Accepted: 10729
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) 

题意:

这道题我不想说话,谢谢(被坑的不行)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
const double eps = 1e-10;

const int MAXN = 5e4 + 5;

struct o{
double x, y;
o(){}
o(double x, double y):x(x), y(y){}
double add(double a, double b){
if(abs(a + b) < eps * (abs(a) + abs(b))) return 0;
return a + b;
}

o operator - (o p){
return o(add(x, -p.x), add(y, -p.y));
}

o operator + (o p){
return o(add(x, p.x), add(y, p.y));
}

double operator * (o p){
return add(x * p.x, y * p.y);
}

double operator ^ (o p){
return add(x * p.y, -y * p.x);
}

double dist(o p){
return (*this - p) * (*this - p);
}

bool operator < (const o &p) const{
if(x == p.x) return y < p.y;
return x < p.x;
}
}PS[MAXN];
int n;

vector<o> convex_hull(){
sort(PS, PS + n);
int k = 0;
vector<o> QS(n * 2);
for(int i = 0;i < n;i ++){
while(k > 1 && ((QS[k - 1] - QS[k - 2]) ^ (PS[i] - QS[k - 1])) <= 0) k --;
QS[k ++] = PS[i];
}

for(int i = n - 2, t = k; i >= 0;i --){
while(k > t && ((QS[k - 1] - QS[k - 2]) ^ (PS[i] - QS[k - 1])) <= 0) k --;
QS[k ++] = PS[i];
}
QS.resize(k - 1);
return QS;
}

void solve(){
vector<o> QS = convex_hull();
int m = QS.size();
if(m == 2){
printf("%.0f\n", QS[0].dist(QS[1]));
return;
}
int i = 0, j = 0;
for(int k = 0;k < m;k ++){
if(!(QS[i] < QS[k])) i = k;
if(QS[j] < QS[k]) j = k;
}
double res = 0;
int si = i, sj = j;
while(i != sj || j != si){
res = max(res, QS[i].dist(QS[j]));
if(((QS[(i + 1) % m] - QS[i]) ^ (QS[(j + 1) % m] - QS[j])) < 0){
i = (i + 1) % m;
}
else{
j = (j + 1) % m;
}
}
printf("%.0f\n", res);
}
#define FIN freopen("input.txt", "r",stdin)
int main(){
//FIN;
while(~scanf("%d", &n)){
for(int i = 0;i < n;i ++){
scanf("%lf%lf", &PS[i].x, &PS[i].y);
}
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: