Gym100714A Alien Visit+多个圆的并面积

来源:互联网 发布:macd3背离源码股海网 编辑:程序博客网 时间:2024/05/29 11:23

25 May, 1997, near Elcino-Borisovo place, Jandal region, strange signs were found in the field…
Witness: “First, I saw only one UFO. It was shining with cold-blue light.Closer to the center of the object, the light was pink. It hung over the field, then began to blink and move intermittently round. The UFO was quite big. The second UFO came several minutes after the first. It had the same size as the first one. It seemed that there was some kind of contact between them— they began to blink alternately.”
Circles of scorched barley were found in the field. The circles were of the same radius, and their centers were lying on a straight line.
You were hired to investigate the damage caused to the farms of Elcino-Borisovo place by the visit of aliens. In order to do this you are to calculate the total area of scorched barley.
Input
The first line of the input contains two integers n and r denoting number of circles and the radius of the circles, respectively (1≤n≤1 000, 1≤r≤100). The next line contains n space separated integers a1, a2, … , an— the shifts of circles’ centers relative to some origin (0≤ai≤5 000). All shifts are guaranteed to be distinct.
Output
Output the only real number — the total area covered by these circles. The relative error of your answer must not exceed 10^−6.
Examples
1 1
0
3.1415926536
2 2
0 2
20.2192624343

给你n个在x轴的圆,半径都是r,问你总共的面积是多少.
因为都是在一个坐标轴上,就没必要套圆的面积并模板:http://blog.csdn.net/xtulollipop/article/details/52268475
直接超时。。。
so..我们想象其他的。。在一个坐标轴上,,那总面积减去相邻两个圆的交面积就可以了啊。。两个圆,很容易。。3个圆时,1,3个圆不想交也满足,1,3个圆相交时1,3交的部分重叠了3次,减去了两次,也就是剩下了一块了。所以直接总面积减去相邻两个圆的交面积就可以了。

#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<stack>#include<set>#include<complex>#define EPS 1e-6    //log(x)#define e exp(1.0); //2.718281828#define mod 1000000007#define INF 0x7fffffff#define inf 0x3f3f3f3ftypedef long long LL;using namespace std;//基本函数const double eps=1e-8;int cmp(double x) {    if(fabs(x)<eps) return 0;    if(x>0) return 1;    return -1;}const double pi=acos(-1.0);inline double sqr(double x) {    return x*x;}struct point {    double x,y;    point() {};    point(double a,double b):x(a),y(b) {};    friend point operator - (const point &a,const point &b) {        return point(a.x-b.x,a.y-b.y);    }    double norm() {        return sqrt(sqr(x)+sqr(y));    }};double dis(const point &a,const point &b) {    return (a-b).norm();}int dcmp(double k) {    return k<-eps?-1:k>eps?1:0;}struct Circle {    point p;    double r;    Circle(){};    Circle(point p,double r):p(p),r(r){};    bool operator < (const Circle &o) const {        if(dcmp(r-o.r)!=0) return dcmp(r-o.r)==-1;        if(dcmp(p.x-o.p.x)!=0) {            return dcmp(p.x-o.p.x)==-1;        }        return dcmp(p.y-o.p.y)==-1;    }    bool operator == (const Circle &o) const {        return dcmp(r-o.r)==0&&dcmp(p.x-o.p.x)==0&&dcmp(p.y-o.p.y)==0;    }};//两个圆的交面积double Circle_area(Circle x,Circle y){    double a=dis(x.p,y.p),b=x.r,c=y.r;    //相离、相切    if(a>=b+c) return 0.0;    //某个圆是点时    if(b<eps||c<eps) return 0.0;    //包含    if(b<c) swap(b,c);    if(a+c<=b) return pi*c*c;    //相交。    double cta1=acos((a*a+b*b-c*c)/2/(a*b)),           cta2=acos((a*a+c*c-b*b)/2/(a*c));    double s1=b*b*cta1-b*b*sin(cta1)*(a*a+b*b-c*c)/2/(a*b);    double s2=c*c*cta2-c*c*sin(cta2)*(a*a+c*c-b*b)/2/(a*c);    return fabs(s1+s2);}Circle tc[1005];int main(){    int n;    double r,x;    while(cin>>n>>r){        for(int i=0;i<n;i++){            cin>>x;            tc[i]=Circle(point(x,0),r);        }        sort(tc,tc+n);        double ans=n*pi*r*r;        for(int i=1;i<n;i++) ans-=Circle_area(tc[i],tc[i-1]);        printf("%.10f\n",ans);    }    return 0;}
0 0