poj 1328

1、贪心算法,刚开始不知道哪里搞错了,总是超时....

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<queue>
 5 #include<math.h>
 6 #include <iostream>
 7 #include <string>
 8 #include <vector>
 9 #include <cstdlib>
10 #include <cmath>
11 #include <map>
12 #include <list>
13 #include <ctime>
14 #include <queue>
15 #include <stdlib.h>
16 using namespace std;
17 
18 struct range {
19     double l, r;
20 };
21 
22 bool comp(const range &A, const range &B) {
23     if (A.r < B.r) {
24         return true;
25     } else if (A.r == B.r) {
26         return A.l < B.l;
27     } else {
28         return false;
29     }
30 }
31 
32 int main() {
33     int n, d, ans, flag;
34     int ct = 1;
35     while (1) {
36         vector<range> data;
37         cin >> n >> d;
38         if (0 == d && 0 == n)
39             break;
40         ans = 1;
41         flag = 1;
42         if (d < 0)
43             flag = 0;
44         for (int i = 0; i < n; i++) {
45             int x, y;
46             cin >> x >> y;
47             double t;
48             t = sqrt(((double) d * d - (double) y * y));
49             range tk;
50             tk.l = (double) x - t;
51             tk.r = (double) x + t;
52             data.push_back(tk);
53             if (y > d || (-y) > d)
54                 flag = 0;
55         }
56         if (flag == 0)
57             ans = -1;
58         else {
59             if (0 == n)
60                 ans = 0;
61             else {
62                 sort(data.begin(), data.end(), comp);
63                 double left = data[0].r;
64                 for (int i = 1; i < n; i++) {
65                     if (data[i].l > left) {
66                         ans++;
67                         left = data[i].r;
68                     }
69                 }
70             }
71         }
72         cout << "Case " << ct << ": " << ans << endl;
73         ct++;
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/kakamilan/p/2995890.html