把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

【UVA1303】Wall(凸包)

点此看题面

大致题意: 给你一个多边形,要求建一面墙使得墙上的点至少离多边形每个顶点\(R\)的距离,求最短的墙长。

考虑\(R=0\)

考虑当\(R=0\)时,所求的答案显然就是求得的凸包的周长。

因为这堵墙如果向内凹,显然长度只会变大。

考虑所有情况

对于任一情况,我们可以得图如下:

可以发现,答案是绿色部分+紫色部分,而绿色部分=红色部分=凸包周长,紫色部分=圆的周长。

所以答案就是凸包周长+圆的周长,看起来是不是很简单?

代码

#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 1000
using namespace std;
int n,m;const double pi=acos(-1);
struct Point
{
	int x,y;I Point(CI a=0,CI b=0):x(a),y(b){}
	I Point operator - (Con Point& o) Con {return Point(x-o.x,y-o.y);}
	I int operator ^ (Con Point& o) Con {return x*o.y-y*o.x;}
	I bool operator < (Con Point& o) Con {return x^o.x?x<o.x:y<o.y;}
	I double len() {return sqrt(x*x+y*y);}
}p[N+5];
struct ConvexHull
{
	int n;Point p[N+5];I ConvexHull() {n=0;}
};
class FastIO
{
	private:
		#define FS 100000
		#define tc() (A==B&&(B=(A=FI)+fread(FI,1,FS,stdin),A==B)?EOF:*A++)
		#define tn (x<<3)+(x<<1)
		#define D isdigit(c=tc())
		int f;char c,*A,*B,FI[FS];
	public:
		I FastIO() {A=B=FI;}
		Tp I void read(Ty& x) {x=0,f=1;W(!D) f=c^'-'?1:-1;W(x=tn+(c&15),D);x*=f;}
		Ts I void read(Ty& x,Ar&... y) {read(x),read(y...);}
}F;
I ConvexHull GetConvexHull(CI n,Point *p)//求凸包
{
	RI i,t;ConvexHull res;
	for(sort(p+1,p+n+1),i=1;i<=n;++i)//排序,正着扫一遍
	{
		W(res.n>1&&((res.p[res.n]-res.p[res.n-1])^(p[i]-res.p[res.n-1]))<=0) --res.n;
		res.p[++res.n]=p[i];
	}
	for(t=res.n,i=n-1;i;--i)//倒着扫一遍
	{
		W(res.n>t&&((res.p[res.n]-res.p[res.n-1])^(p[i]-res.p[res.n-1]))<=0) --res.n;
		res.p[++res.n]=p[i];
	}return res;//返回结果
}
I double PolygonC(Con ConvexHull& x)//凸包周长
{
	RI i;double res=0;for(i=1;i^x.n;++i) res+=(x.p[i]-x.p[i+1]).len();
	return res+(x.p[x.n]-x.p[1]).len();
}
int main()
{
	RI i,Tt;scanf("%d",&Tt);W(Tt--)
	{
		for(F.read(n,m),i=1;i<=n;++i) F.read(p[i].x,p[i].y);//读入数据
		printf("%.0lf\n",PolygonC(GetConvexHull(n,p))+2*pi*m),Tt&&(putchar('\n'),0);//输出答案
	}return 0;
}
posted @ 2019-07-17 19:21  TheLostWeak  阅读(187)  评论(0编辑  收藏  举报