POJ2187-最远点对->旋转卡壳(怎么开心怎么读)
2016-03-23 15:16
351 查看
给n个点,求最远点对,n<=50000
n^2暴力可以过么- -|| 给了3s
不过据说凸包+n^2暴力可以过,没有卡数据。
然后我写了一下,发现果然数据很弱。
这是裸的凸包+n^2
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cmath>
const int maxn=50000+20;
using namespace std;
struct point
{
int x,y;
point(){}
point(int _x,int _y)
{
x=_x;y=_y;
}
point operator +(const point&b)
{
return point(x+b.x,y+b.y);
}
point operator -(const point&b)
{
return point(x-b.x,y-b.y);
}
point operator *(const int &b)
{
return point(x*b,y*b);
}
};
int cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
int dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
bool cmp(point a,point b)
{
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
int n;
int dis(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
point p[maxn];
point q[maxn];
void solve()
{
sort(p+1,p+n+1,cmp);
int m=0;
for(int i=1;i<=n;i++)
{
while(m>1&&cross(q[m-1]-q[m-2],p[i]-q[m-2])<=0)m--;
q[m++]=p[i];
}
int k=m;
for(int i=n-1;i>=1;i--)
{
while(m>k&&cross(q[m-1]-q[m-2],p[i]-q[m-2])<=0)m--;
q[m++]=p[i];
}
int ans=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
ans=max(ans,dis(q[i],q[j]));
}
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
solve();
}
return 0;
}
不过跑的最快的应该就是凸包+旋转卡壳了。
对于一个凸多边形,我们可以尝试用两条相平行但反向的有向直线去夹,一定能够夹住。 夹住的两个点为对踵点。
我们可以看成是对于每一条边,都找到一个最高的点,使得和它构成的三角形面积最大。 然后再转动这个边,如果是逆时针转的,那么新的最高点也会向逆时针转,因为两条平行线转动方向需要一致。利用这个性质我们就可以从上一次找到的点枚举,就降低了复杂度。(一般优化到O(n))
旋转卡壳的代码其实很短:
然后就是求最远点对的模版:
n^2暴力可以过么- -|| 给了3s
不过据说凸包+n^2暴力可以过,没有卡数据。
然后我写了一下,发现果然数据很弱。
这是裸的凸包+n^2
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cmath>
const int maxn=50000+20;
using namespace std;
struct point
{
int x,y;
point(){}
point(int _x,int _y)
{
x=_x;y=_y;
}
point operator +(const point&b)
{
return point(x+b.x,y+b.y);
}
point operator -(const point&b)
{
return point(x-b.x,y-b.y);
}
point operator *(const int &b)
{
return point(x*b,y*b);
}
};
int cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
int dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
bool cmp(point a,point b)
{
if(a.x!=b.x)return a.x<b.x;
return a.y<b.y;
}
int n;
int dis(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
point p[maxn];
point q[maxn];
void solve()
{
sort(p+1,p+n+1,cmp);
int m=0;
for(int i=1;i<=n;i++)
{
while(m>1&&cross(q[m-1]-q[m-2],p[i]-q[m-2])<=0)m--;
q[m++]=p[i];
}
int k=m;
for(int i=n-1;i>=1;i--)
{
while(m>k&&cross(q[m-1]-q[m-2],p[i]-q[m-2])<=0)m--;
q[m++]=p[i];
}
int ans=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
ans=max(ans,dis(q[i],q[j]));
}
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
solve();
}
return 0;
}
不过跑的最快的应该就是凸包+旋转卡壳了。
对于一个凸多边形,我们可以尝试用两条相平行但反向的有向直线去夹,一定能够夹住。 夹住的两个点为对踵点。
我们可以看成是对于每一条边,都找到一个最高的点,使得和它构成的三角形面积最大。 然后再转动这个边,如果是逆时针转的,那么新的最高点也会向逆时针转,因为两条平行线转动方向需要一致。利用这个性质我们就可以从上一次找到的点枚举,就降低了复杂度。(一般优化到O(n))
旋转卡壳的代码其实很短:
void qiake(int n) { int pos=1; int ans=0; for(int i=0;i<n-1;i++) { while(cross(q[i+1]-q[i],q[pos]-q[i])<cross(q[i+1]-q[i],q[pos+1]-q[i]))pos=(pos+1)%n; ans=max(ans,dis(q[i],q[pos])); ans=max(ans,dis(q[i+1],q[pos])); } printf("%d\n",ans); }看出来了,其实是利用了叉积计算三角形面积,使得某一个点的和边构成的三角形面积最大。
然后就是求最远点对的模版:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cmath>
const int maxn=50000+5;
using namespace std;
struct point
{
int x,y;
point(){}
point(int _x,int _y)
{
x=_x;
y=_y;
}
point operator +(const point&b)
{
return point(x+b.x,y+b.y);
}
point operator -(const point&b)
{
return point(x-b.x,y-b.y);
}
point operator *(const int &b)
{
return point(x*b,y*b);
}
};
int dis(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
int dot(point a,point b)
{
return a.x*b.x+a.y*b.y;
}
bool cmp(point x,point y)
{
if(x.x!=y.x)return x.x<y.x;
else return x.y<y.y;
}
point q[maxn];
point p[maxn];
int len;
void qiake(int n) { int pos=1; int ans=0; for(int i=0;i<n-1;i++) { while(cross(q[i+1]-q[i],q[pos]-q[i])<cross(q[i+1]-q[i],q[pos+1]-q[i]))pos=(pos+1)%n; ans=max(ans,dis(q[i],q[pos])); ans=max(ans,dis(q[i+1],q[pos])); } printf("%d\n",ans); }
int n;
int solve()
{
sort(p+1,p+n+1,cmp);
int m=0;
for(int i=1;i<=n;i++)
{
while(m>1&&cross(q[m-1]-q[m-2],p[i]-q[m-2])<=0)m--;
q[m++]=p[i];
}
int k=m;
for(int i=n-1;i>=1;i--)
{
while(m>k&&cross(q[m-1]-q[m-2],p[i]-q[m-2])<=0)m--;
q[m++]=p[i];
}
if(n>1)m--;
return m;
}
point t[maxn];
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].y);
len=solve();
qiake(len);
}
return 0;
}
相关文章推荐
- 总结一下最近做的计算几何学到的知识
- 初学算法 - 求凸包的Garham's Scan算法的C++实现
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)