POJ_Radar Installation_1328(简单贪心)

详细题解:http://www.doc88.com/p-204590908215.html

转化为区间覆盖问题

 1 # include <stdio.h>
 2 # include <string.h>
 3 # include <math.h>
 4 # include <algorithm>
 5 using namespace std;
 6 struct node 
 7 {
 8     double l;
 9     double r;
10 }p[5012];
11 int comp(struct node x,struct node y)
12 {
13     return x.l<y.l;
14 }
15 int main()
16 {
17     int i,j,k=1,t,n,x,y;
18     double d,c[5012],e[5012];
19     while(scanf("%d%lf",&n,&d)!=EOF)
20     {
21         if(n==0 && d==0)
22             break;
23         int temp=1;
24         for(i=0;i<n;i++)
25         {
26             scanf("%lf%lf",&c[i],&e[i]);
27             if(abs(e[i])>d)
28                 temp=0;
29         }
30         if(d<=0 || temp==0)
31             printf("Case %d: -1\n",k++);
32         else
33         {
34             for(i=0;i<n;i++)
35             {
36                 p[i].l=c[i]-sqrt(d*d-e[i]*e[i]);
37                 p[i].r=c[i]+sqrt(d*d-e[i]*e[i]);
38             }    
39             sort(p,p+n,comp);
40             int count=1;
41             double tem=p[0].r;
42             for(i=1;i<n;i++)
43             {
44                 if(p[i].r<tem)
45                     tem=p[i].r;
46                 else if(p[i].l>tem)
47                 {
48                     count++;
49                     tem=p[i].r;
50                 }
51             }
52             printf("Case %d: %d\n",k++,count);
53         }
54     }
55     return 0;
56 }
View Code

 

posted on 2013-08-07 17:27  随风浪子的博客  阅读(115)  评论(0编辑  收藏  举报

导航