【BZOJ1043】【HAOI2008】—下落的圆盘(圆的并集)

传送门

题意:给n(n2000)n(n\le 2000)个圆,上面的会盖住下面的圆,求周长的并

考虑到nn很小,我们可以O(n2)O(n^2)枚举每两个圆判一下覆盖了多少
然后对于一个圆把被覆盖的取个并集就可以了

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch))res=res*10+(ch^48),ch=getchar();
	return res*f;
}
const int N=1005;
const double pi=acos(-1);
double x[N],y[N],r[N],ans;
int n,tot;
struct data{
	double l,r;
	inline bool operator <(const data&a)const{
		return l<a.l;
	}
}a[N<<1];
inline double sqr(double x){
	return x*x;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		scanf("%lf%lf%lf",&r[i],&x[i],&y[i]);
	}
	for(int i=1;i<=n;i++){
		ans+=2*pi*r[i],tot=0;
		for(int j=i+1;j<=n;j++){
			++tot;double dis=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
			if(sqr(r[i]+r[j])<=dis){
				a[tot].l=a[tot].r=0;
			}
			else if(sqr(r[i]-r[j])>=dis){
				if(r[i]>r[j])a[tot].l=a[tot].r=0;
				else a[tot].l=0,a[tot].r=2*pi;
			}
			else{
				double af=acos((sqr(r[i])+dis-sqr(r[j]))/(2*r[i]*sqrt(dis)));
				double bt=atan2(y[j]-y[i],x[j]-x[i]);
				if(bt<0)bt+=2*pi;
				a[tot].l=bt-af,a[tot].r=bt+af;
				if(a[tot].l<0)a[++tot].l=a[tot-1].l+2*pi,a[tot].r=2*pi,a[tot-1].l=0;
				else if(a[tot].r>2*pi)a[++tot].r=a[tot-1].r-2*pi,a[tot].l=0,a[tot-1].r=2*pi;
			}
		}
        sort(a + 1 , a + tot + 1);
        double last = -1;
        for(int j = 1;j<= tot;j++){
            if(a[j].r<=last)continue;
            if(a[j].l>last)ans-=(a[j].r-a[j].l)*r[i];
            else ans-=(a[j].r-last)*r[i];
            last=a[j].r;
        }
    }
	printf("%.3lf",ans);
	return 0;
}
posted @ 2019-02-21 21:44  Stargazer_cykoi  阅读(196)  评论(0编辑  收藏  举报