【贪心】时空定位II

【贪心】时空定位II

题目描述

有一块空间,横向长w,纵向长为h,在它的横向中心线上不同位置处装有n(n≤10000)个点状的定位装置,每个定位装置i定位的效果是让以它为中心半径为Ri的圆都被覆盖。请在给出的定位装置中选择尽量少的定位装置,把整个空间全部覆盖。

输入

第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个定位装置,w表示空间的横向长度,h表示空间的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个定位装置的横坐标(最左边为0),ri表示该定位装置能覆盖的圆的半径。

输出

每组测试数据输出一个正整数,表示共需要多少个定位装置,每个输出单独占一行。
如果不存在一种能够把整个空间覆盖的方案,请输出0。

样例输入

2
2 8 6
1 1
4 5
2 10 6
4 5
6 5

样例输出

1
2
分析:将雷达覆盖的边界起点从小到大排序,对起点小于等于当前起点的雷达,选取最大的终点即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e3+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,cnt,flag,w,h,all;
pair<double,double>a[maxn];
double x,y,now;
int main()
{
    int i,j,k,t;
    scanf("%d",&t);
    while(t--)
    {
        flag=true;
        all=cnt=0;
        scanf("%d%d%d",&n,&w,&h);
        rep(i,0,n-1){
            scanf("%lf%lf",&x,&y);
            if(y>1.0*h/2)
                a[all].fi=x-sqrt(y*y-h*h/4),a[all++].se=x+sqrt(y*y-h*h/4);
        }
        sort(a,a+all);
        now=0;
        //rep(i,0,all-1)cout<<a[i].fi<<" "<<a[i].se<<endl;
        for(i=0;i<all;i++)
        {
            if(now>=w)break;
            double ma=-1;
            while(i<all&&a[i].fi<=now)ma=max(ma,a[i].se),i++;
            if(ma!=-1)flag=true,now=ma,cnt++,i--;
            else
            {
                flag=false;break;
            }
        }
        if(flag&&now>=w)printf("%d
",cnt);
        else puts("0");
    }
    //system ("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/dyzll/p/5697016.html