[vijos1007] 绕钉子的长绳子

来源:互联网 发布:nginx tomcat 集群 编辑:程序博客网 时间:2024/05/01 04:13

题目描述

平面上有N个圆柱形的大钉子,半径都为R,所有钉子组成一个凸多边形。
现在你要用一条绳子把这些钉子围起来,绳子直径忽略不计。
求出绳子的长度


输入格式

第1行两个数:整数N(1<=N<=100)和实数R。
接下来N行按逆时针顺序给出N个钉子中心的坐标
坐标的绝对值不超过100。


输出格式

一个数,绳子的长度,精确到小数点后2位。


样例数据

样例输入

4 1
0.0 0.0
2.0 0.0
2.0 2.0
0.0 2.0

样例输出

14.28


题目分析

裸凸包?但注意面积为0周长不能减半,因为钉子有直径


源代码

#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() {    int num=0,bj=1;    char x=getchar();    while(x<'0'||x>'9') {        if(x=='-')bj=-1;        x=getchar();    }    while(x>='0'&&x<='9') {        num=num*10+x-'0';        x=getchar();    }    return num*bj;}const double eps=1e-10;int DoubleCompare(double x) { //精度三出口判断与0关系    if(fabs(x)<eps)return 0; //=0    else if(x<0)return -1; //<0    else return 1; //>0}struct Point {    double x,y;    bool operator < (Point b) const {        return x<b.x||(x==b.x&&y<b.y);    }};double Dist(Point a,Point b) {    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}struct Vector {    double x,y;};Vector operator - (Point a,Point b) {    Vector c;    c.x=b.x-a.x;    c.y=b.y-a.y;    return c;}double Cross(Vector a,Vector b) { //叉积    return a.x*b.y-b.x*a.y;}double Area(Point a,Point b,Point c) { //三点的平行四边形有向面积    Vector u=b-a;    Vector v=c-a;    return Cross(u,v);}double Area(int n,Point* P) { //计算多边形有向面积(剖分法)     double ans=0;    for(int i=2; i<n; i++)ans+=Area(P[1],P[i],P[i+1]);    return ans/2;}int ConvexHull(int n,Point* p,Point* ans) { //Andrew算法求凸包(求上链与下链):p是点数组,ch是凸包顶点,返回顶点数     //输入不能有重复点,若要凸包边上没有输入点,将两个<=改为<    sort(p+1,p+n+1);    int top=0;    for(int i=1; i<=n; i++) {        while(top>1&&DoubleCompare(Cross(ans[top-1]-ans[top-2],p[i]-ans[top-2]))<=0)top--;        ans[top++]=p[i];    }    int k=top;    for(int i=n-1; i>=1; i--) {        while(top>k&&DoubleCompare(Cross(ans[top-1]-ans[top-2],p[i]-ans[top-2]))<=0)top--;        ans[top++]=p[i];    }    if(n>1)top--;    return top;}//////////////Point a[30005],b[30005];int n;double l,r;int main() {    n=Get_Int();    scanf("%lf",&r);    for(int i=1; i<=n; i++)scanf("%lf%lf",&a[i].x,&a[i].y);    int cnt=ConvexHull(n,a,b);    double s=Area(cnt,b);    for(int i=1; i<cnt; i++)l+=Dist(b[i],b[i+1]);    l+=Dist(b[cnt],b[1]);    printf("%0.2lf\n",l+2*3.1415926*r);    return 0;}

0 0
原创粉丝点击