POJ2187 Beauty Contest
原题链接:http://poj.org/problem?id=2187
洛谷链接:https://www.luogu.org/problemnew/show/P1452
Beauty Contest
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)
题目大意
求凸包直径的平方
题解
日常求凸包一波,然后使用旋转卡壳求凸包直径。
每次以一条边为基准,用一条与之平行的线去“卡”凸包,卡到对面的点上,这样就可以找到一组点更新直径。在此基础上,我们就不断旋转这两条平行线来“卡”凸包,即可在O(n)的时间找到正解。
代码实现方面,我们只需要不停的以基准边为底求三角形面积最大就可以找到对应的点。
代码
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#define db int
using namespace std;
const int M=5e4+5;
struct pt{db x,y;};
bool operator <(pt a,pt b){return a.x==b.x?a.y<b.y:a.x<b.x;}
pt operator -(pt a,pt b){return (pt){a.x-b.x,a.y-b.y};}
db operator *(pt a,pt b){return a.x*b.y-a.y*b.x;}
db sqr(db x){return x*x;}
db dis(pt a,pt b){return sqr(a.x-b.x)+sqr(a.y-b.y);}
db area(pt a,pt b,pt c){return (b-a)*(c-a);}
int n,r,f,top;
char c;
int read()
{
r=0;f=1;
c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){r=(r<<1)+(r<<3)+c-'0';c=getchar();}
return r*f;
}
pt p[M],sta[M];
void in()
{
n=read();
for(int i=1;i<=n;++i)
p[i].x=read(),p[i].y=read();
}
void tubao()
{
sort(p+1,p+1+n);
for(int i=1;i<=n;++i)
{
while(top>1&&(sta[top]-sta[top-1])*(p[i]-sta[top-1])<=0)--top;
sta[++top]=p[i];
}
int k=top;
for(int i=n-1;i>=1;--i)
{
while(top>k&&(sta[top]-sta[top-1])*(p[i]-sta[top-1])<=0)--top;
sta[++top]=p[i];
}
}
void ac()
{
int ans=0;
if(top==2)printf("%d",dis(sta[1],sta[2])),exit(0);
int j=2;
for(int i=1;i<=top;++i)
{
while(area(sta[i],sta[i+1],sta[j])<area(sta[i],sta[i+1],sta[j+1]))j=(j+1)%top;
ans=max(ans,max(dis(sta[i],sta[j]),dis(sta[i+1],sta[j])));
}
printf("%d",ans);
}
int main()
{
in();tubao();
ac();
return 0;
}
推荐阅读
-
2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记
-
AE/PR怎么使用Beauty Box插件对照片进行磨皮光滑降噪处理?
-
AE/PR怎么使用Beauty Box插件对照片进行磨皮光滑降噪处理?
-
AtCoder Grand Contest 043--A - Range Flip Find Route
-
2020年3月21日Benelux Algorithm Programming Contest 2019
-
共享化妆空间“17 Beauty化妆盒子”完成数百万元天使轮融资
-
2019 Multi-University Training Contest 2: 1010 Just Skip The Problem 自闭记
-
The 2019 Asia Nanchang First Round Online Programming Contest E. Magic Master
-
AtCoder Beginner Contest 173(E 思维模拟 F 容斥 思维题 )
-
CodeForces April Fools Contest 2018题解