其他分享
首页 > 其他分享> > CodeForces 314 D Sereja and Straight Lines

CodeForces 314 D Sereja and Straight Lines

作者:互联网

传送门

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct ab
{
    int x,y;
} t[500005];
int maxl[500005],minl[500005],maxr[500005],minr[500005],n;
int cmp(ab x,ab y)
{
    return x.x<y.x;
}
int work(int x)
{
    int h=1;
    for(int i=1;i<=n;i++)//枚举横着的区间
    {
        for(;h<=n;h++)
        {
            if(t[h].x-t[i].x>x) break;
        }
        if(max(maxl[i-1],maxr[h])-min(minl[i-1],minr[h])<=x) return 1;//判断竖着的区间
    }
    return 0;
}
signed main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        int x,y;
        scanf("%lld%lld",&x,&y);
        t[i].x=x-y;//旋转
        t[i].y=x+y;
    }
    sort(t+1,t+1+n,cmp);
    maxl[0]=-2e10;
    minl[0]=2e10;
    for(int i=1;i<=n;i++)//预处理纵坐标的最大最小值,判断竖着的区间时有用
    {
        maxl[i]=max(maxl[i-1],t[i].y);
        minl[i]=min(minl[i-1],t[i].y);
    }
    maxr[n+1]=-2e10;
    minr[n+1]=2e10;
    for(int i=n;i>=1;i--)//预处理纵坐标的最大最小值,判断竖着的区间时有用
    {
        maxr[i]=max(maxr[i+1],t[i].y);
        minr[i]=min(minr[i+1],t[i].y);
    }
    int l=0,r=1e10;
    while(l<r)//二分
    {
        int mid=(l+r)/2;
        if(!work(mid)) l=mid+1;
        else r=mid;
    }
    printf("%lf\n",(double)r/2);//注意二分的是区间的大小,所以答案还要除以二才是距离
    return 0;
}

标签:直线,int,Straight,Lines,距离,maxr,minr,500005,Sereja
来源: https://www.cnblogs.com/sunyukai/p/10388193.html