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;
}
SPOJ CIRU的更多相关文章
- SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题
		SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. P ... 
- SPOJ CIRU - The area of the union of circles (圆的面积并)
		CIRU - The area of the union of circles no tags You are given N circles and expected to calculate t ... 
- SPOJ CIRU The area of the union of circles
		You are given N circles and expected to calculate the area of the union of the circles ! Input The f ... 
- SPOJ CIRU The area of the union of circles (计算几何)
		题意:求 m 个圆的并的面积. 析:就是一个板子题,还有要注意圆的半径为0的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ... 
- SPOJ CIRU The area of the union of circles ——Simpson积分
		[题目分析] 圆的面积并. 直接Simpson积分,(但是有计算几何的解法,留着flag). simpson积分,如果圆出现了不连续的情况,是很容易出事情的.(脑补一下) 但是没有什么办法,本来就是一 ... 
- SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)
		Description You are given N circles and expected to calculate the area of the union of the circles ! ... 
- SPOJ 8073 The area of the union of circles (圆并入门)
		Sphere Online Judge (SPOJ) - Problem CIRU [求圆并的若干种算法,圆并扩展算法]_AekdyCoin的空间_百度空间 参考AekdyCoin的圆并算法解释,根据 ... 
- 【题解】CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178]
		[题解]CIRU - The area of the union of circles [SP8073] \ 圆的面积并 [Bzoj2178] 传送门: \(\text{CIRU - The area ... 
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
		2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ... 
随机推荐
- iptables防火墙和selinux
			iptables 存在以下两种方式: 一.service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙 开启 ... 
- Spring中的容器
			1.Spring容器 Spring容器最基本的接口就是BeanFactory, 负责配置,创建和管理bean.我们通常不直接使用BeanFactory接口,而是使用其子接口ApplicationCon ... 
- Photon + Unity3D 线上游戏开发   学习笔记(一)
			大家好. 我也是学习Photon + unity3D 的新手 有什么说错的地方大家见谅哈. 我的开发环境是 unity3D 4.1.3 , Visual Studio 是2010 版本号的 p ... 
- 写一个函数,输入int型,返回整数逆序后的字符串
			刚刚看到一个面试题:写一个函数,输入int型,返回整数逆序后的字符串.如:输入123,返回"321". 要求必须用递归,不能用全局变量,输入必须是一个參数.必须返回字符串.&quo ... 
- validate命令---rman进行备份和回复的验证
			rman作为oracle备份与恢复工具,为我们提供了强大的功能.当中包含对数据文件的物理和逻辑检測以及备份文件的有效性检測. 首先.来看一下rman对数据文件的检測. 我们知道,rman在备份数据时, ... 
- ym—— Android网络框架Volley(终极篇)
			转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103).谢谢支持! 没看使用过Volley的同学能够,先看看Android网络框架Volley(体验篇)和 ... 
- Chrome下使用百度地图报错Cannot read property 'minZoom' of undefined
			问题:工作中在Google chome下面的js console里面测试百度地图API var map = new BMap.Map("container"); map.cente ... 
- c12---数组
			// // main.c // 数组基本概念 // // Created by xiaomage on 15/6/9. // Copyright (c) 2015年 itcast. All right ... 
- ASP.NET Core-组件-后台任务:Hangfire
			ylbtech-ASP.NET Core-组件-后台任务:Hangfire Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的 ... 
- idea设置Template
			在eclipse里面经常会用到syso和main类似这样的内容,但是idea工具里面没有,可以通过 Editor ==> Live templates ==> 1.首先创建一个自己的Te ... 
