看懂题目大意后,和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);
}
}