题目
http://acm.hdu.edu.cn/showproblem.php?pid=1348
题解
至今不懂,为什么31行的“<=”改成“<” 就WA?(为此查了一上午的错)
凸包模板题,直接Graham Scan扫出凸包,然后求周长在加上2πL
代码
#include <cstdio>
#include <algorithm>
#include <cmath>
#define PI 3.1415926
#define maxn 1010
using namespace std;
struct vec{double x, y;}p[maxn], s[maxn];
int top, N, L;
double ans;
double cp(vec p1, vec p2){return p1.x*p2.y-p2.x*p1.y;}
bool cmp(vec p1, vec p2)
{
double x=s[1].x, y=s[1].y, c=(p1.x-x)*(p2.y-y)-(p2.x-x)*(p1.y-y);
return c==0?(p1.x<p2.x or p1.y<p2.y):c>0;
}
void graham()
{
int i, j;
vec t, v1, v2;
double x, y;
j=1,x=p[1].x,y=p[1].y;
for(i=2;i<=N;i++)
if(p[i].y<y or p[i].y==y and p[i].x<x )x=p[i].x,y=p[i].y,j=i;
t=p[j], p[j]=p[1], p[1]=t;
s[top=1]=t;
sort(p+2,p+N+1,cmp);
for(i=2;i<=N;i++)
{
v1=(vec){s[top].x-s[top-1].x,s[top].y-s[top-1].y};
v2=(vec){p[i].x-s[top].x,p[i].y-s[top].y};
while(top>1 and cp(v1,v2)<=0)
{
top--;
v1=(vec){s[top].x-s[top-1].x,s[top].y-s[top-1].y};
v2=(vec){p[i].x-s[top].x,p[i].y-s[top].y};
}
s[++top]=p[i];
}
}
double dist(vec p1, vec p2)
{
double t1, t2;
t1=p1.x-p2.x;
t2=p1.y-p2.y;
return sqrt(t1*t1+t2*t2);
}
void calc()
{
int i;
for(ans=dist(s[1],s[top]),i=2;i<=top;i++)ans+=dist(s[i],s[i-1]);
ans+=2*PI*L;
}
int main()
{
int i, T, j;
scanf("%d",&T);
for(i=1;i<=T;i++)
{
scanf("%d%d",&N,&L);
for(j=1;j<=N;j++)scanf("%lf%lf",&p[j].x,&p[j].y);
graham();
calc();
printf("%.lf\n",ans);
if(i!=T)printf("\n");
}
return 0;
}