题目链接:HDU1348
题目大意:给你一些n个点,求其凸包,然后将每条边向外平移一段距离l,这时候每个相邻的两条边之间就会有一个夹角,用需要用弧来填充,画一下就很明白了,然后所有的弧长加起来就是,
2∗π∗l
另外还需要注意的一点是,题目要求输出四舍五入整数,这样写
%.0lf
考虑到凸包模板代码还没有看懂,所以没有加上注释,等我以后看懂了,会更新的:)
AC代码
/*
2017年8月1日23:42:25
HDU1348 凸包入门
ACs
*/
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=1010;
struct point{
int x,y;
}pt[maxn],ans[maxn];
int cnt;
int cmp(point a,point b){
/*左下角优先*/
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int cross(point p0,point p1,point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double length(point a,point b){
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void convex(int n){
sort(pt,pt+n,cmp);
cnt=0;
for(int i=0;i<n;i++){//下凸包
while(cnt>1&&cross(ans[cnt-2],ans[cnt-1],pt[i])<=0){
cnt--;
}
ans[cnt++]=pt[i];
}
int key=cnt;
for(int i=n-2;i>=0;i--){
while(cnt>key&&cross(ans[cnt-2],ans[cnt-1],pt[i])<=0){
cnt--;
}
ans[cnt++]=pt[i];
}
}
int main(){
int n,l;
double Pi=atan(1.0)*4;
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&l);
for(int i=0;i<n;i++){
scanf("%d%d",&pt[i].x,&pt[i].y);
}
convex(n);
double res=2*Pi*l;
for(int i=0;i<cnt-1;i++){
res+=length(ans[i],ans[i+1]);
}
printf("%.0lf\n",res);//.0代表小数点后保留0位数字,如果多出来,用四舍五入
if(t) printf("\n");
}
return 0;
}