hihoCoder1227 The Cats' Feeding Spots 二分

编程入门 行业动态 更新时间:2024-10-07 08:26:02

hihoCoder1227    The <a href=https://www.elefans.com/category/jswz/34/1758674.html style=Cats' Feeding Spots 二分"/>

hihoCoder1227 The Cats' Feeding Spots 二分

题目链接:


题目大意:平面上有m个点,要从这m个点当中找出n个点,使得包含这n个点的圆的半径(圆心为n个点当中的某一点且半径为整数)最小,同时保证圆周上没有点。


分析:枚举这m个点,以每个点为圆心找出一个符合条件的半径,然后找出这些半径中的最小值即可。至于如何找出半径,我们可以二分[-1001,1001]区间。


实现代码如下:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define INF 0x7fffffff
struct POINT
{double x,y;bool operator <(const POINT &a) const{if(x==a.x) return y<a.y;return x<a.x;}
}p[110];
double d[110][110];
int m,n,minr;
bool flag;
int min(int a,int b)
{return a<b?a:b;
}
double dist(POINT a,POINT b)
{return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void init()
{for(int i=0;i<m;i++)for(int j=i;j<m;j++)d[i][j]=d[j][i]=dist(p[i],p[j]);
}
int judge(int cnt,int rad)
{int num=0;flag=true;for(int i=0;i<m;i++){if(d[cnt][i]<rad*1.0) num++;if(d[cnt][i]==rad*1.0) flag=false;}return num;
}
void solve()
{init();minr=INF;for(int i=0;i<m;i++){int l=-1001,r=1001;while(l<=r){int m=(l+r)>>1;int tmp=judge(i,m);if(tmp==n&&flag)  minr=min(minr,m);if(tmp>=n) r=m-1;else l=m+1;}}if(minr==INF) puts("-1");else printf("%d\n",minr);
}
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);for(int i=0;i<m;i++)scanf("%lf%lf",&p[i].x,&p[i].y);sort(p,p+m);solve();}return 0;
}


更多推荐

hihoCoder1227 The Cats' Feeding Spots 二分

本文发布于:2024-02-13 12:45:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1758780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Cats   Spots   Feeding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!