杭电OJ 1007 Quoit Design

Quoit Design

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


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

 


首先我借鉴了一下他人的博客,原文地址http://www.cnblogs.com/ACMDoli/articles/4292029.html

原文也分析了,我也就不分析了,这就是分冶的方法

做这个题的时候,让我充分感受到了C++,C两者效率的差别

一开始我用的C++的标准输入输出流进行输入输出,杭电OJ给我的结果是内存超出了,但是把全部的输入输出改为C的输入输出,内存才占用五分之一不到。。。。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstdio>
using namespace std;

const double eps = 1e-6;
const long long  MAXN = 100010;
const double INF = 1e20;

struct Point
{
    double x,y;
};

Point p[MAXN];
Point temp[MAXN];

double Fabs(double x)
{
    if(x>0)
        return x;
    else return -x;
}

double dist(Point x,Point y)
{
    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}

bool cmpxy(Point x,Point y)
{
    if(x.x!=y.x) return x.x<y.x;
    else return x.y<y.y;
}

bool cmpy(Point x,Point y)
{
    return x.y<y.y;
}

double Closest_Pair(int left,int right)
{
    double d = INF;
    if(left == right)
        return d;
    if(left+1 == right)
        return dist(p[left],p[right]);
    int mid = (left+right)/2;
    double d1 = Closest_Pair(left,mid);
    double d2 = Closest_Pair(mid+1,right);
    d = min(d1,d2);
    int k = 0;
    for(int i = left; i < right; i++)
    {
        if(Fabs(p[mid].x-p[i].x)<d)
            temp[k++] = p[i];
    }
    sort(temp,temp+k,cmpy);
    for(int i = 0; i < k; i++)
    {
        for(int j = i + 1; j < k && temp[j].y - temp[i].y<d; j++)
        {
            d = min(dist(temp[j],temp[i]),d);
        }
    }
    return d;
}

int main(int argc,char **argv)
{
    int n;
    while(scanf("%d",&n)==1)
    {
        if(n==0)
            break;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf %lf",&p[i].x,&p[i].y);
        }
        sort(p,p+n,cmpxy);
        double ret = Closest_Pair(0,n-1)/2;
        printf("%.2lf\n",ret);
        //cout<<showpoint<<setprecision(2)<<ret<<(ret?"":"0")<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值