[SHOI2012]信用卡凸包

XXVI.[SHOI2012]信用卡凸包

一种异端想法是因为只需保证两位精度所以直接在每个半圆上取 \(100\) 个点代表该半圆,没有试过,但说不定也能过……

书归正传。

我们考虑画出最终所得到的图形,发现其就是一堆小扇形,再加上中间的“裁去边角后的信用卡”的部分。大眼观察可得那堆小扇形拼一起就得到了一个整圆。

于是我们用所有“裁去边角后的信用卡”的四个角怼一块求一个凸包,用它的周长再加上此整圆的周长,即是最终答案。

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
const double pi=acos(-1);
const double eps=1e-8;
int cmp(double ip){
	if(ip>eps)return 1;
	if(ip<-eps)return -1;
	return 0;
}
struct Vector{
	double x,y;
	Vector(){}
	Vector(double X,double Y){x=X,y=Y;}
	void read(){scanf("%lf%lf",&x,&y);}
	void print()const{printf("(%lf,%lf)",x,y);}
	double operator~()const{return sqrt(x*x+y*y);}
	double operator!()const{return atan2(y,x);}
	friend Vector operator+(const Vector &u,const double &v){
		double theta=(!u)+v;
		double r=~u;
//		u.print(),printf("%lf %lf(%lf,%lf)\n",r,theta,cos(theta),sin(theta));
		return Vector(r*cos(theta),r*sin(theta));
	}
	friend Vector operator+(const Vector &u,const Vector &v){return Vector(u.x+v.x,u.y+v.y);}
	friend Vector operator-(const Vector &u,const Vector &v){return Vector(u.x-v.x,u.y-v.y);}
	friend double operator&(const Vector &u,const Vector &v){return u.x*v.y-u.y*v.x;}
}p[40100];
int stk[40100],tp;
double a,b,r,theta;
bool cmp1(const Vector &u,const Vector &v){return cmp(u.y-v.y)?u.y<v.y:u.x<v.x;}
bool cmp2(const Vector &u,const Vector &v){return !u<!v;}
double ConvexHull(){
	sort(p+1,p+m+1,cmp1);
	for(int i=m;i>=1;i--)p[i]=p[i]-p[1];
	sort(p+1,p+m+1,cmp2);
//	for(int i=1;i<=m;i++)p[i].print(),puts("");puts("");
	stk[tp++]=1;
	for(int i=2;i<=m;i++){
		while(tp>=2&&cmp((p[stk[tp-1]]-p[stk[tp-2]])&(p[stk[tp-1]]-p[i]))!=-1)tp--;
		stk[tp++]=i;
	}
	while(tp>=2&&cmp((p[stk[tp-1]]-p[stk[tp-2]])&(p[stk[tp-1]]-p[stk[0]]))!=-1)tp--;
//	for(int i=0;i<tp;i++)p[stk[i]].print(),puts("");
	double ret=0;
	for(int i=0;i<tp;i++)ret+=~(p[stk[(i+1)%tp]]-p[stk[i]]);
	return ret;
}
int main(){
	scanf("%d%lf%lf%lf",&n,&b,&a,&r),(a/=2)-=r,(b/=2)-=r;
	for(int i=1;i<=n;i++){
		p[0].read(),scanf("%lf",&theta);
		p[++m]=p[0]+(Vector(a,b)+theta);
		p[++m]=p[0]+(Vector(a,-b)+theta);
		p[++m]=p[0]+(Vector(-a,b)+theta);
		p[++m]=p[0]+(Vector(-a,-b)+theta);
	}
//	for(int i=1;i<=m;i++)p[i].print(),puts("");puts("");
	printf("%.2lf\n",2*r*pi+ConvexHull());
	return 0;
} 

posted @ 2021-04-05 20:50  Troverld  阅读(46)  评论(0编辑  收藏  举报