hdu_1348_Wall(凸包)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1348

题意:让你求n个点的凸包,凸包离点的距离为l

题解:就凸包周长+一个半径为l的圆周长

#include<cstdio>
#include<cmath>
#include<algorithm>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
/*  
* 求凸包,Graham算法  * 点的编号0~n-1  
* 返回凸包结果Stack[0~top-1]为凸包的编号 
 */ 
const int MAXN = 1010;
const double eps = 1e-8; 
const double PI = acos(-1.0);
struct Point {
	 double x,y; 
	 Point(){} 
	 Point(double _x,double _y){x = _x,y = _y;} 
	 Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}
	 double operator ^(const Point &b)const{return x*b.y-y*b.x;}//叉积 
	 double operator *(const Point &b)const{return x*b.x + y*b.y;}//点积
	 void transXY(double B){double tx = x,ty = y,x = tx*cos(B) - ty*sin(B),y = tx*sin(B) + ty*cos(B);} //绕原点旋转角度B(弧度值),后x,y的变化 
}list[MAXN];
int S[MAXN],top;//相对于list[0]的极角排序
int sgn(double x) { 
 if(fabs(x) < eps)return 0; 
 if(x < 0)return -1;  
 else return 1; 
} 
double dist(Point a,Point b){return sqrt((a-b)*(a-b));}
bool _cmp(Point p1,Point p2){
	double tmp =(p1-list[0])^(p2-list[0]); 
	if(sgn(tmp)>0)return 1; 
	else if(sgn(tmp)==0&&sgn(dist(p1,list[0])-dist(p2,list[0]))<= 0)return 1;  
	return 0;
}
void Graham(int n){
	Point p0=list[0],tp; 
	int k=0;
	for(int i=1;i<n;i++)if((p0.y > list[i].y)||(p0.y ==list[i].y&&p0.x>list[i].x))p0 =list[i],k=i;
	tp=list[k],list[k]=list[0],list[0]=tp,sort(list+1,list+n,_cmp);
	if(n==1){top=1,S[0]=0;return;} 
	if(n==2){top=2,S[0]=0,S[1]=1;return;} 
	S[0]=0,S[1]=1,top=2; 
	for(int i=2;i<n;i++){
		while(top>1&&sgn((list[S[top-1]]-list[S[top-2]])^(list[i]-list[S[top-2]]))<=0)top--;
		S[top++]=i; 
	} 
}

int main(){
	int t,n,l;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&l);
		F(i,0,n-1)scanf("%lf%lf",&list[i].x,&list[i].y);
		Graham(n);
		double ans=0;
		F(i,0,top-2)ans+=dist(list[S[i]],list[S[i+1]]);
		ans+=dist(list[S[0]],list[S[top-1]])+PI*2*l;
		printf("%.0lf\n",ans);
		if(t!=0)puts("");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值