程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1007 (最近點對問題)

HDU 1007 (最近點對問題)

編輯:C++入門知識

Quoit Design
Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20332    Accepted Submission(s): 5216


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

 


經典最近點對問題  //類似題目  POJ   3714 Raid (沒做)

參考 《算法導論》、《計算機算法設計與分析》(王曉東)

算導上的關於 證明P中至多有8個點可能處於該d x 2d 矩形區域中 有待理解

 


code:


[cpp]
#include <cmath>  
#include <cstdio>  
#include <cstdlib>  
#include <algorithm>  
#define N 100002  
#define INF 0x3f3f3f3f  
using namespace std; 
struct NODE{ double x, y;}; 
NODE edge[N], b[N]; 
int cmp_x(const void *a, const void *b) 

    NODE *aa = (NODE*) a; 
    NODE *bb = (NODE*) b; 
    return aa->x > bb->x ? 1: -1; 

int cmp_y(const void *a, const void *b) 

    NODE *aa = (NODE*) a; 
    NODE *bb = (NODE*) b; 
    return aa->y > bb->y ? 1: -1; 

 
inline double dis( NODE a, NODE b) 

    double xx =a.x - b.x, yy = a.y - b.y; 
    return sqrt(xx*xx+yy*yy); 

double find(int l, int r)//前閉後開區間   

    if(r-l == 1) return INF; 
    if(r-l == 2) return dis(edge[l], edge[r-1]); 
    int i, j, k; 
    double dm, tmp; 
    int m = (l+r)/2; 
    double  d1 = find(l, m); 
    double  d2 = find(m, r); 
    dm = d1 < d2 ? d1:d2; 
    for(i=l,k=0;i<r;i++) 
        if(fabs(edge[m].x-edge[i].x)<=dm ) 
            b[k++] = edge[i]; 
    qsort(b,k,sizeof(b[0]),cmp_y); 
    for(i=0;i<k;i++) 
        for(j=i+1;j<k;j++) 
        { 
            tmp = dis(b[i],b[j]); 
            if(tmp<dm)dm=tmp; 
        } 
    return dm; 
 

int main() 

    int n, i; 
    while(scanf("%d",&n),n) 
    { 
        for(i=0;i<n;i++)           
        // #1   scanf("%lf%lf",&edge[i].x, &edge[i].y);  
        /* #2 */scanf("%lf%lf",&edge[i].y, &edge[i].x); 
        qsort(edge,n,sizeof(edge[0]),cmp_x); 
        double d = find(0,n); 
        printf("%.2lf\n",d/2); 
    } 
    return 0; 

/*
#1 按x軸排序 果斷TLE   Time Limit Exceeded    1007    5000MS  1812K
#2 按y軸排序 AC        Accepted             1007    1125MS  1840K
*/ 

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define N 100002
#define INF 0x3f3f3f3f
using namespace std;
struct NODE{ double x, y;};
NODE edge[N], b[N];
int cmp_x(const void *a, const void *b)
{
 NODE *aa = (NODE*) a;
 NODE *bb = (NODE*) b;
 return aa->x > bb->x ? 1: -1;
}
int cmp_y(const void *a, const void *b)
{
 NODE *aa = (NODE*) a;
 NODE *bb = (NODE*) b;
 return aa->y > bb->y ? 1: -1;
}

inline double dis( NODE a, NODE b)
{
 double xx =a.x - b.x, yy = a.y - b.y;
 return sqrt(xx*xx+yy*yy);
}
double find(int l, int r)//前閉後開區間
{
 if(r-l == 1) return INF;
 if(r-l == 2) return dis(edge[l], edge[r-1]);
 int i, j, k;
 double dm, tmp;
 int m = (l+r)/2;
 double  d1 = find(l, m);
 double  d2 = find(m, r);
 dm = d1 < d2 ? d1:d2;
 for(i=l,k=0;i<r;i++)
  if(fabs(edge[m].x-edge[i].x)<=dm )
   b[k++] = edge[i];
 qsort(b,k,sizeof(b[0]),cmp_y);
 for(i=0;i<k;i++)
  for(j=i+1;j<k;j++)
  {
   tmp = dis(b[i],b[j]);
   if(tmp<dm)dm=tmp;
  }
 return dm;

}
int main()
{
 int n, i;
 while(scanf("%d",&n),n)
 {
  for(i=0;i<n;i++)         
        // #1   scanf("%lf%lf",&edge[i].x, &edge[i].y);
  /* #2 */scanf("%lf%lf",&edge[i].y, &edge[i].x);
  qsort(edge,n,sizeof(edge[0]),cmp_x);
  double d = find(0,n);
  printf("%.2lf\n",d/2);
 }
 return 0;
}
/*
#1 按x軸排序 果斷TLE   Time Limit Exceeded 1007 5000MS 1812K
#2 按y軸排序 AC        Accepted             1007 1125MS 1840K
*/
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved