POJ 3714 最近点对

简介:

题意:给出两个点的集合,求属于不同集合的最近点对。

这题是最近点对的变形,在求两点距离的时候加以判断是否来自不同集合就行。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 200005
struct point
{
    double x,y;
    int f;
} p[maxn],p1[maxn];
int cmpx(point a,point b)
{
    return a.x<b.x;
}
int cmpy(point a,point b)
{
    return a.y<b.y;
}
double dis(point a,point b)
{
    if(a.f!=b.f)
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    return 1e30;
}
double getmin(int l,int r)
{
    double ans;
    if(l>=r)   return 1e30;
    if(l+1==r)
        return dis(p[l],p[r]);
    int m=(l+r)>>1;
    ans=min(getmin(l,m),getmin(m+1,r));
    int cn=0;
    for(int i=l; i<=r; i++)
        if(fabs(p[i].x-p[m].x)<ans)
            p1[cn++]=p[i];
    sort(p1,p1+cn,cmpy);
    for(int i=0; i<cn; i++)
        for(int j=i+1; j<cn&&p1[j].y-p1[i].y<ans; j++)
            ans=min(ans,dis(p1[i],p1[j]));
    return ans;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y),p[i].f=0;
        for(int i=n; i<n+n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y),p[i].f=1;
        sort(p,p+n+n,cmpx);
        printf("%.3f\n",getmin(0,n+n-1));
    }
    return 0;
}


目录
相关文章
|
存储
POJ 1936 All in All
POJ 1936 All in All
64 0
poj 3620
题意:给出一个矩阵,其中有些格子干燥、有些潮湿。       如果一个潮湿的格子的相邻的四个方向有格子也是潮湿的,那么它们就可以构成更大       的湖泊,求最大的湖泊。       也就是求出最大的连在一块儿的潮湿的格子的数目。
555 0
poj 3664
http://poj.org/problem?id=3664 进行两轮选举,第一轮选前n进入第二轮,第二轮选最高   #include #include using namespace std; struct vote { int a,b; int c; ...
708 0
poj 1456 Supermarket
点击打开链接poj 1456 思路: 贪心+并查集 分析: 1 题目的意思是给定n个物品的利润和出售的最后时间,求最大的利润 2 比较明显的贪心问题,按照利润排序,假设当前是第i个物品,那么利润为pi出售的时间为di,那么假设di还没有物品销售那么肯定先销售第i个物品,否则找di~1这些时间里面是否有没有销售物品 3 如果按照2的思路做法最坏的情况是O(n^2),但是数据比较弱可以过。
785 0
|
网络协议 网络架构
poj 2675 songs
点击打开链接poj 2675 思路:相邻交换法 分析: 1 题目要求找到一种序列使得所求的值最小 2 那么根据输入的序列我们做如下处理,设sum[i]表示播放第i首歌的和 sum[i] = f[i]*(len[1]+.
830 0