SPOJ CIRU The area of the union of circles ——Simpson积分

【题目分析】

圆的面积并。

直接Simpson积分,(但是有计算几何的解法,留着flag)。

simpson积分,如果圆出现了不连续的情况,是很容易出事情的。(脑补一下)

但是没有什么办法,本来就是一种取巧的做法,还能指望这暴力积分做什么。

【代码】

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>

#include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

#define maxn 1005
#define eps 1e-6
#define db double
#define ll long long
#define ldb long double
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)

void Finout()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    #endif
}

int Getint()
{
    int x=0,f=1; char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) {if (ch==‘-‘) f=-1; ch=getchar();}
    while (ch>=‘0‘&&ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();}
    return x*f;
}

int n,cnt=0,tag[maxn],tot;
struct Circle{int x,y,r;}c[maxn];
struct Segment{db l,r;}a[maxn];

bool cmp(Segment x,Segment y)
{return x.l==y.l?x.r<y.r:x.l<y.l;}

bool cmp1(Circle a,Circle b)
{return a.r<b.r;}

db get(db x)
{
	int cnt=0;
	F(i,1,n) if (fabs(c[i].x-x)<=c[i].r-eps)
	{
		db tmp=sqrt(c[i].r*c[i].r-(x-c[i].x)*(x-c[i].x));
		a[++cnt]=(Segment){c[i].y-tmp,c[i].y+tmp};
	}
	sort(a+1,a+cnt+1,cmp);
	db h=-inf,ans=0;
	F(i,1,cnt)
	{
		if (a[i].l>h) ans+=a[i].r-a[i].l,h=a[i].r;
		else if (a[i].r>h) ans+=a[i].r-h,h=a[i].r;
	}
	return ans;
}

db cal(db l,db r)
{
	db mid=(l+r)/2,fl=get(l),fr=get(r),fm=get(mid);
	return (r-l)/6*(fl+fr+4*fm);
}

db simpson(db l,db r)
{
	db mid=(l+r)/2,s1=cal(l,r),s2=cal(l,mid)+cal(mid,r);
	if (fabs(s2-s1)<=eps) return s2;
	else return simpson(l,mid)+simpson(mid,r);
}

int main()
{
	Finout();n=Getint();
	F(i,1,n)
	{
		c[i].x=Getint();
		c[i].y=Getint();
		c[i].r=Getint();
	}
	sort(c+1,c+n+1,cmp1);
	F(i,1,n-1) F(j,i+1,n)
	{
		if ((c[i].x-c[j].x)*(c[i].x-c[j].x)+(c[i].y-c[j].y)*(c[i].y-c[j].y)<=(c[j].r-c[i].r)*(c[j].r-c[i].r))
		{
			tag[i]=1;
			break;
		}
	}
	F(i,1,n) if (!tag[i]) c[++tot]=c[i];
	n=tot;
	printf("%.3f\n",simpson(-2000.0,2000.0));
}

  

时间: 2024-10-10 02:14:08

SPOJ CIRU The area of the union of circles ——Simpson积分的相关文章

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 first line is one integer n indicates the number of the circles. (1 <= n <= 1000) Then follows n lines every line has three integers Xi Yi Ri indicate

SPOJ CIRU The area of the union of circles (计算几何)

题意:求 m 个圆的并的面积. 析:就是一个板子题,还有要注意圆的半径为0的情况. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstrin

The area of the union of circles

#include<stdio.h> #include<math.h> #include<algorithm> using namespace std; const double eps=1e-8; const double PI=acos(-1.0); struct Circle{ double x,y,r; Circle(){} Circle(double xx,double yy){x=xx;y=yy;} }; struct Node{//入点+1,可以表示覆盖了多

SPOJ CIRU SPOJ VCIRCLE 圆的面积并问题

SPOJ VCIRCLE SPOJ CIRU 两道题都是给出若干圆 就面积并,数据规模和精度要求不同. 求圆面积并有两种常见的方法,一种是Simpson积分,另一种是几何法. 在这里给出几何方法. PS.以下算法基于正方向为逆时针 考虑上图中的蓝色圆,绿色的圆和蓝色的圆交于 A,B 2个交点 ,我们在逆时针系下考虑,那么 可以知道 对于蓝色的圆,它对应的某个 角度区间被覆盖了 假设 区间为 [A, B], 并且角度是按照 圆心到交点的 向量的 极角来定义 (为了方便,我一般都把角度转化到 [0,

hdu 1071 The area 高斯消元求二次函数+辛普森积分

构造系数矩阵,高斯消元求解二次函数,然后两点式求直线函数,带入辛普森积分法无脑AC... #include<cstdio> #include<queue> #include<algorithm> #include<cstring> #include<vector> #include<cmath> using namespace std; struct node { double x,y; }p[4]; double g[10][10]

【转】[专题学习][计算几何]

原文地址:http://www.cnblogs.com/ch3656468/archive/2011/03/02/1969303.html 基本的叉积.点积和凸包等东西就不多说什么了,网上一搜一大堆,切一些题目基本熟悉了就差不多了. 一些基本的题目可以自己搜索,比如这个blog:http://blog.sina.com.cn/s/blog_49c5866c0100f3om.html 接下来,研究了半平面交,思想方法看07年朱泽园的国家队论文,模板代码参考自我校大牛韬哥: http://www.o

The Inclusion-Exclusion Principle

The Inclusion-Exclusion Principle The inclusion-exclusion principle is an important combinatorial way to compute the size of a set or the probability of complex events. It relates the sizes of individual sets with their union. Statement The verbal fo

LeetCode解题思路:595. Big Countries

There is a table World +-----------------+------------+------------+--------------+---------------+ | name | continent | area | population | gdp | +-----------------+------------+------------+--------------+---------------+ | Afghanistan | Asia | 652

poj 1151 Atlantis

Atlantis http://poj.org/problem?id=1151 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22662   Accepted: 8478 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts