2018 Multi-University Training Contest 5 1005 Everything Has Changed

 Problem Description

Edward is a worker for Aluminum Cyclic Machinery. His work is operating mechanical arms to cut out designed models. Here is a brief introduction of his work.
Assume the operating plane as a two-dimensional coordinate system. At first, there is a disc with center coordinates (0,0) and radius R. Then, m mechanical arms will cut and erase everything within its area of influence simultaneously, the i-th area of which is a circle with center coordinates (xi,yi) and radius ri (i=1,2,⋯,m). In order to obtain considerable models, it is guaranteed that every two cutting areas have no intersection and no cutting area contains the whole disc.
Your task is to determine the perimeter of the remaining area of the disc excluding internal perimeter.
Here is an illustration of the sample, in which the red curve is counted but the green curve is not.

 

Input

The first line contains one integer T, indicating the number of test cases.
The following lines describe all the test cases. For each test case:
The first line contains two integers m and R.
The i-th line of the following m lines contains three integers xi,yi and ri, indicating a cutting area.
1≤T≤1000, 1≤m≤100, −1000≤xi,yi≤1000, 1≤R,ri≤1000 (i=1,2,⋯,m).

Output

For each test case, print the perimeter of the remaining area in one line. Your answer is considered correct if its absolute or relative error does not exceed 10−6.
Formally, let your answer be a and the jury's answer be b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.

Sample Input

1 4 10 6 3 5 10 -4 3 -2 -4 4 0 9 1

Sample Output

81.62198908430238475376

一开始利用弦长公式求解,弦长 = sqrt((R+r+d)(R-r+d)(R+r-d)(-R+r+d))/d,这样一来精度就丢失了。。。于是改用余弦定理,a^2+b^2-2abcosC = c^2  -> cosC = (a^2+b^2-c^2)/2ab

另外需要注意的是,浮点数等于、大于、小于的比较问题:

等于:“abs(a-b) < 1e-5”

大于:“a+1e-5 > b”

小于:“a-1e-5 < b”

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
const double PI = acos(-1.0);
double dist(double x1, double y1, double x2, double y2)
{
    return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
}
struct node{
    double x;
    double y;
    double r;
    double d;
}cr[500];
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        double R;
        cin >> n >> R;
        double C = R*2*PI;
//        printf("%lf\n", C);
        for(int i = 0; i < n; i++)
        {
//            cin >> cr[i].x >> cr[i].y >> cr[i].r;
            scanf("%lf %lf %lf", &cr[i].x, &cr[i].y, &cr[i].r);
            cr[i].d = sqrt(cr[i].x*cr[i].x+cr[i].y*cr[i].y);
//            printf("%lf\n", cr[i].d);
//            if(cr[i].d+1e-5 >= cr[i].r + R||cr[i].d-1e-5 < R-cr[i].r)
//                continue;
//            if(fabs(cr[i].d - R-cr[i].r) <= 1e-5)
//            {
//                C = C+PI*cr[i].r*2;
//                continue;
//            }
//            double D = sqrt((R+cr[i].r+cr[i].d)*(R+cr[i].r-cr[i].d)*(R-cr[i].r+cr[i].d)*(-R+cr[i].r+cr[i].d))/cr[i].d;
//            double A1 = 2*asin((D/2)/R);    
//            double C1 = R*A1;
//            double A2 = 2*asin((D/2)/cr[i].r);
//            if(cr[i].d < sqrt(R*R-cr[i].r*cr[i].r))
//                A2 = 2*PI - A2;    
//            double C2 = cr[i].r*A2;
//            C = C-C1+C2;
//            
            if (abs(cr[i].d - (R - cr[i].r)) < 1e-5)
            {
                C = C+PI*cr[i].r*2;
                continue;
            }
            else if ((cr[i].d + 1e-5) > (R - cr[i].r) && (cr[i].d - 1e-5) < (R + cr[i].r))
            {
                double D = sqrt((R+cr[i].r+cr[i].d)*(R+cr[i].r-cr[i].d)*(R-cr[i].r+cr[i].d)*(-R+cr[i].r+cr[i].d))/cr[i].d;
                double A1 = 2*acos((R*R + cr[i].d*cr[i].d - cr[i].r*cr[i].r) / (2 * R*cr[i].d));    
                double C1 = R*A1;
                double A2 = 2*acos((cr[i].r*cr[i].r + cr[i].d*cr[i].d - R*R) / (2 * cr[i].r*cr[i].d));
//                if(cr[i].d < sqrt(R*R-cr[i].r*cr[i].r))
//                    A2 = 2*PI - A2;    
                double C2 = cr[i].r*A2;
                C = C-C1+C2;
            }
            else
                continue;
//            printf("%lf %lf %lf %lf %lf\n",A1, A2, C1, C2, C);
        }
        printf("%.15lf\n", C);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值