[模板 辛普森积分] Grazed Grains | NCPC2021 | 辛普森积分求圆的面积并

简介: 题目描述This year, there have been unusually many UFO sightings reported. Nobody knows if they are caused by optical illusions, weather phenomena, or secret technology being tested by foreign powers (terrestrial or not). UFO enthusiasts across the world rejoice, and speculations run wild.

评测地址可以到:传送门


题目描述


This year, there have been unusually many UFO sightings reported. Nobody knows if they are caused by optical illusions, weather phenomena, or secret technology being tested by foreign powers (terrestrial or not). UFO enthusiasts across the world rejoice, and speculations run wild.

But someone who is not impressed is the farmer Celeste. Her large grain field has repeatedly been used as a landing site by the UFOs, destroying her crops. Celeste would like to bring whoever is responsible to justice, but before she can do so she must assess the damage caused by the unidentified flying offenders.

In total n circular UFOs have landed in Celeste’s field. The ith one left a crop circle which destroyed all crops within radius r i of the point x i , y i . The field is very large, so you can assume that it extends infinitely in all directions. Your task is to estimate the total area of crops destroyed by the UFOs. Celeste only needs a rough estimate of the true answer, and your answer will be counted as correct if its relative error is less than 10%.


输入


The first line of input contains one integer n (1 ≤ n ≤ 10), the number of UFOs. Then follow n lines, the ith of which contains the three integers xi , yi , and ri (0 ≤ xi , yi ≤ 10, 1 ≤ ri ≤ 10),

describing the crop circle left by the ith UFO.


输出


Output the total area covered by the crop circles in the input, with a relative error of at most 1/10.


样例输入 Copy


【样例1】

1
0 0 1


【样例2】

4
0 0 2
1 0 1
2 2 2
10 10 1


样例输出 Copy

【样例1】
3.1415926535
【样例2】
25.991148


学习参考自博客

模板来自博客


struct Point {
  double x,y;
  Point() {}
  Point(double x,double y):x(x),y(y) {}
  bool operator < (const Point &p)const {
    return x<p.x;
  }
} q[maxn];
struct circle {
  double x,y,r;
  bool operator < (const circle &p)const {
    return r>p.r;
  }
} a[maxn];
const double eps = 1e-8, inf = 1e20, Pi = acos(-1);
int n;
#define sqr(x) (x)*(x)
double f(double x) {
  int top=0;
  for(int i=1; i<=n; i++) if(fabs(x-a[i].x)<a[i].r) {
      double len=sqrt(sqr(a[i].r)-sqr(x-a[i].x));
      q[++top]=Point(a[i].y-len,a[i].y+len);
    }
  double l=-inf,r=-inf,ret=0;
  sort(q+1,q+1+top);
  for(int i=1; i<=top; i++)
    if(q[i].x>r) ret+=r-l,l=q[i].x,r=q[i].y;
    else r=max(r,q[i].y);
  return ret+=r-l;
}
double Simpson(double L,double M,double R,double fL,double fM,double fR,int dep) {
  double M1=(L+M)/2,M2=(M+R)/2,fM1=f(M1),fM2=f(M2);
  double g1=(M-L)*(fL+4*fM1+fM)/6, g2=(R-M)*(fM+4*fM2+fR)/6, g=(R-L)*(fL+4*fM+fR)/6;
  if(dep>11&&fabs(g-g1-g2)<1e-8) return g;
  return Simpson(L,M1,M,fL,fM1,fM,dep+1)+Simpson(M,M2,R,fM,fM2,fR,dep+1);
}
inline double dist(circle &a,circle &b) {
  return sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
}
void prework() {
  int cnt=0;
  sort(a+1,a+1+n);
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=cnt; j++) if(a[i].r+dist(a[i],a[j])<=a[j].r) goto A;
    a[++cnt]=a[i];
A:
    ;
  }
  n=cnt;
}
int main() {
  scanf("%d",&n);
  double l=inf,r=-inf,mid;
  for(int i=1; i<=n; i++) scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].r),l=min(l,a[i].x-a[i].r),r=max(r,a[i].x+a[i].r);
  mid=(l+r)/2;
  prework();
  printf("%.3f",Simpson(l,mid,r,f(l),f(mid),f(r),0));
  return 0;
}



目录
相关文章
|
3月前
dedecms会员登录默认增加两积分怎样去掉,怎么让会员登录不加积分?
dedecms会员登录就增加两积分怎样去掉,怎么让会员登录不加积分?
dedecms会员登录默认增加两积分怎样去掉,怎么让会员登录不加积分?
|
3月前
|
算法 前端开发
边积分最高的节点
边积分最高的节点
28 0
|
10月前
|
机器人 计算机视觉
积分和微分电路的介绍
标题:积分和微分电路的原理与应用 一、引言 积分和微分是微积分学中的两个重要概念,它们在电路中的应用也非常广泛。积分电路可以对输入信号进行积分运算,实现信号的累积效果;而微分电路则可以对输入信号进行微分运算,实现信号的变化率分析。本文将介绍积分和微分电路的原理和应用。 二、积分电路 1. 基本原理 积分电路是一种能够对输入信号进行积分运算的电路。其基本原理是利用电容器的充放电过程来实现积分运算。当输入信号经过电容器后,电容器会根据输入信号的大小和时间长度进行充电或放电,从而实现输入信号的积分效果。 2. 电路结构 积分电路一般由电阻和电容器组成。输入信号经过电阻后,与电容器相连,形成一个
262 0
|
12月前
|
算法
算法练习Day43|● 518. 零钱兑换 II ● 377. 组合总和 Ⅳ
算法练习Day43|● 518. 零钱兑换 II ● 377. 组合总和 Ⅳ
某商品有2种不同数量的包装,对应不同的价格;同时提供满200元减50元的不限量购物券,试求解最好购买策略,在单次购买中以最低总价购买正好500个商品
某商品有2种不同数量的包装,对应不同的价格;同时提供满200元减50元的不限量购物券,试求解最好购买策略,在单次购买中以最低总价购买正好500个商品
94 0
|
JavaScript 前端开发
JS 根据价格区间筛选物品
JS 根据价格区间筛选物品
JS 根据价格区间筛选物品
|
Serverless
阿里云函数计算实战-使用函数计算进行彩票中奖奖金计算和推送
每晚开奖后,自动拉取开奖号码,并根据开奖号码计算用户的奖金,然后发送到指定邮箱。建议发送到QQ邮箱,并在微信中开启“QQ邮箱服务”功能,这样就可以在微信里面查看结果了。发件地址请加入白名单列表,否则可能会被识别为垃圾邮件。
17038 0
|
数据建模
积分
1.重积分 设D是平面上的有界闭区域,z=f(x,y)是D上的有界函数,则称∬Df(x,y)dσ\iint_D f(x,y)d\sigma为二重积分. 二重积分的几何意义 代表着曲顶柱体的体积,底是区域D,顶为曲面z=f(x,y),侧面准线是D的边界,母线平行于z轴. 设Ω\Omega是空间有界闭区域,μ=f(x,y,z)\mu=f(x,y,z)是此区域上
2322 0