zoukankan      html  css  js  c++  java
  • LibreOJ #10002. 「一本通 1.1 例 3」喷水装置

    Description:

    长L米,宽W米的草坪里装有N个浇灌喷头。每个喷头都装在草坪中心线上(离两边各W/2米)。我们知道每个喷头的位置(离草坪中心线左端的距离),以及它能覆盖到的浇灌范围。请问:如果要同时浇灌整块草坪,最少需要打开多少个喷头?

    Code

    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    const int N = 25010;
    struct Interval{
    	double l,r;
    	bool operator < (Interval &a) const {
    		return l < a.l;
    	}
    }w[N];
    int n,T,cnt;
    double L,W;
    void init()
    {
    	cnt = 0;
    	memset(w,0,sizeof(w));
    }
    void solve()
    {
    	sort(w + 1,w + cnt + 1);
    	int ans = 0,last = 1;
    	double lb,ub = 0;
    	while(ub < L)
    	{
    		++ans;
    		lb = ub;
    		for(;w[last].l <= lb && last <= cnt;++last)
    		{
    			if(w[last].r > ub)
    			{
    				ub = w[last].r;
    			}
    		}
    		if(lb == ub && lb < L)
    		{
    			printf("-1
    ");
    			return;
    		}		
    	}
    	printf("%d
    ",ans);
    }
    int main()
    {
    	scanf("%d",&T);
    	while(T--)
    	{
    		init();
    		scanf("%d%lf%lf",&n,&L,&W);
    		double pos,r;
    		for(int i = 1;i <= n;++i)
    		{
    			scanf("%lf%lf",&pos,&r);
    			if(r > (W/2))
    			{
                                    //勾股定理
    				w[++cnt].l = pos - sqrt(r*r - (W / 2)*(W / 2));
    				w[cnt].r = pos + sqrt(r*r - (W / 2)*(W / 2));
    			}else{
    				continue;
    			}
    		}
    		solve();
    	}
    	return 0;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    Homework
    自我介绍,恩。。算是吧
    What ASP.NET Developers Should Know About JavaScript
    短信猫软件的C#实现系列文章
    装箱与拆箱
    HTML的基本标签
    CSS基础用法
    ajaxToolkit:AutoCompleteExtender 用法详解
    比较强大的分页存储过程
    不知道写点什么
  • 原文地址:https://www.cnblogs.com/Zforw/p/10963258.html
Copyright © 2011-2022 走看看