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

传送门

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

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

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值