(暴力+精度)hihocoder - 1227 The Cats' Feeding Spots

原题链接:http://hihocoder.com/problemset/problem/1227


 

题意:在m个点中找一个点为圆心, 求能够精确包括n个点的最小的半径,要求半径为整数。


 

分析:一开始没看清题意,以为是凭空的一个圆,后来才发现是m个点中的某一个,感觉自己有点sb。。

然后想到直接暴力,100*100,暴力每个点,求每个点到它的距离,排个序求出第n个,向上取整,如果触碰到外围的一个点就表示不行。

需要注意的是,如果m==n就不需要额外判外围的点,而且当m>n的时候,应该输出-1.


 

代码:

 1 #include <set>
 2 #include <map>
 3 #include <list>
 4 #include <cmath>
 5 #include <queue>
 6 #include <vector>
 7 #include <bitset>
 8 #include <string>
 9 #include <cctype>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 
16 using namespace std;
17 
18 typedef long long ll;
19 typedef unsigned long long ull;
20 #define inf (0x3f3f3f3f)
21 #define lnf (0x3f3f3f3f3f3f3f3f)
22 #define eps (1e-8)
23 int sgn(double a) {return a < -eps ? -1 : a < eps ? 0 : 1;}
24 
25 //--------------------------
26 
27 
28 int t;
29 int m, n;
30 
31 class point {
32 public:
33     double x, y;
34     point(double x, double y) {
35         this->x = x;
36         this->y = y;
37     }
38 
39 };
40 
41 vector<point> p;
42 
43 double dis(point p1, point p2) {
44     return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
45 }
46 
47 void solve() {
48     scanf("%d", &t);
49     while (t--) {
50         p.clear();
51         scanf("%d%d", &m, &n);
52         for (int i = 0; i < m; i++) {
53             double x, y;
54             scanf("%lf%lf", &x, &y);
55             p.push_back(point(x, y));
56         }
57         int ans = inf;
58         for (int i = 0; i < m; i++) {
59             double res;
60             double ds[110];
61             for (int j = 0; j < m; j++) {
62                 ds[j] = dis(p[i], p[j]);
63             }
64             sort(ds, ds + m);
65             res = ceil(ds[n - 1]);
66             if (res == ds[n - 1])res += 1;
67             if (m > n && res >= ds[n])continue;
68             ans = min(ans, (int)res);
69         }
70         if (ans == inf || n > m) {
71             puts("-1");
72         } else {
73             printf("%d\n", ans);
74         }
75     }
76 
77 }
78 
79 int main() {
80 
81 #ifndef ONLINE_JUDGE
82     freopen("1.in", "r", stdin);
83     //freopen("1.out", "w", stdout);
84 #endif
85     //iostream::sync_with_stdio(false);
86     solve();
87     return 0;
88 }

 

感悟:这种坑精度的题感觉自己有点懵逼,要多坑一坑。。

posted @ 2016-12-08 19:57  tak_fate  阅读(214)  评论(0编辑  收藏  举报