A http://hihocoder.com/problemset/problem/1227
题意:给出平面上m个点,任选一个点为圆心,选最小的半径使得恰好n个点在圆内。
解法:枚举圆心,然后对所有点到该圆心距离排序,取第n大的距离作为半径,若第n+1大的在圆内或圆上不合法。
1 //#define debug 2 //#define txtout 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<cmath> 7 #include<cctype> 8 #include<ctime> 9 #include<iostream> 10 #include<algorithm> 11 #include<vector> 12 #include<queue> 13 #include<stack> 14 #include<map> 15 #include<set> 16 #define mt(a,b) memset(a,b,sizeof(a)) 17 using namespace std; 18 typedef long long LL; 19 const double eps=1e-8; 20 const double pi=acos(-1.0); 21 const int inf=0x3f3f3f3f; 22 const int M=1e2+10; 23 struct point { 24 double x,y; 25 } p[M]; 26 double dist[M]; 27 int n,m; 28 29 double Square(double x) { ///平方 30 return x*x; 31 } 32 double Distance(point a,point b) { ///平面两点距离 33 return sqrt(Square(a.x-b.x)+Square(a.y-b.y)); 34 } 35 36 int choose(point c) { 37 for(int i=0; i<m; i++) { 38 dist[i]=Distance(c,p[i]); 39 } 40 sort(dist,dist+m); 41 double r=dist[n-1]; 42 int int_r=(int)(r+1); 43 if(n<m&&dist[n]<int_r+eps) 44 return inf; 45 return int_r; 46 } 47 int solve() { 48 if(n>m) return -1; 49 int res=inf; 50 for(int i=0; i<m; i++) { 51 res=min(res,choose(p[i])); 52 } 53 if(res==inf) res=-1; 54 return res; 55 } 56 int main() { 57 #ifdef txtout 58 freopen("in.txt","r",stdin); 59 freopen("out.txt","w",stdout); 60 #endif 61 int t; 62 while(~scanf("%d",&t)) { 63 while(t--) { 64 scanf("%d%d",&m,&n); 65 for(int i=0; i<m; i++) { 66 scanf("%lf%lf",&p[i].x,&p[i].y); 67 } 68 printf("%d\n",solve()); 69 } 70 } 71 return 0; 72 }
end
时间: 2024-10-17 20:18:56