zoukankan      html  css  js  c++  java
  • 【BZOJ1043】【HAOI2008】—下落的圆盘(圆的并集)

    传送门

    题意:给n(n2000)n(nle 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;
    }
  • 相关阅读:
    【LeetCode】119. Pascal's Triangle II
    随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比[转]
    linux下一些可用库
    malloc分配的内存空间是连续的吗
    语言模型训练网站
    relocation 错误
    undefined reference to `dlopen'
    静态库之间有依赖关系顺序很重要
    C++引用详解
    malloc原理和内存碎片[转]
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/11145660.html
Copyright © 2011-2022 走看看