Watering Grass(贪心)

本文探讨了在一条长条形草地上安装多个喷水器,以覆盖整块草地的最小喷水器数量问题。通过计算每个喷水器的实际覆盖范围,并采用递减排序和贪心算法确定最少开启的喷水器数目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Watering Grass

 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

 

//意思是有 n 个喷水的,草地是长为 l 宽为 w 然后是 n 个喷水的东西,坐标和喷洒半径 p, r

首先要解决的问题是,如何表示一个喷洒的作用范围

L = p - sqrt ( r * r - w * w / 4 )

R = p - sqrt ( r * r - w * w / 4 )

实际完全覆盖了这个区间的草地

然后,按完全覆盖的最右距离递减排序,然后贪心,每次选出可以向右覆盖的最远喷洒即可

130ms

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 struct Node
 8 {
 9     double l,r;
10     /*
11     friend bool operator < (const Node&a,const Node&b)
12     {
13         return a.r > b.r;
14     }
15     */
16 
17 }node[10005];
18 
19 void sort_node(int n)
20 {
21     for (int i=0;i<n;i++)
22     {
23         int k=i;
24         for (int j=i+1;j<n;j++)
25         {
26             if (node[j].r>node[k].r)
27                 k=j;
28         }
29         swap(node[k],node[i]);
30     }
31 }
32 
33 int main()
34 {
35     int n;
36     while (cin>>n)
37     {
38         double L,W;
39         cin>>L>>W;
40         int i;
41         for (i=0;i<n;i++)
42         {
43             double p,r;
44             cin>>p>>r;
45             node[i].l=p-sqrt(r*r-W*W/4);
46             node[i].r=p+sqrt(r*r-W*W/4);
47         }
48         //sort(node,node+n);//不知道为什么快排就是错的
49         sort_node(n);
50         double k=0;
51         int num=0;
52         while(k<L)
53         {
54             for (i=0;i<n;i++)
55             {
56                 if (node[i].l<=k&&node[i].r>k)
57                 {
58                     k=node[i].r;
59                     num++;
60                     break;
61                 }
62             }
63             if (i==n) break;
64         }
65         if (k<L) cout<<"-1"<<endl;
66         else cout<<num<<endl;
67     }
68     return 0;
69 }
View Code

 

转载于:https://www.cnblogs.com/haoabcd2010/p/6171794.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值