喷水装置(二)

喷水装置(二)

时间限制:3000 ms  |  内存限制:65535 KB
难度:4
 
描述
有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
 
输入
第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出
每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入
2
2 8 6
1 1
4 5
2 10 6
4 5
6 5
样例输出
1
2
来源
《算法艺术与信息学竞赛》
上传者
张云聪
解题:贪心大法!计算出每个喷头的圆形覆盖与坐标y = h/2的两个交点的横坐标!然后以左边的横坐标进行从小到大进行排序!然后进行贪心!每次选取覆盖最靠右的那个!
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 using namespace std;
11 struct point{
12     int lt,rt;
13 }p[10010];
14 bool cmp(const point &a,const point &b){
15     return a.lt < b.lt;
16 }
17 int main(){
18     int kase,n,w,h,i,x,r,ans,cur,v;
19     double temp;
20     bool flag;
21     scanf("%d",&kase);
22     while(kase--){
23         scanf("%d %d %d",&n,&w,&h);
24         for(i = 0; i < n; i++){
25             scanf("%d %d",&x,&r);
26             temp = r*r*1.0 - h*h/4.0;
27             if(temp > 0) temp = sqrt(temp);
28             else temp = 0;
29             p[i].lt = x - temp;
30             p[i].rt = x + temp;
31         }
32         sort(p,p+n,cmp);
33         ans = cur = 0;
34         flag = true;
35         while(cur < w){
36             v = 0;
37             for(i = 0; i < n && p[i].lt <= cur; i++){
38                 v = max(v,p[i].rt);
39             }
40             if(v <= cur){flag = false;break;}
41             cur = v;
42             ans++;
43         }
44         printf("%d\n",flag?ans:0);
45     }
46     return 0;
47 }
View Code

 

posted @ 2014-07-07 21:32  狂徒归来  阅读(206)  评论(0编辑  收藏  举报