hihoCoder1227 The Cats' Feeding Spots【暴力】
2015-10-12 19:18
288 查看
题目链接:
http://hihocoder.com/problemset/problem/1227
题目大意:
给你 M 个点的坐标(二维平面),从这 M 个点中找出 N 个点,使得以这 N 个点中的某一点
为圆心,且半径为整数的圆包含这 N 个点,同时保证圆周上没有点。求这个最小的半径,
如果没有就输出"-1"。
解题思路:
点数最多有 100 个,那么要预先求出 100 个点之间的相互距离,保存在数组 D[][] 中。然
后遍历每个点,对每个点到其他点之间的距离进行排序,判断第 N 个点是否符合要求,并
找出满足要求最小的答案 Ans。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 110;
double X[MAXN],Y[MAXN],D[MAXN][MAXN];
double Dist(double x,double y)
{
return sqrt(x*x+y*y);
}
int main()
{
int T,N,M;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&M,&N);
memset(D,0,sizeof(D));
for(int i = 1; i <= M; ++i)
{
scanf("%lf%lf",&X[i],&Y[i]);
for(int j = 1; j <= i; ++j)
D[i][j] = D[j][i] = Dist(X[i]-X[j],Y[i]-Y[j]);
}
if(N > M)
{
printf("-1\n");
continue;
}
int Ans = INF;
for(int i = 1; i <= M; ++i)
{
sort(D[i]+1,D[i]+1+M);
int R = ceil(D[i]
);
if(D[i]
>= R)
R++;
if(N < M && R >= D[i][N+1])
continue;
if(Ans > R)
Ans = R;
}
if(Ans == INF)
printf("-1\n");
else
printf("%d\n",Ans);
}
return 0;
}
http://hihocoder.com/problemset/problem/1227
题目大意:
给你 M 个点的坐标(二维平面),从这 M 个点中找出 N 个点,使得以这 N 个点中的某一点
为圆心,且半径为整数的圆包含这 N 个点,同时保证圆周上没有点。求这个最小的半径,
如果没有就输出"-1"。
解题思路:
点数最多有 100 个,那么要预先求出 100 个点之间的相互距离,保存在数组 D[][] 中。然
后遍历每个点,对每个点到其他点之间的距离进行排序,判断第 N 个点是否符合要求,并
找出满足要求最小的答案 Ans。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 110;
double X[MAXN],Y[MAXN],D[MAXN][MAXN];
double Dist(double x,double y)
{
return sqrt(x*x+y*y);
}
int main()
{
int T,N,M;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&M,&N);
memset(D,0,sizeof(D));
for(int i = 1; i <= M; ++i)
{
scanf("%lf%lf",&X[i],&Y[i]);
for(int j = 1; j <= i; ++j)
D[i][j] = D[j][i] = Dist(X[i]-X[j],Y[i]-Y[j]);
}
if(N > M)
{
printf("-1\n");
continue;
}
int Ans = INF;
for(int i = 1; i <= M; ++i)
{
sort(D[i]+1,D[i]+1+M);
int R = ceil(D[i]
);
if(D[i]
>= R)
R++;
if(N < M && R >= D[i][N+1])
continue;
if(Ans > R)
Ans = R;
}
if(Ans == INF)
printf("-1\n");
else
printf("%d\n",Ans);
}
return 0;
}
相关文章推荐
- html2canvas根据DOM元素样式实现网页截图
- 开始用JavaScript写一些小东西
- 解决自动换行,自适应高度。常见与标签样式的 ViewGroup
- Understanding the differences between client/server and peer-to-peer networks
- html5 实现网页截屏 页面生成图片(图文)
- BZOJ1029: [JSOI2007]建筑抢修
- JS添加可信站点、修改ActiveX安全设置,禁用弹出窗口阻止程序的方法
- POI操作Office导出Html文档
- POI操作Office导出Html文档
- js 回调(callback)
- swiper.js——初识
- html5本地缓存LocalStorage
- node.js安装和使用步骤-windows
- js url传值中文乱码之解决之道
- ReactJs 组件间通信
- Json解析
- jQuery锚点滚动js
- SeekBar样式
- jquery.uploadify.3.2.1 试用在IE9,IE10中 上传文件的按钮会无法点击
- css一些有效特性