UVA 10382 Watering Grass(变相的最小覆盖问题)

来源:互联网 发布:阿里云邮箱忘记密码 编辑:程序博客网 时间:2024/06/07 12:15

Watering Grass
Input:
 standard input
Output: standard output
Time Limit: 3 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?

Input

Input consists of a number of cases. The first line for each case contains integer numbers nl 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个圆,n行,每行有每个圆的位置,及半径。

求用最少的圆覆盖这一长条。

思路:半径少于宽的一半的,直接舍去,因为没法覆盖,直接没用!

有用的长度一共是2*(r^2-w^2/4),因为圆弧的那一部分相当于没用。

最后就转化成了最小覆盖问题。

#include <iostream>#include <cstring>#include <cstdio>#include<cmath>#include <algorithm>using namespace std; struct point {     double zuo;     double you;   friend bool operator <(const point a,const point b)     {            return a.zuo<b.zuo;     } }a[10010];int sign[10010];int main(){    int n,l,w,i,j,k;    int p,r; while(~scanf("%d%d%d",&n,&l,&w)) {     int ss=0;     for(i=0;i<n;i++)     {         scanf("%d%d",&p,&r);          if(r<=w/2.0) continue;          double tmp=sqrt((double)r*r-(double)w*w/4.0);         a[ss].zuo=p-tmp;         a[ss].you=p+tmp;         ss++;     }   sort(a,a+ss);    if(a[0].zuo>0)       {           printf("-1\n");           continue;       }   int flag=0,s=0;   double cnt=0,cen=0;   i=0;  while(i<ss)  {    j=i;      while(j<ss&&cnt>=a[j].zuo)      {         if(cen<a[j].you)           cen=a[j].you;           j++;      }     if(i==j) break;     s++;     cnt=cen;     i=j;    if(cnt>=l)    {        flag=1;        break;    }  } if(flag)    printf("%d\n",s); else    printf("-1\n"); }    return 0;}


 

 

原创粉丝点击