POJ1113 Wall + CodeForces166B Polygons 凸包问题

第一次写凸包的代码,虽然过了,但是这个原理emmmm...

用的Graham和Andrew算法,记录下来方便以后看吧。。

1.POJ1113  Wall  凸包

就是用的kuangbin的模板,附AC代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<sstream>
#include<fstream>
#include<utility>
using namespace std;
#define ll long long
typedef pair<int,int>pp;
const double pi=acos(-1.0);
const double eps=1e-9;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7ll;

const int MAX=1005;
int n,l;
struct point
{
    double x,y;
    point(){}
    point(double _x,double _y)
    {
        x=_x;y=_y;
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    double operator ^(const point &b)const //叉积
    {
        return x*b.y-y*b.x;
    }
    double operator *(const point &b)const //点积
    {
        return x*b.x+y*b.y;
    }
    void transXY(double B)
    {
        double tx=x,ty=y;
        x=tx*cos(B)-ty*sin(B);
        y=tx*sin(B)+ty*cos(B);
    }
}p[MAX];
int s[MAX],top;
int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    else if(x<0)
        return -1;
    else
        return 1;
}
double dist(struct point a,struct point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(struct point p1,struct point p2)
{
    double tmp=(p1-p[0])^(p2-p[0]);
    if(sgn(tmp)>0)
        return true;
    else if(sgn(tmp)==0&&sgn(dist(p1,p[0])-dist(p2,p[0]))<=0)
        return true;
    else
        return false;
}
void Graham()
{
    int k=0;
    point p0=p[0];
    for(int i=1;i<n;i++)
        if((p0.y>p[i].y)||(p0.y==p[i].y&&p0.x>p[i].x))
        {
            p0=p[i];
            k=i;
        }
    swap(p[k],p[0]);
    sort(p+1,p+n,cmp);
    if(n==1)
    {
        top=1;
        s[0]=0;
        return;
    }
    if(n==2)
    {
        top=2;
        s[0]=0;s[1]=1;
        return;
    }
    top=2;
    s[0]=0;s[1]=1;
    for(int i=2;i<n;i++)
    {
        while(top>1&&sgn((p[s[top-1]]-p[s[top-2]])^(p[i]-p[s[top-2]]))<=0)
            top--;
        s[top++]=i;
    }
    //top--;
}
int main()
{
    while(~scanf("%d%d",&n,&l))
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        sort(p,p+n,cmp);
        Graham();
        double ans=0;
        ans+=2*pi*l;
        for(int i=0;i<top-1;i++)
            ans+=dist(p[s[i]],p[s[i+1]]);
        ans+=dist(p[s[top-1]],p[s[0]]);
        printf("%.0f\n",ans);
		//printf("%d\n",int(res+0.5));
    }
	return 0;
}

2.CodeForces-166B Polygons  凸包+二分

这道题一开始想的是求凸包+判断点是否在凸包上,但是数据量太大了就不行。。

然后贴大佬博客Orz:https://blog.csdn.net/xuh723/article/details/22451957

记录下来这道题主要也是因为学到了一种快速二分查找的函数:lower_bound() 

大佬用了Andrew算法,感觉都差不多吧,但是因为我没学过C++运算符重载,用上面的模板时总是出问题,lower_bound()也总是报错。后来发现两个函数cmp的方式和"<"号的定义方式不一样,因此出现了那么多问题...所以最后还是直接copy大佬的模板了_(:з」∠)_

附上最后AC的代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const double eps=1e-9;
const int INF=0x3f3f3f3f;

struct point
{
    double x,y;
    point(){}
    point(double _x,double _y)
    {
        x=_x;y=_y;
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    bool operator <(const point &b) const
    {
        return x<b.x||x==b.x&&y<b.y;
    }
};
int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    else if(x<0)
        return -1;
    else
        return 1;
}
double cross(point a,point b)
{
       return a.x*b.y-b.x*a.y;
}

const int MAX=2e5+5;
int n,m;
point p[MAX],pb[20005];
point s[MAX];
int top;

void andrew(int n)
{
    sort(p,p+n);
    top=0;
    for(int i=0;i<n;i++)
    {
        while(top>1&&cross(s[top-1]-s[top-2],p[i]-s[top-2])<0)
            top--;
        s[top++]=p[i];
    }
    int k=top;
    for (int i=n-2;i>=0;--i)
    {
        while(top>k&&cross(s[top-1]-s[top-2],p[i]-s[top-2])<0)
            top--;
        s[top++]=p[i];
    }
    if(top>1)
        top--;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        scanf("%d",&m);
        for(int i=n;i<n+m;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            pb[i-n]=p[i];
        }
        andrew(n+m);
        sort(pb,pb+m);
        int i;
        for(i=0;i<top;i++)
        {
            int pos=lower_bound(pb,pb+m,s[i])-pb;//返回第一个大于等于s[i]的pb的下标
            if((sgn(pb[pos].x-s[i].x)==0)&&(sgn(pb[pos].y-s[i].y)==0))
                break;
        }
        if(i==top)
            printf("YES\n");
        else
            printf("NO\n");
    }
	return 0;
}

同时也想说没好好学过C++真是难过啊QAQ...

那个运算符重载。。真是不懂啊555555...

猜你喜欢

转载自blog.csdn.net/Cc_Sonia/article/details/81511020