ACM-ICPC 2018 沈阳赛区网络预赛 The cake is a lie(单位圆模板加二分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/89291453

参考poj1981的博客

蜜汁tle,,sort里面写cmp就tle,放到friend里面就过,,,谁能解答一下,,,还有kuangbin的求distance会tle,,为什么

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <ctime>
using namespace std;


const double eps=1e-8;
int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}

struct point{
    double x,y,at;
    int fg;
    point(){}
    point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    double operator *(const point &b)const 
    {
        return x*b.x+y*b.y;
    }
    point operator /(const double &k)const{
        return point(x/k,y/k);
    }
    double distance(point p){
        return hypot(x - p.x,y - p.y);
    }
    double len(){
        return hypot(x,y);//库函数
    }
    point trunc(double r){
        double l = len();
        if(!sgn(l))return *this;
        r /= l;
        return point(x*r,y*r);
    }
    double operator ^(const point &b)const{
        return x*b.y-y*b.x;
    }
    point operator +(const point &b)const{
        return point(x+b.x,y+b.y);
    }
    point rotleft(){
        return point( - y,x);
    }
    point rotright(){
        return point(y, - x);
    }
	bool friend operator <(const point &a,const point &b)
    {
        double p=a.at,q=b.at;
        if(p!=q){
            return p<q;
        }
        return a.fg<b.fg;
    }
};


double dis(point a,point b)
{
    return sqrt((a-b)*(a-b));
}
const int MAXN=333;
point p[333];
point p1[111111];
int n,s;

	
const double pi=acos(-1.0);
bool solve(double x)
{
    int ans1=1;
    for(int i=0;i<n;i++)
    {
        int cont=0;
        for(int j=0;j<n;j++)
        {
            if(i==j) continue;
			double len=dis(p[i],p[j]);
            if(len>2.0*x) continue;
            double at=acos(len/(2*x));
            double at1=atan2(p[j].y-p[i].y,p[j].x-p[i].x);
            if(at1<0) at1+=2*pi;
            p1[cont].at=at1-at+2*pi;
            p1[cont++].fg=1;
            p1[cont].at=at1+at+2*pi;
            p1[cont++].fg=-1;
        }
        sort(p1,p1+cont);
        int sum=1;
        for(int j=0;j<cont;j++) 
        {
            if(p1[j].fg==1) sum++;
            else sum--;
            ans1=max(ans1,sum);
        }
    }
    if(ans1>=s) return true;
    else return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&s);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        double d;
        scanf("%lf",&d);
        if(s>n)
        {
            printf("The cake is a lie.\n");
            continue;
        }
        double l=0;
        double r=3333333;
        while(sgn(r-l)>0)
        {
            double mid=(l+r)/2;
            if(solve(mid))
                r=mid;
            else l=mid;
        }
        printf("%.4lf\n",l+d);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/89291453