凸包模板

Andrew:

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define MAXN 10000
void Read(int &x){
    char c;
    while(c=getchar(),c!=EOF)
        if(c>='0'&&c<='9'){
            x=c-'0';
            while(c=getchar(),c>='0'&&c<='9')
                x=x*10+c-'0';
            ungetc(c,stdin);
            return;
        }
}
struct point{
    int x,y;
    point(){
    }
    point(int xx,int yy){
        x=xx,y=yy;
    }
    point operator-(const point&a)const{
        return point(x-a.x,y-a.y);
    }
    bool operator<(const point&a)const{
        if(x==a.x)
            return y<a.y;
        return x<a.x;
    }
}a[MAXN+10];
int s[MAXN+10],n,tp;
double ans;
inline int cross(const point &a,const point &b){
    return a.x*b.y-a.y*b.x;
}
void read(){
    Read(n);
    for(int i=1;i<=n;i++)
        Read(a[i].x),Read(a[i].y);
    sort(a+1,a+n+1);
}
inline double Get_dist(const point &a,const point &b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void andrew(){
    sort(a+1,a+n+1);
    int i,k;
    for(i=1;i<=n;i++){
        while(tp>1&&cross(a[s[tp]]-a[s[tp-1]],a[i]-a[s[tp-1]])<=0)
            tp--;
        s[++tp]=i;
    }
    k=tp;
    for(i=n-1;i;i--){
        while(tp>k&&cross(a[s[tp]]-a[s[tp-1]],a[i]-a[s[tp-1]])<=0)
            tp--;
        s[++tp]=i;
    }
}
void print(){
    printf("%d\n",tp-1);
    int st=1,i;
    for(i=2;i<tp;i++)
        if(a[s[i]].y<a[s[st]].y)
            st=i;
    for(i=st;i<tp;i++)
        printf("%d %d\n",a[s[i]].x,a[s[i]].y);
    for(i=1;i<st;i++)
        printf("%d %d\n",a[s[i]].x,a[s[i]].y);
}
int main()
{
    read();
    andrew();
    print();
}

Graham:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define EPS 1e-8
#define MAXN 100000
using namespace std;
void Read(int &x){
    char c;
    while(c=getchar(),c!=EOF)
        if(c>='0'&&c<='9'){
            x=c-'0';
            while(c=getchar(),c>='0'&&c<='9')
                x=x*10+c-'0';
            ungetc(c,stdin);
            return;
        }
}
struct point{
    double x,y;
    point(){
    }
    point(double x,double y):x(x),y(y){
    }
    point operator-(const point&a)const{
        return point(x-a.x,y-a.y);
    }
    bool operator<(const point&a)const{
        if(x==a.x)
            return y<a.y;
        return x<a.x;
    }
}a[MAXN+10];
inline double cross(const point &a,const point &b){
    return a.x*b.y-a.y*b.x;
}
inline double Get_sqdist(const point &a,const point &b){
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
inline double Get_dist(const point &a,const point &b){
    return sqrt(Get_sqdist(a,b));
}
inline bool cmp(const point &x,const point &y){
    return cross(x-a[1],y-a[1])<0||(cross(x-a[1],y-a[1])<EPS&&Get_sqdist(x,a[1])<Get_sqdist(y,a[1]));
}
int n,s[MAXN+10],tp;
double ans;
void graham(){
    sort(a+1,a+n+1,cmp);
    int i;
    tp=0;
    for(i=1;i<=n;i++){
        while(tp>1&&cross(a[i]-a[s[tp]],a[s[tp]]-a[s[tp-1]])<0)
            tp--;
        s[++tp]=i;
    }
    s[++tp]=1;
    for(i=1;i<tp;i++)
        ans+=Get_dist(a[s[i]],a[s[i+1]]);
}
void read(){
    Read(n);
    int i,mi=1;
    for(i=1;i<=n;i++)
        scanf("%lf%lf",&a[i].x,&a[i].y);
    for(i=1;i<=n;i++)
        if(a[i]<a[mi])
            mi=i;
    swap(a[1],a[mi]);
}
int main()
{
    read();
    graham();
    printf("%.2lf",ans);
}
posted @ 2016-06-26 22:14  outer_form  阅读(114)  评论(0编辑  收藏  举报