POJ 2187 旋转卡壳第一题

这个题目没什么好说的。昨天做这个题目的时候,卷包裹法有很多心酸,死活过不去,当步进法就能够1A。

看Discuss里有人用旋转卡壳来写。也就学习了一下。这次也用旋转卡壳来写了一下。用凸包(两种方法)+旋转卡壳,都过了~~~!#¥%……&×()

步进法+旋转卡壳

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define EPS 1e-8
#define N 50002
struct point
{
    double x;
    double y;
}p[N];
int s[N];
int top;
int dblcmp(double d)
{
    if(fabs(d)<EPS)return 0;
    return d>0?1:-1;
}
double dis(point a,point b)
{
    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double det(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
    return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
bool cmp(point a,point b)
{
    if(a.y==b.y)return a.x<b.x;
    return a.y<b.y;
}
void graham_scan(int n)
{
    sort(p,p+n,cmp);
    top=1;
    s[0]=0;
    s[1]=1;
    for(int i=2;i<n;i++)
    {
        while(top&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
        {
            top--;
        }
        s[++top]=i;
    }
    int len=top;
    s[++top]=n-2;
    for(int i=n-3;i>=0;i--)
    {
        while(top!=len&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
        {
            top--;
        }
        s[++top]=i;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        if(n==2)
        {
            printf("%.0lf\n",dis(p[0],p[1]));
            continue;
        }
        graham_scan(n);
        int q=1;
        double ans=0;
        for(int i=0;i<top;i++)     //旋转卡壳
        {
            while(cross(p[s[i]],p[s[i+1]],p[s[q+1]])>cross(p[s[i]],p[s[i+1]],p[s[q]]))
            {
                q=(q+1)%top;
            }
            ans=max(ans,max(dis(p[s[i]],p[s[q]]),dis(p[s[i+1]],p[s[q+1]])));
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}

卷包裹法+旋转卡壳

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define EPS 1e-8
#define N 50002
struct point
{
    double x;
    double y;
}p[N];
int s[N];
int top;
int dblcmp(double d)
{
    if(fabs(d)<EPS)return 0;
    return d>0?1:-1;
}
double dis(point a,point b)
{
    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double det(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
    return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
bool cmp1(point a,point b)
{
    if(a.y==b.y)return a.x<b.x;
    return a.y<b.y;
}
bool cmp2(point a,point b)
{
    double d=cross(a,b,p[0]);
    if(d>0)return true;
    else if(d<0)return false;
    else
    {
        double dis1=dis(a,p[0]);
        double dis2=dis(b,p[0]);
        if(dis1<dis2)return true;
        else return false;
    }
}
void graham_scan(int n)
{
    sort(p,p+n,cmp1);
    sort(p+1,p+n,cmp2);
    top=-1;
    s[++top]=0;
    s[++top]=1;
    s[++top]=2;
    for(int i=3;i<n;i++)
    {
        while(top>=1&&dblcmp(cross(p[s[top]],p[i],p[s[top-1]]))<=0)
        {
            top--;
        }
        s[++top]=i;
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        if(n==2)
        {
            printf("%.0lf\n",dis(p[0],p[1]));
            continue;
        }
        graham_scan(n);
        top++;
        s[top]=s[0];
        int q=1;
        double ans=0;
        for(int i=0;i<top;i++)     //旋转卡壳 
        {
            while(cross(p[s[i]],p[s[i+1]],p[s[q+1]])>cross(p[s[i]],p[s[i+1]],p[s[q]]))
            {
                q=(q+1)%top;
            }
            ans=max(ans,max(dis(p[s[i]],p[s[q]]),dis(p[s[i+1]],p[s[q+1]])));
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}
凸包+枚举
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 50005
struct point
{
    double x,y;
}p[N];
int s[N],top;
double dis(point a,point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double det(double x1,double y1,double x2,double y2)
{
    return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
    return det(a.x-c.x,a.y-c.y,b.x-c.x,b.y-c.y);
}
bool cmp(point a,point b)
{
    if(a.y==b.y)return a.x<b.x;
    return a.y<b.y;
}
void graham_scan(int n)
{
    sort(p,p+n,cmp);
    top=1;
    s[0]=0;
    s[1]=1;
    for(int i=2;i<n;i++)
    {
        while(top&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
        {
            top--;
        }
        s[++top]=i;
    }
    int len=top;
    s[++top]=n-2;
    for(int i=n-3;i>=0;i--)
    {
        while(top!=len&&cross(p[s[top]],p[i],p[s[top-1]])<=0)
        {
            top--;
        }
        s[++top]=i;
    }

}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        if(n==2)
        {
            printf("%.0lf\n",dis(p[0],p[1]));
            continue;
        }
        else
        {
            graham_scan(n);
            double dismax=-2147483646;
            for(int i=0;i<top;i++)
            {
                for(int j=i+1;j<=top;j++)
                {
                    if(dismax<dis(p[s[i]],p[s[j]]))
                    {
                        dismax=dis(p[s[i]],p[s[j]]);
                    }
                }
            }
            printf("%.0lf\n",dismax);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值