HDU1007-Quoit Design&POJ3714-Raid(最近点对问题)

Quoit Design

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

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
ZJCPC2004

//最接近点对
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std ;
const int MAXN=1e6+10;
const int INF=0x3f3f3f3f;
int n,temp[MAXN];

struct Point
{
    double x,y;
}S[MAXN];

bool cmpx(const Point &a,const Point &b)
{
    if(a.x==b.x) return a.y<b.y;
    else return a.x<b.x;
}

bool cmpy(const int &a,const int &b)
{
    return S[a].y<S[b].y;
}

double dist(int i,int j)
{
    double x=(S[i].x-S[j].x)*(S[i].x-S[j].x);
    double y=(S[i].y-S[j].y)*(S[i].y-S[j].y);
    return sqrt(x+y);
}

double closestPair(int L,int R)
{
    double d=INF ;
    if(L==R)//若左右边界重合
    return d ;
    if(L+1==R)//若左右边界差值为1即只有两个点
    return dist(L,R);

    int M=(L+R)>>1;//从中间划分
    double d1=closestPair(L,M);
    double d2=closestPair(M+1,R);
    d=min(d1,d2);//递归求出最小值d
    int i,j,k=0;
    for(i=L;i<=R;i++)//找出d范围内的点存入temp[]
    if(fabs(S[M].x-S[i].x)<=d)
      temp[k++]=i;
    sort(temp,temp+k,cmpy);//按y坐标排序
    for(i=0;i<k;i++)//穷举存入temp[]内的这些点
    {
        for(j=i+1;j<k&&j<i+12&&S[temp[j]].y-S[temp[i]].y<d;j++)//找出最近(p,q)
        {
          double d3=dist(temp[i],temp[j]);
          if(d>d3) d=d3;
        }
    }
    return d ;
}

int main()
{
    //freopen("nearest.in","r",stdin);
    //freopen("nearest.out","w",stdout);
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        if(n==0) break;
        for(int i=0;i<n;i++)
            cin>>S[i].x>>S[i].y;
        sort(S,S+n,cmpx);//点按x坐标由小到大排序
        cout<<fixed<<setprecision(2)<<closestPair(0,n-1)/2<<endl;
        //printf( "%.2lf\n" , closestPair( 0 , n - 1 ));
    }
    return 0 ;
}

Raid

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 12345 Accepted: 3646
Description

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union’s attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.  

Output

For each test case output the minimum distance with precision of three decimal placed in a separate line.

Sample Input

2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Sample Output

1.414
0.000
Source

POJ Founder Monthly Contest – 2008.12.28, Dagger

注意事项:加一个标志位判断一下是否属于同类。

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int MAXN=1e6+10;
const int INF=0x3f3f3f3f;
int n,temp[MAXN];

struct Point
{
    double x,y;
    bool flag;
}S[MAXN];

bool cmpx(const Point &a,const Point &b)
{
    if(a.x==b.x) return a.y<b.y;
    else return a.x<b.x;
}

bool cmpy(const int &a,const int &b)
{
    return S[a].y<S[b].y;
}

double dist(int i,int j)
{
    double x=(S[i].x-S[j].x)*(S[i].x-S[j].x);
    double y=(S[i].y-S[j].y)*(S[i].y-S[j].y);
    return sqrt(x+y);
}

double closestPair(int L,int R)
{
    double d=INF ;
    //若左右边界重合
    if(L==R)  return d;
    if(L+1==R)//若左右边界差值为1即只有两个点
    {
        if(S[L].flag==S[R].flag) return d;
        else return dist(L,R);
    }

    int M=(L+R)>>1;//从中间划分
    double d1=closestPair(L,M);
    double d2=closestPair(M+1,R);
    d=min(d1,d2);//递归求出最小值d
    int i,j,k=0;
    for(i=L;i<=R;i++)//找出d范围内的点存入temp[]
    {
        if(fabs(S[M].x-S[i].x)<=d)
            temp[k++]=i;
    }
    sort(temp,temp+k,cmpy);//按y坐标排序
    for(i=0;i<k;i++)//穷举存入temp[]内的这些点
    {
        for(j=i+1;j<k&&(S[temp[i]].flag!=S[temp[j]].flag)&&S[temp[j]].y-S[temp[i]].y<d;j++)//找出最近(p,q)
        {
            double d3=dist(temp[i],temp[j]);
            if(d>d3) d=d3;
        }
    }
    return d ;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&S[i].x,&S[i].y);
            S[i].flag=true;
        }
        for(int i=n;i<2*n;i++)
        {
            scanf("%lf%lf",&S[i].x,&S[i].y);
            S[i].flag=false;
        }
        sort(S,S+2*n,cmpx);//点按x坐标由小到大排序
        printf("%.3f\n",closestPair(0,2*n-1));
    }
    return 0 ;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值