Radar Installation Page43 贪心

Radar Installation Page43 贪心

题意:选出最少的点,使得每一个区间都包含最少一个点

贪心思想:如果把所有区间按照右端点排序,遍历,记录下当前最右位置

  • 若左端点大于这个最右位置,则需要添加一个点,并修改最右位置;
  • 反之,则可以用之前的范围来覆盖此区间

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=2e6+7;
const int INF=1e9;
const ll INFF=1e18;
double eps=1e-5;
struct point
{
    double x,y;
}a[maxn];
struct node
{
    double l,r;
}p[maxn];
bool cmp(node a,node b){return a.r<b.r;}
int n,d,T=0;
int main()
{
    while(~scanf("%d%d",&n,&d))
    {
        if (n==0&&d==0)break;
        bool mark=true;
        int ans=0;
        rep(i,1,n)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
            if (a[i].y-d>eps)mark=false;
        }
        if (!mark)
        {
            printf("Case %d: -1\n",++T);
            continue;
        }
        rep(i,1,n)
        {
            p[i].l=a[i].x-sqrt((double)d*d-(double)a[i].y*a[i].y);
            p[i].r=a[i].x+sqrt((double)d*d-(double)a[i].y*a[i].y);
        }
        sort(p+1,p+1+n,cmp);
        double pos=-1e9;
        rep(i,1,n)
        {
            if (p[i].l>pos)
            {
                ans++;
                pos=p[i].r;
            }
        }
        printf("Case %d: %d\n",++T,ans);
    }
    return 0;
}
发布了125 篇原创文章 · 获赞 8 · 访问量 9108

猜你喜欢

转载自blog.csdn.net/w_udixixi/article/details/104807477
今日推荐