SPOJ CIRU

SPOJ CIRU

题意

给出n个圆,求他们覆盖的面积。

解法

自适应Simpson,但需要将圆离散化一下,以保证我们查询的是一个连续的有圆的区间。 奇怪的是我没有离散化,样例都没有过,却把题给A了

代码如下: ( 注意 :要去掉被覆盖的圆,才不会TLE)

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;

struct cir {
	double x,y,r;
	cir(double x=0.0,double y=0.0,double r=0.0):x(x),y(y),r(r){}
	double dh(double nowx) {
		return sqrt( r*r - (nowx - x)*(nowx - x) );
	}
	bool operator < (const cir&a) const {
		return r<a.r;
	} 
}c[maxn];

struct line {
	double y;
	bool k;
	bool operator < (const line& a) const {
		return y < a.y;
	}
}temp[maxn];

int n;

double F(double x) {
	int num=0;double ans=0.0;
	for(int i=1;i<=n;i++) {
		if(x>=c[i].x-c[i].r&&x<=c[i].x+c[i].r){
			double dy=c[i].dh(x);
			temp[++num].y=c[i].y-dy; temp[num].k=1;
			temp[++num].y=c[i].y+dy; temp[num].k=0;
		} 
	}
	sort(temp+1,temp+1+num);
	int f=0;double be;
	for(int i=1;i<=num;i++) {
		if(temp[i].k) {
			if(!(f++)) {be=temp[i].y;}
		}
		else if(!(--f)) ans+=temp[i].y-be;
	}
	return ans;
}

double simpson(double a,double b) {
	double mid=(a+b)/2;
	return ( F(a) + 4*F(mid) + F(b) ) * (b - a) / 6;
}

double asr(double a,double b,double e,double A) {
	double mid=(a+b)/2;
	double L=simpson(a,mid),R=simpson(mid,b);
	if( fabs(L+R-A) <= 15*e) return L+R+(L+R-A)/15;
	return asr(a,mid,e/2,L)+asr(mid,b,e/2,R);
}
double asr(double l,double r,double eps) {
	return asr(l,r,eps,simpson(l,r));
}
double dis(int a,int b){
	return sqrt( (c[a].x-c[b].x)*(c[a].x-c[b].x)+(c[a].y-c[b].y)*(c[a].y-c[b].y) );
}
bool cover[maxn];
int main() {
	scanf("%d",&n);
	double l=2e33,r=-2e33;
	for(int i=1;i<=n;i++) {
		scanf("%lf%lf%lf",&c[i].x,&c[i].y,&c[i].r);
		l=min(l,c[i].x-c[i].r);
		r=max(r,c[i].x+c[i].r);
	}
	sort(c+1,c+1+n);
	int cnt=n;n=0;
	for(int i=1;i<cnt;i++)
		for(int j=i+1;j<=cnt;j++){
			if(dis(i,j)+c[i].r<=c[j].r){
				cover[i]=1;
				break;
			}
		}
	for(int i=1;i<=cnt;i++) 
		if(!cover[i]) 
			c[++n]=c[i];
	printf("%.3lf",asr(l,r,1e-4));
	return 0;
}

以下是加了离散化过后的版本。

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000+5;

struct cir {
	double x,y,r;
	cir(double x=0.0,double y=0.0,double r=0.0):x(x),y(y),r(r){}
	double dh(double nowx) {
		return sqrt( r*r - (nowx - x)*(nowx - x) );
	}
	bool operator < (const cir&a) const {
		return r<a.r;
	} 
}c[maxn];

struct line {
	double y;
	bool k;
	bool operator < (const line& a) const {
		return y < a.y;
	}
}temp[maxn << 1],k[maxn << 1];

int n;

double F(double x) {
	int num=0;double ans=0.0;
	for(int i=1;i<=n;i++) {
		if(x>=c[i].x-c[i].r&&x<=c[i].x+c[i].r){
			double dy=c[i].dh(x);
			temp[++num].y=c[i].y-dy; temp[num].k=1;
			temp[++num].y=c[i].y+dy; temp[num].k=0;
		} 
	}
	sort(temp+1,temp+1+num);
	int f=0;double be;
	for(int i=1;i<=num;i++) {
		if(temp[i].k) {
			if(!(f++)) {be=temp[i].y;}
		}
		else if(!(--f)) ans+=temp[i].y-be;
	}
	return ans;
}

double simpson(double a,double b) {
	double mid=(a+b)/2;
	return ( F(a) + 4*F(mid) + F(b) ) * (b - a) / 6;
}

double asr(double a,double b,double e,double A) {
	double mid=(a+b)/2;
	double L=simpson(a,mid),R=simpson(mid,b);
	if( fabs(L+R-A) <= 15*e) return L+R+(L+R-A)/15;
	return asr(a,mid,e/2,L)+asr(mid,b,e/2,R);
}
double asr(double l,double r,double eps) {
	return asr(l,r,eps,simpson(l,r));
}
double dis(int a,int b){
	return sqrt( (c[a].x-c[b].x)*(c[a].x-c[b].x)+(c[a].y-c[b].y)*(c[a].y-c[b].y) );
}
bool cover[maxn];
int main() {
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%lf%lf%lf",&c[i].x,&c[i].y,&c[i].r);
	}
	sort(c+1,c+1+n);
	int cnt=n;n=0;
	for(int i=1;i<cnt;i++)
		for(int j=i+1;j<=cnt;j++){
			if(dis(i,j)+c[i].r<=c[j].r){
				cover[i]=1;
				break;
			}
		}
	for(int i=1;i<=cnt;i++) 
		if(!cover[i]) 
			c[++n]=c[i];
	cnt=0;
	double ans=0.0;
	for(int i=1;i<=n;i++){
		k[++cnt].y=c[i].x-c[i].r; k[cnt].k=1;
		k[++cnt].y=c[i].x+c[i].r; k[cnt].k=0;
	}
	sort(k+1,k+1+cnt);
	int f=0;double be;
	for(int i=1;i<=cnt;i++){
		if(k[i].k) { if(!(f++)) be=k[i].y; }
		else if(!(--f)) ans+=asr(be,k[i].y,1e-4);
	}
	printf("%.3f",ans);
	return 0;
}

posted @ 2018-08-22 10:36  Mr_asd  阅读(131)  评论(0编辑  收藏  举报