poj1328Radar Installation(贪心—区间选点)

题目链接:

啊哈哈,点我点我

题目:

Radar Installation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 52037   Accepted: 11682

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

Beijing 2002

这道题目是贪心里面的区间选点问题。。贪心策略是:选区间的最右端的点.

思路:首先抽象出这个模型,以海岛为圆心,雷达距离为半径,求出在陆地上的区间,则雷达选在

这个区间之类那么必定能够扫描到这个海岛。。求出所有区间后,就转化成区间选点问题。。

还有就是代码中的那个标准end要用double,我wa了好久。。。

代码为:

#include<cstdio>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=1000+10;

struct Line
{
   double le,ri;
}line[maxn];

bool cmp(Line a,Line b)
{
   if(a.ri!=b.ri)
        return a.ri<b.ri;
   else
        return a.le>b.le;
}

int main()
{
    bool ok;
    int u,v,cas=1;
    int cnt;
	double End;
    int n,d;
    while(~scanf("%d%d",&n,&d))
    {
    	if(n==0&&d==0)   return 0;
        cnt=0;
        ok=false;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&u,&v);
            if(v>d)
                ok=true;
            else
            {
                line[i].le=(double)u-sqrt((double)(d*d-v*v));
                line[i].ri=(double)u+sqrt((double)(d*d-v*v));
            }
        }
        if(ok)
            printf("Case %d: -1\n",cas++);
        else
        {
           sort(line+1,line+1+n,cmp);
           cnt=0;
           End=-INF;
           for(int i=1;i<=n;i++)
           {
               if(line[i].le>End)
               {
                   cnt++;
                   End=line[i].ri;
               }
           }
           printf("Case %d: %d\n",cas++,cnt);
        }
    }
    return 0;
}

poj1328Radar Installation(贪心—区间选点),布布扣,bubuko.com

时间: 2024-08-05 19:33:10

poj1328Radar Installation(贪心—区间选点)的相关文章

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

UVa1615 Highway (贪心,区间选点)

链接:http://bak.vjudge.net/problem/UVA-1615 分析:以村庄为圆心,D为半径作圆,可选区间就是高速公路在圆内的部分(包括两个交点),按区间右端点从小到大,如若右端点相同再按左端点从大到小排好序,接下来就是很纯粹的区间选点问题. 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 6 const int maxn =

poj 1328 Radar Installation【贪心区间选点】

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 22   Accepted Submission(s) : 9 Problem Description Assume the coasting is an infinite straight line. Land is in one side of coa

POJ1328 Radar Installation 【贪心&#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 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 loca

poj1328Radar Installation 贪心

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64472   Accepted: 14497 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 loca

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~

UVA - 1615 Highway(高速公路)(贪心+区间选点)

题意:给定平面上n(n<=105)个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过D. 分析: 1.根据D可以算出每个点在x轴上的可选区域,计算出区域的左右端点. 2.贪心选点,每次都选这个区域的最右端点,这样此端点可存在于尽可能多的区域. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cs

nyoj 710 外星人的供给站【贪心区间选点】

外星人的供给站 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 外星人指的是地球以外的智慧生命.外星人长的是不是与地球上的人一样并不重要,但起码应该符合我们目前对生命基本形式的认识.比如,我们所知的任何生命都离不开液态水,并且都是基于化学元素碳(C)的有机分子组合成的复杂有机体. 42岁的天文学家Dr. Kong已经执著地观测ZDM-777星球十多年了,这个被称为“战神”的红色星球让他如此着迷.在过去的十多年中,他经常有一些令人激动的发现.ZDM-777星球表面有着

POJ1328 Radar Installation 【贪心&amp;#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 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 loca