poj 1328 Radar Installation——贪心

6 篇文章 0 订阅

Radar Installation
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 50199
Accepted: 11261

Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. 

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates. 
 
Figure A Sample Input of Radar Installations


Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. 

The input is terminated by a line containing pair of zeros 

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

Source

大体意思就是以x轴为岸,在岸上安装雷达,覆盖海洋(x轴上方)里的所有岛屿,岛屿会以坐标的形式给出,要求用的雷达数最小—— 思路:以岛屿为中心以给出的雷达范围为半径画圆,求出和x轴的两个(或是一个)交点作为区间,然后区间贪心选点,使区间数最少但全部覆盖所有区间。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>

using namespace std;

struct linshi
{
    double s,e;
}ls[1010];

int cmp(struct linshi a,struct linshi b)
{
   if(a.e<b.e)
   {
       return 1;
   }
   else if(fabs(a.e-b.e)<1e-6)
   {
       return a.s<b.s?1:0;
   }
   else
   {
       return 0;
   }
}//说句实话,这是我进ACM集训队以来第一次用快排函数对double类型排序,
//可是耽误了我好几天的时间在弄对,差点让我失去信心啊,排序法则就是按
//区间的右端点从小到大排序,如果相等,则按左端点从小到大大排序,要注意,
//这个相等是按fabs(a.e-b.e)<1*10^9来实现的(这个技巧是杭电的朱宇轩大神告诉我的,
//在此对他表示感谢),还有那个return,如果是int类型这样写return a.s<b.s就可以了,
//但是double就必须像我那样写,呜呜~~~~光这个排序法则就弄了好几天.....

int main()
{
    int i,n,c=0,sum,flag;
    double x,y,r,et;
    while(scanf("%d%lf",&n,&r),n&&r)//接受岛屿数和雷达半径,雷达半径用double类型存储
    {
        c++;//case的数目
        flag=0;//标记变量
        sum=0;//所用雷达数初始化
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&x,&y);//接受岛屿的坐标
            if(y>r)//如果雷达半径不足以覆盖到岛屿
            {
                flag=1;//标记下来
            }
            else
            {
                ls[i].s=x-sqrt(r*r-y*y);
                ls[i].e=x+sqrt(r*r-y*y);//区间的计算
            }
        }
        if(flag==1)
        {
            printf("Case %d: %d\n",c,-1);//如果没法覆盖所有岛屿,打印-1
        }
        else
        {
            sort(ls,ls+n,cmp);//对所得到的区间进行排序排序法则见cmp()函数
            et=-999999999;//初始化尾坐标
            for(i=0;i<n;i++)
            {
                if(ls[i].s>et)//筛选合适的区间
                {
                    sum++;
                    et=ls[i].e;
                }
            }
            printf("Case %d: %d\n",c,sum);
        }

    }
    return 0;
}


好了,本题就是这样,一开始刷poj总是痛苦的,但很磨练意志,只能坚持下来,才能感觉poj的魅力,等到把题AC了,就会发现poj非常有意思....









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值