码迷,mamicode.com
首页 > 其他好文 > 详细

1424:【例题3】喷水装置

时间:2019-06-08 15:05:03      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:快排   struct   can   喷水装置   http   style   思路   turn   string   

1424:【例题3】喷水装置

技术图片

技术图片

 

题解

 技术图片

 

所以就可以吧这些圆简化为线段

 

技术图片

 

思路

①读入数据,并计算 a[cnt].s =p -sqrt((r*r)-(w/2.0)*(w/2.0));

                               a[cnt].e =p +sqrt((r*r)-(w/2.0)*(w/2.0));

②按S[i].s进行从小到大快排

③从左到右依次处理每个区间

 

 

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>

using namespace std;

int T;
int n,l,w,r,p,cnt;

struct app
{
    double s,e;
}a[20015];

bool cmp(app x,app y)
{
    return x.s <y.s ;
}

void read()
{
        cnt=0;
        scanf("%d%d%d",&n,&l,&w);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&p ,&r );
            if(r<=w/2)  continue;  //直径无法完成w,没有用,直接不计入读入
            cnt++;
            a[cnt].s =p -sqrt((r*r)-(w*w/4.0));
            a[cnt].e =p +sqrt((r*r)-(w*w/4.0));    
        }
        
}

void solve()
{
       int ans=0;
       int i=1;
       bool flag=1;
       double t=0;
       
       while(t<l)
       {
           ans++;
           double s=t;
           for(;a[i].s <=s&&i<=cnt ; i++)   //依次找能够覆盖L点的最大右端点
               if(t<a[i].e ) t=a[i].e ;
           
           if(t==s&&s<l)    //中间有断层,且未到达终点,判断无解
           {
            flag=0;
            printf("-1\n");
            break;
           }
          
       }
       
       if(flag) printf("%d\n",ans);
}

int main()
{
    scanf("%d",&T);
    
    while(T--)
    {
        read();
        sort(a+1,a+cnt+1,cmp);
        solve();
    }
    
    return 0;
}

 

1424:【例题3】喷水装置

标签:快排   struct   can   喷水装置   http   style   思路   turn   string   

原文地址:https://www.cnblogs.com/xiaoyezi-wink/p/10975992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!