Coverage

http://acm.hdu.edu.cn/showproblem.php?pid=3425

Coverage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 510    Accepted Submission(s): 105


Problem Description
A cell phone user is travelling along a line segment with end points having integer coordinates. In order for the user to have cell phone coverage, it must be within the transmission radius of some transmission tower. As the user travels along the path, cell phone coverage may be gained (or lost) as the user moves inside the radius of some tower (or outside of the radii of all towers). Given the location of up to 100 towers and their transmission radii, you are to compute the percentage of cell phone coverage the user has
along the specified path. The (x,y) coordinates are integers between -100 and 100, inclusive, and the tower radii are integers between 1 and 100, inclusive.
 

Input
Your program will be given a sequence of configurations, one per line, of the form: N C0X C0Y C1X C1Y T1X T1Y T1R T2X T2Y T2R ... Here, N is the number of towers, (C0X,C0Y) is the start of path of the cell phone user,
(C1X,C1Y) is the end of the path, (TkX,TkY) is the position of the kth tower, and TkR is its transmission radius. The start and end points of the paths are distinct. The last problem is terminated by the line 0
 

Output
For each configuration, output one line containing the percentage of coverage the cell phone has, rounded to two decimal places.
 

Sample Input
  
3 0 0 100 0 0 0 10 5 0 10 15 0 10 1 0 0 100 0 40 10 50 0
 

Sample Output
  
25.00 88.99
 

Source
 
简单几何
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct line{
    double left,right;
    bool operator<(const line &other)const {
        return left<other.left;
    }
}l[110];
double fun(double a){
    if(a<0)return 0;
    if(a>1)return 1;
    return a;
}
int main()
{
    int n;
    double x0,y0,x1,y1;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        int cnt=0;
        scanf("%lf%lf%lf%lf",&x0,&y0,&x1,&y1);
        double dx=x1-x0,dy=y1-y0;
        double x,y,r;
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf",&x,&y,&r);
            double a=dx*dx+dy*dy;
            double b=2*(dx*(x0-x)+dy*(y0-y));
            double c=(x0-x)*(x0-x)+(y0-y)*(y0-y)-r*r;//由圆方程跟点向式推出
            double delta=b*b-4*a*c;
            if(delta<=0)continue;
            l[cnt].left=fun((-b-sqrt(delta))/(2*a));
            l[cnt].right=fun((-b+sqrt(delta))/(2*a));
            if(l[cnt].left>l[cnt].right)swap(l[cnt].left,l[cnt].right);
            cnt++;
        }
        sort(l,l+cnt);
       if(cnt==0){
            printf("0.00\n");
            continue;
        }
        double ans=0,left=l[0].left,right=l[0].right;
        for(int i=1;i<cnt;i++){
            if(l[i].left>right){
                ans+=right-left;
                left=l[i].left;
                right=l[i].right;
            }
            else
                right=right>l[i].right?right:l[i].right;
        }
            ans+=right-left;
        printf("%.2lf\n",ans*100);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值