HDU 1007 Quoit Design(二分+浮点数精度控制)

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47104    Accepted Submission(s): 12318

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
 
Author
CHEN, Yue
 
Source

题目链接:HDU 1007

题目本身有很简单的做法就是排个序找到最小的nearst_r输出即可,但是如果用二分来做的话就很容易WA,浮点数二分跟整数二分还是有点不同的,第一次运气好把eps定为1e-3居然也过了……后来了解了一下浮点数的二分其实没这么简单,精度一般要控制在1e-5,如果题目要求更高那eps就要更小,还有L与R就不需要再加减了,直接等于mid就可以,条件也要改为R-L>eps……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=100010;
struct info
{
double x,y;
bool operator<(const info &b)const
{
if(y==b.y)
return x<b.x;
return y<b.y;
}
double nearst_r;
};
info pos[N];
int n;
inline double getdx(const info &a,const info &b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
inline bool check(const double &r)
{
for (int i=1; i<n; ++i)
if(r>pos[i].nearst_r)
return false;
return true;
}
int main(void)
{
int i,j;
double L,R,ans,mid,eps=1e-5;
while (~scanf("%d",&n)&&n)
{
for (i=0; i<n; ++i)
scanf("%lf%lf",&pos[i].x,&pos[i].y);
sort(pos,pos+n);
for (i=1; i<n; ++i)
pos[i].nearst_r=getdx(pos[i],pos[i-1])/2.0;
L=0,R=1e9;
while (R-L>=eps)
{
mid=(L+R)/2.0;
if(check(mid))
L=mid;
else
R=mid;
}
printf("%.2lf\n",mid);
}
return 0;
}
上一篇:hdu 1164:Eddy's research I(水题,数学题,筛法)


下一篇:.NET并行编程 - 并行方式