poj 2187【旋转卡壳】

求平面最远点对

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=50005;
int n,w=1,top;
double ans;
struct dian
{
    double x,y;
    dian(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    dian operator + (const dian &a) const
    {
        return dian(x+a.x,y+a.y);
    }
    dian operator - (const dian &a) const
    {
        return dian(x-a.x,y-a.y);
    }
}p[N],s[N];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>‘9‘||p<‘0‘)
    {
        if(p==‘-‘)
            f=-1;
        p=getchar();
    }
    while(p>=‘0‘&&p<=‘9‘)
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
double dis(dian a,dian b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double cj(dian a,dian b)
{
    return a.x*b.y-a.y*b.x;
}
bool cmp(const dian &a,const dian &b)
{
    int ar=cj(a-p[1],b-p[1]);
    return ar>0||(ar==0&&a.x<b.x);
}
double mj(dian a,dian b,dian c)
{
    return cj(b-a,c-a);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        int x=read(),y=read();
        p[i]=dian(x,y);
        if(p[i].x<p[w].x||(p[i].x==p[w].x&&p[i].y<p[w].y))
            w=i;
    }
    swap(p[w],p[1]);
    sort(p+2,p+1+n,cmp);
    s[++top]=p[1],s[++top]=p[2];
    for(int i=3;i<=n;i++)
    {
        while(top>1&&cj(s[top]-p[i],s[top-1]-p[i])>=0)
            top--;
        s[++top]=p[i];
    }
    w=2;s[top+1]=s[1];
    for(int i=1;i<=top;i++)
    {
        while(cj((s[i]-s[w]),(s[i+1]-s[w]))<cj((s[i]-s[w+1]),(s[i+1]-s[w+1])))
            w=(w+1)>top?1:w+1;
        ans=max(ans,dis(s[w],s[i]));
    }
    printf("%.0f\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/lokiii/p/8503199.html

时间: 2024-08-28 01:59:58

poj 2187【旋转卡壳】的相关文章

POJ 2187 旋转卡壳 + 水平序 Graham 扫描算法

水平序 Graham 扫描算法: 计算二维凸包的时候可以用到,Graham 扫描算法有水平序和极角序两种. 极角序算法能一次确定整个凸包, 但是计算极角需要用到三角函数,速度较慢,精度较差,特殊情况较多. 水平序算法需要扫描两次,但排序简单,讨论简单,不易出错. [算法流程] 1.对顶点按x为第一关键字,y为第二关键字进行排序. 2.准备一个空栈,并将前两个点压入栈. 3.对于每一个顶点A,只要栈顶中还至少两个顶点,记栈顶为T,栈中第二个为U. 若UT(向量) * TA(向量) <= 0, 则将

poj 3608(旋转卡壳求解两凸包之间的最短距离)

Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9768   Accepted: 2866   Special Judge Description Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory

Bridge Across Islands POJ - 3608 旋转卡壳求凸包最近距离

\(\color{#0066ff}{题目描述}\) 几千年前,有一个小王国位于太平洋的中部.王国的领土由两个分离的岛屿组成.由于洋流的冲击,两个岛屿的形状都变成了凸多边形.王国的国王想建立一座桥来连接这两个岛屿.为了把成本降到最低,国王要求你,主教,找到两个岛屿边界之间最小的距离. \(\color{#0066ff}{输入格式}\) 输入由几个测试用例组成. 每个测试用两个整数n,m(3≤n,m≤10000)开始 接下来的n行中的每一行都包含一对坐标,用来描述顶点在一个凸多边形中的位置. 下一条

poj 2187 Beauty Contest——旋转卡壳

题目:http://poj.org/problem?id=2187 学习材料:https://blog.csdn.net/wang_heng199/article/details/74477738 https://www.jianshu.com/p/74c25c0772d6 可以再倒着枚举一遍那样求凸包. 用叉积算面积来旋转卡壳. 注意在面积等于的时候就不要往后走了,不然只有两个点的数据就会死循环. #include<cstdio> #include<cstring> #inclu

poj 2079 Triangle 凸包+旋转卡壳

题意: 给平面上n个点,求这n个点组成的最大三角形面积. 分析: 旋转卡壳,但要注意和求平面最远点对的区别,最大三角形的边不一定在凸包上的,也贴出以前写的求平面最远点对的poj 2187代码作为对比. 代码: //poj 2079 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=50012; struct P { int x,y; }pnt[maxN],cnt[m

POJ 3608 两凸包最近距离 旋转卡壳

Bridge Across Islands Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8071   Accepted: 2364   Special Judge Description Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory

poj 2079 Triangle,旋转卡壳求点集的最大三角形

给出一个点集,求顶点在点集中的最大的三角形面积. 我们知道这三角形的三个点肯定在凸包上,我们求出凸包之后不能枚举,因为题目n比较大,枚举的话要O(n^3)的数量级,所以采用旋转卡壳的做法: 首先枚举三角形的第一个顶点i, 初始化第二个顶点j=i+1和第三个顶点k=j+1,对k进行循环,直到找到第一个k使得cross(i,j,k)>cross(i,j,k+1),如果k==i进入下一次循环. 对j,k进行旋转,每次循环之前更新最大值,然后固定一个j,同样找到一个k使得cross(i,j,k)>cr

hdu 3934&amp;&amp;poj 2079 (凸包+旋转卡壳+求最大三角形面积)

链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8173   Accepted: 2423 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices

POJ 2079 Triangle [旋转卡壳]

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9525   Accepted: 2845 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points. Input

poj 2079 Triangle(旋转卡壳)

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8917   Accepted: 2650 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points. Input