Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 52536 Accepted Submission(s): 13852
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.
Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100007
using namespace std;
struct Point
{
double x, y;
}*pt;
int *a;
int n;
bool cmp_x(Point a, Point b)
{
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
bool cmp_y(int id1, int id2) {
return pt[id1].y < pt[id2].y;
}
double getDis(const Point &a, const Point &b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x*x + y*y);
}
double solve(int l, int r)
{
double ans = 0;
if (r - l + 1 <= 3)
{
if (r - l + 1 == 1) return ans;
ans = getDis(pt[l], pt[l + 1]);
if (r - l + 1 == 2) return ans;
for (int i = l; i < r; ++i)
{
for (int j = i + 1; j <= r; ++j)
{
ans = min(ans, getDis(pt[i], pt[j]));
}
}
return ans;
}
int m = (l + r) >> 1;
double s1 = solve(l, m);
double s2 = solve(m + 1, r);
ans = min(s1, s2);
int k = 0;
for (int i = m; i >= l && pt[m].x - pt[i].x <= ans; --i)
a[k++] = i;
for (int i = m + 1; i <= r && pt[i].x - pt[m].x <= ans; ++i)
a[k++] = i;
sort(a, a + k, cmp_y);
for (int i = 0; i < k; ++i)
{
for (int j = i + 1; j < k && j <= i + 7; ++j)//此处只搜索点i后的七个点非常关键,不加这个限制会超时,而这个限制的根据由来贴在下面
{
ans = min(ans, getDis(pt[a[i]], pt[a[j]]));
}
}
return ans;
}
int main()
{
while (~scanf("%d", &n))
{
if (!n) break;
pt = new Point[n];
a = new int[n];
for (int i = 0; i < n; ++i) scanf("%lf%lf", &pt[i].x, &pt[i].y);
sort(pt, pt + n, cmp_x);
printf("%.2lf\n", solve(0, n - 1) / 2.0);
delete []pt;
pt = NULL;
delete []a;
a = NULL;
}
return 0;
}
//AC(2)
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 100007
using namespace std;
struct Point
{
double x,y;
}pt[N];
int a[N];
int n;
bool cmp(Point a, Point b)
{
if (a.x != b.x) return a.x < b.x;
else return a.y < b.y;
}
bool cmp_y(int id1, int id2){
return pt[id1].y < pt[id2].y;
}
double getDis(const Point &a, const Point &b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return sqrt(x*x + y*y);
}
double solve(int l, int r)
{
double ans = 0;
if (r - l + 1 <= 3)
{
if (r - l + 1 == 1) return ans;
ans = getDis(pt[l], pt[l + 1]);
if (r - l + 1 == 2) return ans;
for (int i = l; i < r; ++i)
{
for (int j = i + 1; j <= r; ++j)
{
ans = min(ans, getDis(pt[i],pt[j]));
}
}
return ans;
}
int m = (l + r) >> 1;
double s1 = solve(l, m);
double s2 = solve(m + 1, r);
ans = min(s1,s2);
int k = 0;
for (int i = m; i >= l && pt[m].x - pt[i].x <= ans; --i) a[k++] = i;
for (int i = m + 1; i <= r && pt[i].x - pt[m].x <= ans; ++i) a[k++] = i;
sort(a, a + k, cmp_y);//按照y的大小顺序从小到大排序
for (int i = 0; i < k; ++i)
{
for (int j = i + 1; j < k && j <= i + 7; ++j)
{
ans = min(ans, getDis(pt[a[i]], pt[a[j]]));//使用a[i]可以确保所对比的点就是两区域的中间点,a[x]其实就是第几个点,pt[a[x]]
//是取这个点的详细信息,
}
}
return ans;
}
int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
for (int i = 0; i < n; ++i) scanf("%lf%lf",&pt[i].x, &pt[i].y);
sort(pt, pt + n, cmp);
printf("%.2lf\n",solve(0, n - 1)/2.0);
}
return 0;
}
404




被折叠的 条评论
为什么被折叠?



