求凸包(graham扫描法)

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. 


Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. 

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle. 

Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

Sample Output

1628

Hint

结果四舍五入就可以了

我的wa(不知为啥)代码    ,ac在下面

# include<cstdio>
# include<iostream>
# include<cmath>
# include<algorithm>
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-8;
const int maxn=1e3+10;
int n,l;
int top;
struct point{
	double x,y;
	point(double _x=0,double _y=0){
		x=_x;y=_y;
	}
	friend point operator + (const point &a,const point &b){
		return point(a.x+b.x,a.y+b.y);
	}
	friend point operator - (const point &a,const point &b){
		return point(a.x-b.x,a.y-b.y);
	}
	friend double operator ^ (point a,point b){
		return a.x*b.y-a.y*b.x;
	}
};
point dot[maxn],stk[maxn];

double distance11(point a,point b){  // 求两点之间的距离 
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

double parellel(double key){//判断是否平行 
	return fabs(key)<eps?0:key;
}

int cmp(point a,point b){//对点进行极角排序 
	double res=parellel((a-dot[1])^(b-dot[1]));
	if(res>0)return 1;
	if(res==0&&distance11(a,dot[1])<distance11(b,dot[1]))return 1;
	return 0;
}

//调用函数求取凸包 
void graham(){
	sort(dot+2,dot+1+n,cmp);
	top=2;stk[1]=dot[1];stk[2]=dot[2];
	for(int i=3;i<=n;++i){
		while(top>=2&&((stk[top]-stk[top-1])^(dot[i]-stk[top-1]))<eps)--top;
		stk[++top]=dot[i];
	}stk[top+1]=stk[1];
}
int main(){

	scanf("%d%d",&n,&l);
	for(int i=1;i<=n;i++){
		cin>>dot[i].x>>dot[i].y;
	}
	
	//获得最下面的点 
	int k=1;
	for(int i=2;i<=n;i++){
		if(dot[i].y<dot[k].y||(dot[i].y==dot[k].y&&dot[i].x<dot[k].x)){
			k=1;
		}
	}swap(dot[1],dot[k]);
	graham();
	double ans=2*pi*l;
	for(int i=2;i<=top+1;i++){
		ans+=distance11(stk[i],stk[i-1]);
	}
	int ans1=round(ans);
	cout<<ans1;
}

ac代码

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
# include<cstdio>
#define pi acos(-1.0)
#define eps 1e-8
using namespace std;
int n,m;
const int maxn = 1e3+10;
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){
	}
	friend Point operator +(const Point &a,const Point &b){
		return Point(a.x+b.x,a.y+b.y);
	}
	friend Point operator -(const Point &a,const Point &b){
		return Point(a.x-b.x,a.y-b.y);
	}
	friend double operator ^(Point a,Point b){
		return a.x*b.y-a.y*b.x;
	}
};

 Point dots[maxn];
 double distance111(Point a,Point b){
 	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 }
double parellel(double key){
	return fabs(key)<eps?0:key;
}
Point stk[maxn];
	int top;
	int cmp(Point a,Point b){
		double res=parellel((a-dots[1])^(b-dots[1]));
		if(res>0)return 1;
		if(res==0&&distance111(a,dots[1])<distance111(b,dots[1]))return 1;
		return 0;
	}
   void graham(){
   	sort(dots+2,dots+1+n,cmp);
   	top=2;stk[1]=dots[1];stk[2]=dots[2];
   	for(int i=3;i<=n;i++){
   		while(top>=2&&((stk[top]-stk[top-1]^dots[i]-stk[top-1]))<eps)--top;
   		stk[++top]=dots[i];
   		
   	}stk[top+1]=stk[1];
   }
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lf%lf",&dots[i].x,&dots[i].y);
	}
	int k=1;
	for(int i=2;i<=n;i++){
		if(dots[i].y<dots[k].y||(dots[i].y==dots[k].y&&dots[i].x<dots[k].x)){
			k=i;
		}
	}swap(dots[1],dots[k]);
	graham();
double ans=2*pi*m;
for(int i=2;i<=top+1;i++){
	ans+=distance111(stk[i],stk[i-1]);

}
int ans1=round(ans);
printf("%d",ans1);	
	
	
	
	return 0;
}

阿里云建站—为企业提供互联网“快”服务

2020年因为一场突如其来的疫情,不少企业受到了严重冲击,疫情的冲击力对传统“纯线

下”行业的危害最大,互联网女皇玛丽·米克(MaryMeeker)4月17日发布了著名的年度互

联网趋势报告,报告中指出:拥有强大的互联网线上线下融合能力的企业在疫情中的表现最好,

线上线下融合的趋势已经存在一段时间了,但是疫情让这种需求变得更加的迫切。

如果你的企业完全依附于传统的、纯线下的经验模式,那么在2020年你将“必死无疑”,

一场巨大的,前所未有的互联网革命已经到来!

阿里云建站为了助力各行各业复工复产,疫情期间“马不停蹄”为数以万计的企业快速完成

建站,为他们朝着“线上线下融合”或者“纯线上”的互联网经营模式迈进,打下了坚实的基础。

“云·速成美站”模板建站1天就能上线,不懂技术没关系,打字就能建网站。千套网站模

板免费提供,百元就能建官网,一价全包,无任何隐形消费。

“云·企业官网”定制建站1周就能上线,高端量身定制建站,千元建官网无需自己动手,

建站专家1对1网站策划及设计,专业省心之选。

疫情,是一场大浪淘沙,每一次危机背后都隐藏着机会,危机越大,机会也越大。大环境

已经改变,如果你不努力不去改变来迎合这个大环境,那你必将被淘汰。

阿里云助力企业建站,优惠多多,福利多多,详情请点击如下链接

红包+折扣,阿里云上云大礼包! https://www.aliyun.com/minisite/goods?userCode=uwo6gq74

猜你喜欢

转载自blog.csdn.net/weixin_44075041/article/details/99452222