poj1328 Radar Installation(贪心 策略要选好)

https://vjudge.net/problem/POJ-1328

贪心策略选错了恐怕就完了吧。。

一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的。。。因为空中是个圆弧。

后来知道了通过对每个island求雷达范围,将这些范围排序,但是去重策略也没想对。因为有两种情况,都需要更新t值,但是只有一种需要ans++。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<set>
 8 #define INF 0x3f3f3f3f
 9 typedef long long ll;
10 using namespace std; 
11 typedef struct{
12     double x, y;
13 }Node;
14 Node node[1010]; 
15 double lengthb(int c, int a)
16 {
17     return sqrt(c*c-a*a);
18 }
19 bool cmp(const Node a, const Node b)
20 {
21     return a.x<b.x;
22 }
23 int main()
24 {
25     int n, d, a, b, kase=0;
26     while(cin >> n >> d){
27         if(!n&&!d) break;
28         int flag=1;
29         for(int i = 0; i < n; i++){
30             cin >> a >> b;
31             if(d < b) flag=0;
32             else{
33                 double tmp = lengthb(d, b);
34                 node[i].x = a-tmp, node[i].y = a+tmp;    
35             } 
36         }
37         cout << "Case " << ++kase << ": ";
38         if(!flag){
39             cout << "-1" << endl; 
40             continue;
41         }
42         sort(node, node+n, cmp);
43         double t = node[0].y;
44         int ans = 1;
45         for(int i = 1; i < n; i++){
46             if(node[i].y<t||abs(node[i].y-t)<1e-6){//这种情况不用ans++,但该雷达的检测范围却因此缩小了 
47                 t = node[i].y;
48             }
49             else if(t<node[i].x){
50                 t = node[i].y; 
51                 ans++;
52             } 
53         } 
54         cout << ans << endl;
55     }
56     return 0;
57 }
原文地址:https://www.cnblogs.com/Surprisezang/p/9006980.html