喷水~~~~~~~~~

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler
is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the
distance from the left end of the center line and its radius of operation.
What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?


Input
Input consists of a number of cases. The first line for each case contains integer numbers n, l and w
with n ≤ 10000. The next n lines contain two integers giving the position of a sprinkler and its radius
of operation. (The picture above illustrates the first case from the sample input.)


Output
For each test case output the minimum number of sprinklers needed to water the entire strip of grass.
If it is impossible to water the entire strip output ‘-1’.


Sample Input
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1


Sample Output
6
2
-1

 

思路:

     先找区间,通过三角形跟圆的结合。

    然后进行区间排序,找区间右边尽量大的区间,直到覆盖整个草坪。

 

源代码:

    

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<cstdio>
 7 using namespace std;
 8 #define maxn 100005
 9 struct sprinkler{
10     double left;
11     double right;
12     bool operator <(const sprinkler&a)const
13     {
14         return left < a.left;
15     }
16 }arr[maxn];
17 int main()
18 {
19     int n;
20     double l, w;
21     while (cin >> n >> l >> w)
22     {
23         double p, r;
24         int Index=0;
25         int flag = 0;
26         for (int i = 0; i < n; i++)
27         {
28             cin >> p >> r;
29             if (r <= w / 2.0)
30                 continue;
31                 arr[Index].left = (double)p - sqrt(r*r - w*w / 4.0);
32                 arr[Index++].right = (double)p + sqrt(r*r - w*w / 4.0);                      //算区间
33             
34         }
35         sort(arr, arr + Index);
36         int count = 0;
37         double le=0,cur = 0;
38         int j;
39         if (arr[0].left <=0)
40         {
41             int i = 0;
42           while (i < Index)
43         {
44             j = i;
45             while (j < Index&&arr[j].left <=le)
46             {
47                 if (arr[j].right > cur)
48                     cur = arr[j].right;     //找区间右边大的
49                     j++;  
50             }
51                 if (j == i)break;    //i=j相当于没有进入循环,也就是都不满足
52                 ++count;            //满足就计数加加
53                 le = cur;            //起点更新
54                 i=j;             
55 
56                 if (cur >= l)      //全部覆盖
57                 {
58                     flag = 1;
59                     break;
60                 }
61 
62             }
63         }
64         if (flag)
65             cout << count << endl;
66 
67         else
68             cout << "-1" << endl;
69 
70 
71     }
72     return 0;
73 
74 }

 

posted @ 2015-08-09 19:44  白一  阅读(431)  评论(0编辑  收藏  举报