Light OJ 1239 计算凸包周长

看懂题目大意后,和POJ1113非常像,求一个凸包周长加上圆的周长
建立凸包,求周长,出结果即可,注意精度,不过用double比较保险

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps=1e-10;
const double PI=acos(-1);
const int maxn=1e+5;
int n,top;
double d;
int sgn(double x){
    if(fabs(x)<eps)
        return 0;
    return x>0 ?1:-1;
}
struct point{
    double x,y;
    point(){}
    point(double _x,double _y){
        x=_x;
        y=_y;
    }
};
typedef point vector;
vector operator - (point a,point b){
    return point(a.x-b.x,a.y-b.y);
}
double operator ^ (point a,point b){
        return a.x*b.y-a.y*b.x;
}
bool operator < (point a,point b){
    return a.x<b.x || a.x==b.x && a.y<b.y;
}
bool operator == (point a,point b){
    return a.x==b.x && a.y==b.y;
}
point p[maxn],stack[maxn];
double dis(point a,point b){
    return hypot(a.x-b.x,a.y-b.y);
}
bool cmp(point a,point b){
    int d=sgn((a-p[0])^(b-p[0]));
    return d>0 || d==0 && sgn(dis(a,p[0])-dis(b,p[0]))<0;
}
void graham(){
    if(n==1){
        top=1;
        stack[0]=p[0];
        return ;
    }
    else if(n==2){
        top=2;
        stack[0]=p[0];stack[1]=p[1];
        if(stack[0]==stack[1])
            top--;
    }
    top=2;
    stack[0]=p[0];stack[1]=p[1];
    for(int i=2;i<n;i++){
        while( top>1 && sgn((stack[top-1]-stack[top-2])^(p[i]-stack[top-2]))<=0)
            top--;
        stack[top++]=p[i];
    }
    //cout<<top<<endl;
}
double convex_perimeter(){
    double sum=0;
    for(int i=0;i<top;i++)
        sum+=dis(stack[i],stack[(i+1)%top]);
    return sum;
}
double solve(){
    double perimeter;
    for(int i=0;i<n;i++)
        if(p[i]<p[0])
            swap(p[0],p[i]);
    sort(p+1,p+n,cmp);
    /*for(int i=0;i<n;i++)
        printf("(%lf,%lf),",p[i].x,p[i].y);
    cout<<endl;*/
    graham();
    /*for(int i=0;i<top;i++)
        printf("(%lf,%lf),",stack[i].x,stack[i].y);*/
    perimeter=convex_perimeter();
    return perimeter;
}
using namespace std;
int main(){
    freopen("input.txt","r",stdin);
    int T,cnt=0;
    scanf("%d",&T);
    while(T--){
        double res;
        scanf("%d%lf",&n,&d);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        solve();
        res=2*PI*d+solve();
        printf("Case %d: %.4lf\n",++cnt,res);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值