hdu1007

Quoit Design

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


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
//这个会超时
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<math.h>
using namespace std;

struct node1
{
    double x,y;
}node[100010],node2[100010];

int cmpx(node1 p1,node1 p2)
{
    return p1.x<p2.x;
}

int cmpy(node1 p1,node1 p2)
{
    return p1.y<p2.y;
}

double Distance(node1 p1,node1 p2)
{
    return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);
}

double minn(double p1,double p2)
{
    return p1>p2?p2:p1;
}

double findd(int l,int r)
{
  if(r==l+1)
     return Distance(node[l],node[r]);
  if(r==l+2)
    return minn(Distance(node[l],node[l+1]),minn(Distance(node[l+1],node[r]),Distance(node[l],node[r])));
  int mid=l+(r-l)/2;
  double ans=minn(findd(l,mid),findd(mid+1,r));
  int v=0;
  for(int i=l;i<=r;i++)
    {
      if(node[i].x>=node[mid].x-ans&&node[i].x<=node[mid].x+ans)
         node2[v++]=node[i];
    }
    sort(node2,node2+v,cmpy);
    for(int i=0;i<r-1;i++)
        for(int j=i+1;j<r;j++)
        {
            if(node2[j].y-node2[i].y>=ans)
               break;
            else
            ans=min(ans,Distance(node2[i],node2[j]));
        }
  return ans;
}

int main()
{
    int t;
    while(scanf("%d",&t)!=EOF&&t)
    {
        if(t==0)
            break;
        for(int i=0;i<t;i++)
         scanf("%lf%lf",&node[i].x,&node[i].y);
         sort(node,node+t,cmpx);

         printf("%.2lf\n",sqrt(findd(0,t-1))/2);
    }
    return 0;
}

  
  
0.71 0.00 0.75 题意是说给你n个点,是半径为1的圆的圆心。然后让你求其中两点距离最短的距离的一半! 暴力一定会超时,复杂度是n*n/2.所以想到了枚举,就是对x轴线排序,然后扫一下附近几个点 比较一下最小的,这样复杂度就成了n*(常数)了,就过了,但是后来想了想,万一一个y只很小但是x 却比较大的点距离更小,这不就错了么?但是找来找去环视会超时吧,。。于是想到了树, 但是不会写,没什么用。于是搜题解了,看了他的题解,讲的还好。 但是其中必须用结构体指针复制,不能用结构体(会超时),也许就是直接走地址快吧。附上两个AC 代码和一个超时
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<math.h>
using namespace std;

struct node1
{
    double x,y;
}node[100010];

int cmp(node1 p1,node1 p2)
{
    if(p1.x!=p2.x)
    return p1.x<p2.x;
    return p1.y<p2.y;
}

int main()
{
    int t;
    while(scanf("%d",&t)!=EOF&&t)
    {
        double maxx=10000,p,ans;
        if(t==0)
            break;
        for(int i=0;i<t;i++)
         scanf("%lf%lf",&node[i].x,&node[i].y);
         sort(node,node+t,cmp);
//        for(int i=0;i<t;i++)
//         printf("%lf %lf\n",node[i].x,node[i].y);
         for(int i=0;i<t-1;i++)
            for(int j=i+1;j<t&&j<=i+2;j++)
               {
                   p=(node[i].x-node[j].x)*(node[i].x-node[j].x)+(node[i].y-node[j].y)*(node[i].y-node[j].y);
                   if(p<maxx)
                      maxx=p;
               }
               ans=sqrt(maxx)/2;
               printf("%.2lf\n",ans);
    }
    return 0;
}

//这个是正规写法

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<math.h>
using namespace std;

 struct node1
{
    double x,y;
};

struct node1 node[100010],*node3[100010],*node2[100010];

int cmpx(node1 *p1,node1 *p2)
{
    return p1->x<p2->x;
}

int cmpy(node1 *p1,node1 *p2)
{
    return p1->y<p2->y;
}

double Distance(node1 *p1,node1 *p2)
{
    return (p1->x-p2->x)*(p1->x-p2->x)+(p1->y-p2->y)*(p1->y-p2->y);
}

double minn(double p1,double p2)
{
    return p1>p2?p2:p1;
}

double findd(int l,int r)
{
  if(r==l+1)
     return Distance(node3[l],node3[r]);
  if(r==l+2)
    return minn(Distance(node3[l],node3[l+1]),minn(Distance(node3[l+1],node3[r]),Distance(node3[l],node3[r])));
  int mid=l+(r-l)/2;
  double ans=minn(findd(l,mid),findd(mid+1,r));
  int v=0;
  for(int i=l;i<=r;i++)
    {
      if(node3[i]->x>=node3[mid]->x-ans&&node3[i]->x<=node3[mid]->x+ans)
         node2[v++]=node3[i];
    }
    sort(node2,node2+v,cmpy);
    for(int i=0;i<v-1;i++)
        for(int j=i+1;j<v;j++)
        {
            if(node2[j]->y-node2[i]->y>=ans)
               break;
            else
            ans=min(ans,Distance(node2[i],node2[j]));
        }
  return ans;
}

int main()
{
    int t;
    while(scanf("%d",&t)!=EOF&&t)
    {
        if(t==0)
            break;
        for(int i=0;i<t;i++)
         {
             scanf("%lf%lf",&node[i].x,&node[i].y);
             node3[i]=&node[i];
         }
         sort(node3,node3+t,cmpx);

         printf("%.2lf\n",sqrt(findd(0,t-1))/2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值