标签:
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 47104 Accepted Submission(s): 12318
题目本身有很简单的做法就是排个序找到最小的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 1007 Quoit Design(二分+浮点数精度控制)
标签:
原文地址:http://www.cnblogs.com/Blackops/p/5792590.html