UVA 10382.Watering Grass-贪心

时间:2023-03-09 08:22:32
UVA 10382.Watering Grass-贪心

10382 - Watering Grass

Time limit: 3.000 seconds

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?

UVA 10382.Watering 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

0 2

13 3

16 2

19 4

3 10 1

3 5

9

6 1

3 10 1

5 3

1 1

9 1
Sample Output
6

2

-1

题意就是浇水,整片草地都要浇上水,装置在草地水平中间的线上,要注意的是不是圆的面积,是近似矩形的面积。

然后就是直接算长度就可以,要求装置洒水的半径要>草地一半的宽度。

UVA 10382.Watering Grass-贪心

然后长度的话也不是圆的半径,是图上红色的长度。(图画大了,挠头。。。)

关键就是左右两边的长度计算,精度很重要,打训练赛的时候就是卡死在精度这里,wa了8发,gg。

a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0));
a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));

就是这个,还是一个大佬帮我改对的。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
struct node{
double l,r;
}a[N];
double cmp(node a,node b){
return a.r>b.r;
}
int main(){
int n,num,l,s,w,r;
double p;
while(~scanf("%d%d%d",&n,&l,&s)){
p=0.0;num=;
for(int i=;i<n;i++){
scanf("%d%d",&w,&r);
if(r>=s/2.0){
a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0)); //wa在这里了。
a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));}
}
sort(a,a+n,cmp);
//for(int i=0;i<n;i++) cout<<a[i].l<<" "<<a[i].r<<endl;
while(p<l){
int i;
for(i=;i<n;i++){
if(a[i].l<=p&&a[i].r>p){
p=a[i].r;
num++;
break;
}
}
if(i==n)break;
}
if(p<l) printf("-1\n");
else printf("%d\n",num);
}
return ;
}

网上好多代码都是错的,卡精度上了。

==

然后就是NYOJ上有一道贪心长得差不多,但是比上面的水。。。

NYOJ 6.喷水装置(一)-贪心