HDU 1007 Quoit Design
2016-03-29 22:33
1126 查看
Problem Description
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
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.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated
by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
Sample Input
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
Sample Output
0.71
0.00
0.75
平面最近点对求解,采用分治的方法,感觉做着做着就有cdq分治的感觉了,果然分治都是相通的啊,顺便一说,这里用到了鸽巢原理。
#include<cstdio>
#include<stack>
#include<ctime>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 2e9 + 10;
const int maxn = 1e5 + 10;
int n;
struct point
{
double x, y;
void read(){ scanf("%lf%lf", &x, &y); }
bool operator<(const point&a)const
{
return x < a.x;
}
}a[maxn], b[maxn];
bool cmp(const point&a, const point&b)
{
return a.y < b.y;
}
double dis(point&a, point&b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double solve(int l, int r)
{
if (l + 1 == r) { sort(a + l, a + r + 1, cmp); return dis(a[l], a[r]); }
if (l + 2 == r) { sort(a + l, a + r + 1, cmp); return min(dis(a[l], a[l + 1]), min(dis(a[l], a[r]), dis(a[r], a[r - 1]))); }
int mid = l + r >> 1, tot = 0;
double d = min(solve(l, mid), solve(mid + 1, r));
for (int i = mid + 1; i <= r; i++) if (a[i].x - a[mid].x <= d) b[tot++] = a[i];
for (int i = l, j = 0; i <= mid; i++)
{
for (; j < tot&&a[i].y - b[j].y > d; j++);
for (int k = j; k < tot&&b[k].y - a[i].y<d; k++) d = min(d, dis(a[i], b[k]));
}
for (int i = l, j = l, k = mid + 1; i <= r; i++)
{
if (j <= mid && (k>r || a[j].y < a[k].y)) b[i] = a[j++];
else b[i] = a[k++];
}
for (int i = l; i <= r; i++) a[i] = b[i];
return d;
}
int main()
{
while (scanf("%d", &n), n)
{
for (int i = 0; i < n; i++) a[i].read();
sort(a, a + n);
printf("%.2lf\n", solve(0, n - 1) / 2);
}
return 0;
}
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
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.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated
by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.
Sample Input
2
0 0
1 1
2
1 1
1 1
3
-1.5 0
0 0
0 1.5
0
Sample Output
0.71
0.00
0.75
平面最近点对求解,采用分治的方法,感觉做着做着就有cdq分治的感觉了,果然分治都是相通的啊,顺便一说,这里用到了鸽巢原理。
#include<cstdio>
#include<stack>
#include<ctime>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int INF = 2e9 + 10;
const int maxn = 1e5 + 10;
int n;
struct point
{
double x, y;
void read(){ scanf("%lf%lf", &x, &y); }
bool operator<(const point&a)const
{
return x < a.x;
}
}a[maxn], b[maxn];
bool cmp(const point&a, const point&b)
{
return a.y < b.y;
}
double dis(point&a, point&b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double solve(int l, int r)
{
if (l + 1 == r) { sort(a + l, a + r + 1, cmp); return dis(a[l], a[r]); }
if (l + 2 == r) { sort(a + l, a + r + 1, cmp); return min(dis(a[l], a[l + 1]), min(dis(a[l], a[r]), dis(a[r], a[r - 1]))); }
int mid = l + r >> 1, tot = 0;
double d = min(solve(l, mid), solve(mid + 1, r));
for (int i = mid + 1; i <= r; i++) if (a[i].x - a[mid].x <= d) b[tot++] = a[i];
for (int i = l, j = 0; i <= mid; i++)
{
for (; j < tot&&a[i].y - b[j].y > d; j++);
for (int k = j; k < tot&&b[k].y - a[i].y<d; k++) d = min(d, dis(a[i], b[k]));
}
for (int i = l, j = l, k = mid + 1; i <= r; i++)
{
if (j <= mid && (k>r || a[j].y < a[k].y)) b[i] = a[j++];
else b[i] = a[k++];
}
for (int i = l; i <= r; i++) a[i] = b[i];
return d;
}
int main()
{
while (scanf("%d", &n), n)
{
for (int i = 0; i < n; i++) a[i].read();
sort(a, a + n);
printf("%.2lf\n", solve(0, n - 1) / 2);
}
return 0;
}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)
- 2015-11-11 hdu新生赛 C题(结束后一发AC)