UVALive - 2519 Radar Installation

来源:互联网 发布:华泰软件工程有限公司 编辑:程序博客网 时间:2024/06/06 10:54

题意:给定n个岛屿坐标,和雷达半径,雷达只能放在x轴上,求出最少放几个雷达能覆盖满所有岛屿。

思路:贪心。每个岛屿都有最左和最右最远放雷达能覆盖到的点,我们把这作为左右区间。只要在区间中选中一个位置放雷达。就可以满足该岛屿被覆盖,转换为区间选点问题。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int MAXN = 1005;struct Node{    double l;    double r;    friend bool operator <(const Node &a,const Node &b){        if (a.r != b.r)            return a.r < b.r;        return a.l < b.l;    }}arr[MAXN];int main(){    int n,r,cas = 1;    while (scanf("%d%d",&n,&r) != EOF && r + n){        memset(arr,0,sizeof(arr));        int flag = 0;        for (int i = 0; i < n; i++){            int x,y;            scanf("%d%d",&x,&y);            if (y > r){                flag = 1;                continue;            }            int t = sqrt(r*r - y*y);            arr[i].l = x - t;            arr[i].r = x + t;        }                printf("Case %d: ",cas++);        if (flag)            printf("-1\n");        else {            sort(arr,arr+n);            int cur = 1;            double start = arr[0].r;            for (int i = 1; i < n; i++){                if (arr[i].l <= start)                    continue;                cur++;                start = arr[i].r;            }            printf("%d\n",cur);        }    }    return 0;}