二分 starway

来源:互联网 发布:程序员三大浪漫 编辑:程序博客网 时间:2024/06/06 00:36

很容易联想到二分答案。同样重点在于如何check

什么时候不满足呢?有些点间距离<当前二分的答案*2,并且这些点连成了一排使至切断了0~m的整个平面。

很明显可以维护一个并查集,并维护这个联通块的最上端和最下端,比较即可。

优化:按x坐标排序,如果a[i].x-a[j].x>ans*2,那么前面的不可能再相交了。(否则n=6000,O(N*N*logN)会超时)

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#define de double#define eps (1e-7)using namespace std;int n,m,k,f[6003];de cnt[6005];struct node{de x,y;}a[6004];inline bool cmp(node x,node y){return x.x!=y.x?x.x<y.x:x.y<y.y;}inline int find(int x){return f[x]==x?x:f[x]=find(f[x]);}inline bool check(de x){    for(int i=1;i<=k;i++)f[i]=i,cnt[i]=a[i].y*1.0;    for(int i=1;i<=k;i++)    {        for(int j=i-1;j>=1;j--)        {            if(a[i].x-a[j].x>x*2)break;            if(sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y))/2.0-x>=eps)continue;            int fx=find(i),fy=find(j);            if(fx^fy)            {                if(a[fx].y<a[fy].y)                {                    f[fy]=fx;                    if(cnt[fx]<cnt[fy])cnt[fx]=cnt[fy];                }                else                {                    f[fx]=fy;                    if(cnt[fy]<cnt[fx])cnt[fy]=cnt[fx];                }            }        }        int fx=find(i);        if(a[fx].y<x*2.0&&cnt[fx]>m-x*2.0)return 0;    }    return 1;}int main(){    scanf("%d%d%d",&n,&m,&k);    for(int i=1;i<=k;i++)        scanf("%lf%lf",&a[i].x,&a[i].y);    sort(a+1,a+k+1,cmp);    de l=0.0,r=m/2.0,mid,ans;    while(r-l>eps)    {        mid=(l+r)/2.0;        if(check(mid))l=mid,ans=mid;        else r=mid;    }    printf("%.8lf",ans);}
原创粉丝点击