Graham算法求凸包,90pts,WA #8 #12
查看原帖
Graham算法求凸包,90pts,WA #8 #12
1258467
hao12345ia楼主2025/1/25 14:58

自测已过hack

#include<bits/stdc++.h>
using namespace std;
struct node{
	double x,y;
	double angle;
	node operator-(const node p)const{
		 return{x-p.x,y-p.y,0};
	}
}p[40001];
int n;
double l,h,r;
double x,y,th;
double ju(node x,node y){
	return sqrt((x.y-y.y)*(x.y-y.y)+(y.x-x.x)*(y.x-x.x));
}
bool cmp(node x,node y){
	if(x.angle==y.angle){
		return ju(x,p[1])>ju(y,p[1]);
	}
	return x.angle>y.angle;
}
double jiao(node x,node y){
	return x.x*y.y-x.y*y.x;
}
void init(){
	int tot=0;
	cin>>n;
	cin>>l>>h>>r;
    for(int i=1;i<=n;i++){
//    	cout<<"i"<<i<<endl;
        cin>>x>>y>>th;
//        cout<<"i"<<i<<endl;
        p[++tot].x=(h/2-r)*cos(th)-(l/2-r)*sin(th)+x;
        p[tot].y=(l/2-r)*cos(th)+(h/2-r)*sin(th)+y;
        p[++tot].x=-(h/2-r)*cos(th)-(l/2-r)*sin(th)+x;
        p[tot].y=(l/2-r)*cos(th)-(h/2-r)*sin(th)+y;
        p[++tot].x=(h/2-r)*cos(th)+(l/2-r)*sin(th)+x;
        p[tot].y=-(l/2-r)*cos(th)+(h/2-r)*sin(th)+y;
        p[++tot].x=-(h/2-r)*cos(th)+(l/2-r)*sin(th)+x;
        p[tot].y=-(l/2-r)*cos(th)-(h/2-r)*sin(th)+y;
    }
    n=tot;
}
node st[400001];
int top;
int main(){
	init();
	int k,xx,yy;
	for(int i=1;i<=n;i++){
		if(i==1){
			k=i;
			xx=p[i].x;
			yy=p[i].y;
		}
		if(p[i].x<xx||(p[i].x==xx&&p[i].y<yy)){
			k=i;
			xx=p[i].x;
			yy=p[i].y;
		}
	}
	swap(p[k],p[1]);
	for(int i=1;i<=n;i++){
		p[i].angle=atan2(p[i].x-p[1].x,p[i].y-p[1].y);
	}
	sort(p+2,p+n+1,cmp);
	st[++top]=p[1];
	for(int i=2;i<=n;i++){
		while(top>=2&&
		  jiao(st[top]-st[top-1],p[i]-st[top])<=0){
			top--;
		}
		st[++top]=p[i];
	}
	st[top+1]=p[1];
	double ans=0;
	for(int i=1;i<=top;i++){
		ans+=ju(st[i],st[i+1]);
//		cout<<st[i].x<<" "<<st[i].y<<endl;
	}
	if(r!=0){
		printf("%.2lf",ans+double(3.141592653589793)*(double)2*r);
		return 0;
	}
	printf("%.2lf",ans);
}
2025/1/25 14:58
加载中...