Radar Installation

打开题目链接:http://poj.org/problem?id=1328

 

这也算是贪心吗,要是这样的话,贪心看起来好像也很简单啊,行了,贪心就先练到这吧

本题大意:

这题是说,在一条直线两侧分别是海跟陆地,也就是说这条线是海岸线。在海里有若干个用x,y坐标标示位置的岛屿,岸上安排雷达站,每个雷达站有覆盖范围。要求求出最少需要多少个雷达站覆盖所有岛屿 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int M = 1001;
struct xy    //岛屿坐标
{
    double l;
    double r;
}p[M];


double min(double a,double b)
{
    return a < b ? a :b;
}


int d,n;
bool cmp(struct xy a,struct xy b )
{
    return a.l < b.l;
}


int main()
{
    double a,b;
    int icase = 0;
    while(scanf( "%d%d",&n ,&d)&& !(n==0 && d==0) )//n为岛的个数,d为能探测到的半径
    {
        memset( p, 0, sizeof( p ) );

        icase ++;
        int sum = 1;
        bool flag = false;
        for(int i = 1; i <= n;i++ )
        {
           cin>>a>>b;
           if(b>d)  ///当有岛屿离海岸的距离大于雷达覆盖半径,则输出不可能。
               flag = true;
           else
           {
            p[i].l = a-sqrt(d*d - b*b);
            p[i].r = a+sqrt(d*d - b*b);
           }
            
        }
        if(flag)  //大于半径
        {
            printf("Case %d: -1
",icase);
            continue;
        }
        else
        {
            sort(p+1,p+n+1,cmp); 
            double s = p[1].r;
            for(int i = 2; i <= n; i++) ///从左向右
            {
                if(p[i].l > s)
                {
                    sum++;
                    s = p[i].r;
                }
                else
                    s = min(s,p[i].r);
            }
        }
      printf("Case %d: %d
",icase,sum);

    }
    return 0;



 

原文地址:https://www.cnblogs.com/zswbky/p/5432046.html