poj2187(平面最远点对->旋转卡壳)

旋转卡壳真是难读。。。我就是读作xuan zhuan ka ke了怎么着。。_(:зゝ∠)_(强行改输入法

求最远点对的思路就是旋转卡壳了。。而理解旋转卡壳的最快方法就是看动画23333

旋转卡壳的主要思路就是先把凸包求出来,然后逆时针枚举凸包的每条边,找用最大三角形面积找最远点,由于是逆时针枚举边,所以最远点也是逆时针移动的,这样可以分开枚举,把复杂度降到O(n),然后复杂度还是降在了求凸包上面。。然后在找到最远点的基础上再求一下边端点到最远点的距离进而求出最远点对。。

然后有一个地方坑了我好久。。。初始化最远点要从2开始枚举。。。因为第一条边包含1点和2点,从1开始枚举1点和2点的距离明显到边的距离都是0,就不能顺下去找最远点了。。谨记。。。





#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 10000
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 100005
#define nm 100498
#define pi 3.1415926535897931
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}





struct P{
	int x,y;
	P(int x=0,int y=0):x(x),y(y){}
	int operator*(const P&o){return x*o.y-y*o.x;};
	P operator-(const P&o){return P(x-o.x,y-o.y);}
	bool operator<(const P&o){return x<o.x||(x==o.x&&y<o.y);}
}p[NM],s[NM];
int dis(P o){return sqr(o.x)+sqr(o.y);}
int n,m,ans;

void hull(){
	sort(p+1,p+1+n);
	inc(i,1,n){
		while(m>1&&(s[m]-s[m-1])*(p[i]-s[m-1])<=0)m--;
		s[++m]=p[i];
	}
	int k=m;
	dec(i,n-1,1){
		while(m>k&&(s[m]-s[m-1])*(p[i]-s[m-1])<=0)m--;
		s[++m]=p[i];
	}
	m--;
}

void rorate(){
	int t=2;
	inc(i,1,m){
		while((s[i+1]-s[i])*(s[t+1]-s[i])>(s[i+1]-s[i])*(s[t]-s[i]))t=t%m+1;
		ans=max(ans,dis(s[i]-s[t]));ans=max(ans,dis(s[i+1]-s[t]));
	}
}

int main(){
	//freopen("data.in","r",stdin);
	n=read();
	inc(i,1,n)p[i].x=read(),p[i].y=read();
	hull();
	//inc(i,1,m)printf("%d %d\n",s[i].x,s[i].y);
	rorate();
	return 0*printf("%d\n",ans);
}






Beauty Contest
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 40044 Accepted: 12417

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)

Source

[Submit]   [Go Back]   [Status]   [Discuss]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值