高效算法设计专项:UVa 10691

这道题可以转化为区间选点问题,即给定一些区间,找到最少的点使得每个区间至少存在一个点。只不过这道题上的区间是在一个圆上的,注意到n小于200,可以枚举开始的区间,然后分别进行一次贪心即可,时间复杂度为O(n^2)。只不过由于是在圆上,所以有很多细节需要注意,WA了n炮才调过了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=410;
const double eps=1e-6;
const double pi=acos(-1.0);
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
int n,m;
double d;
struct interval
{
    double s,e;
    interval(double s=0,double e=0):s(s),e(e){}
    bool operator<(const interval& tmp) const
    {
        if(dcmp(e-tmp.e)<0) return true;
        if(dcmp(e-tmp.e)==0)
        {
            double s1=dcmp(e-s)<0?s-2*pi:s;
            double s2=dcmp(tmp.e<tmp.s)<0?tmp.s-2*pi:tmp.s;
            if(dcmp(s1-s2)>0) return true;
        }
        return false;
    }
    bool operator==(const interval& tmp) const
    {
        return dcmp(e-tmp.e)==0&&dcmp(s-tmp.s)==0;
    }
} p[maxn];
double length(double x,double y)
{
    return sqrt(x*x+y*y);
}
double formal(double rad)
{
    if(dcmp(rad-2*pi)>=0) return rad-2*pi;
    if(dcmp(rad)<0) return rad+2*pi;
    return rad;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%lf",&n,&d);
        m=0;
        for(int i=0;i<n;i++)
        {
            double x,y;
            scanf("%lf%lf",&x,&y);
            if(dcmp(length(x,y)-d)<=0) continue;
            double angle=asin(d/length(x,y));
            p[m++]=interval(formal(atan2(y,x)-angle),formal(atan2(y,x)+angle));
        }
        sort(p,p+m);
        m=unique(p,p+m)-p;
        for(int i=0;i<m;i++)
        {
            p[i+m]=p[i];
        }
        int ans=m;
        for(int i=0;i<m;i++)
        {
            if(i&&dcmp(p[i].e-p[i-1].e)==0) continue;
            int tmp=1,pre=i;
            for(int j=i+1;j<m+i;j++)
            {
                double s=dcmp(p[j].e-p[j].s)<0?p[j].s-2*pi:p[j].s;
                if(dcmp(s-p[pre].e)<=0) continue;
                else
                {
                    pre=j;
                    tmp++;
                }
            }
            ans=min(ans,tmp);
            double v=p[i].e+2*eps;
            for(int j=0;j<2*m;j++) p[j]=interval(formal(p[j].s-v),formal(p[j].e-v));
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值