NYOJ 287 Radar

View Code
 1 /*
2 这是一个贪心问题:
3 以岛屿为圆心,以雷达半径为半径画圆 和 X轴有两个交点 这就是说在这个范围内安装雷达都可以覆盖到
4 此岛屿
5 我们求出所有的这样的 区域 然后以左边界从小到大排列,左边界相同的 按右边界 从 大到小排列
6
7 我们只需关心 下一个范围十分 在 tr 左边
8 特别要注意的是 更新 tr 时 取得时 老 tr 和 新区域 右边界 r 两者的最小值
9 */
10 #include<iostream>
11 #include<algorithm>
12 #include<cmath>
13 using namespace std;
14
15 struct node
16 {
17 double x,y;
18 double l,r;
19 }island[1010];
20
21 int n,d;
22 bool no;//记录是否 有可行方案
23 int num;
24
25 int cmp(const void *p,const void *q)
26 {
27 node *tp = (node *)p;
28 node *tq = (node *)q;
29 if(tp->l!= tq->l)return tp->l > tq->l ? 1 : 0 ;
30 else tp->r > tq->r ? 0 : 1 ;
31
32 }
33
34 void jisuan()
35 {
36 int i;
37 for(i=0;i<n;++i)
38 {
39 double t = d*d-island[i].y*island[i].y;
40 t = sqrt(t);
41 island[i].l = island[i].x-t;
42 island[i].r = island[i].x+t;
43 }
44 }
45
46 void slove()
47 {
48 int i;
49 double tr;
50 tr=island[0].r;
51 num++;
52 for(i=1;i<n;++i)
53 {
54 if(island[i].l>tr)
55 {
56 num++;
57 tr= island[i].r;
58 }
59 else
60 {
61 tr= island[i].r < tr ? island[i].r : tr;
62 }
63 }
64
65 }
66
67 int main()
68 {
69 int i;
70 int k=0;
71 while(cin>>n>>d&&(n || d))
72 {
73 no = false;
74 num = 0;
75 for(i=0;i<n;++i)
76 {
77 cin>>island[i].x>>island[i].y;
78 if(island[i].y>d)no=true;
79 }
80 if(no)
81 {
82 cout<<"Case "<<++k<<": -1"<<endl<<endl;
83 continue;
84 }
85 jisuan();
86 qsort(island,n,sizeof(island[0]),cmp);
87 slove();
88 cout<<"Case "<<++k<<": "<<num<<endl<<endl;
89 }
90 system("pause");
91 return 0;
92 }

 

posted @ 2012-04-02 16:26  知行执行  阅读(236)  评论(0编辑  收藏  举报